Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 13개 있습니다.)
디버깅 기술: 22. VS.NET SP1 + .NET Framework 소스 코드 디버깅
; https://www.sysnet.pe.kr/2/0/623

개발 환경 구성: 112. Visual Studio 2010 - .NET Framework 소스 코드 디버깅
; https://www.sysnet.pe.kr/2/0/1009

개발 환경 구성: 143. Visual Studio 2010 - .NET Framework 소스 코드 디버깅 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/1243

개발 환경 구성: 297. 소스 코드가 없는 닷넷 어셈블리를 디버깅할 때 지역 변숫값을 확인하는 방법
; https://www.sysnet.pe.kr/2/0/11036

개발 환경 구성: 391. (GitHub 등과 직접 연동해) 소스 코드 디버깅을 쉽게 해 주는 SourceLink
; https://www.sysnet.pe.kr/2/0/11630

VS.NET IDE: 126. 디컴파일된 소스에 탐색을 사용하도록 설정(Enable navigation to decompiled sources)
; https://www.sysnet.pe.kr/2/0/11689

VS.NET IDE: 145. NuGet + Github 라이브러리 디버깅 관련 옵션 3가지 - "Enable Just My Code" / "Enable Source Link support" / "Suppress JIT optimization on module load (Managed only)"
; https://www.sysnet.pe.kr/2/0/12200

개발 환경 구성: 500. (PDB 연결이 없는) DLL의 소스 코드 디버깅을 dotPeek 도구로 해결하는 방법
; https://www.sysnet.pe.kr/2/0/12281

VS.NET IDE: 153. 닷넷 응용 프로그램에서의 "My Code" 범위와 "Enable Just My Code"의 역할
; https://www.sysnet.pe.kr/2/0/12401

VS.NET IDE: 170. Visual Studio에서 .NET Core/5+ 역어셈블 소스코드 확인하는 방법
; https://www.sysnet.pe.kr/2/0/12880

VS.NET IDE: 177. 비주얼 스튜디오 2022를 이용한 (소스 코드가 없는) 닷넷 모듈 디버깅 - "외부 원본(External Sources)"
; https://www.sysnet.pe.kr/2/0/13109

VS.NET IDE: 180. Visual Studio - 닷넷 소스 코드 디버깅 중 "Decompile source code"가 동작하는 않는 문제
; https://www.sysnet.pe.kr/2/0/13247

VS.NET IDE: 189. Visual Studio - 닷넷 소스코드 디컴파일 찾기가 안 될 때
; https://www.sysnet.pe.kr/2/0/13554




(GitHub 등과 직접 연동해) 소스 코드 디버깅을 쉽게 해 주는 SourceLink

지난 글들에서 .NET Core 2.1에 도입된 기능 소개에 이어,

.NET Core 2.1 - 확장 도구(Tools) 관리
; https://www.sysnet.pe.kr/2/0/11538

.NET Core 2.1 - Tiered Compilation 도입
; https://www.sysnet.pe.kr/2/0/11539

이번에는 SourceLink라는 것에 대해 알아보겠습니다.

Announcing .NET Core 2.1 - SourceLink
; https://devblogs.microsoft.com/dotnet/announcing-net-core-2-1/

dotnet/sourcelink 
; https://github.com/dotnet/sourcelink

(* 문서에 의하면 .NET Core 쪽으로만 지원하는 듯이 씌여져 있지만,
현재 .NET Full Framework을 대상으로 한 프로젝트들에서도 문제 없이 적용할 수 있습니다.)

사실 디버깅 중에 소스 코드와 매칭시키는 것은 pdb 파일을 통해 소스 서버로의 링크를 제공하는 것으로 예전부터 가능했습니다.

TFS Team Build + Source Server = 소스 코드 디버깅
; https://www.sysnet.pe.kr/2/0/600

TFS 2010의 소스 서버 수작업 구성
; https://www.sysnet.pe.kr/2/0/902

소스 서버 보안
; https://www.sysnet.pe.kr/2/0/658

그런데, 이게 좀 귀찮다는 문제가 있습니다. ^^ 예를 들어 소스 서버를 운영하는 것도 나름 일인 것이고, 이미 github에 올려진 많은 공개 라이브러리들은 굳이 소스 서버를 2중으로 별도로 운영하는 것이 비효율적입니다. 이런 문제를 해결해 github 등과 곧바로 연동하는 소스 코드 디버깅이 가능하도록 해주는 것이 바로 "SourceLink"입니다.




그럼, 실습을 한번 해볼까요? ^^ 현재(2018-07-27 기준) 지원하는 SourceLink의 연결은 3개입니다.

  • GitHub
  • Visual Studio Team Services or Team Foundation Server
  • GitLab

그냥 github로 정하고, .NET Core 2.1 대상으로 다음과 같이 간단한 예제 라이브러리 프로젝트를 하나 만들어 보겠습니다.

stjeong/NetCoreEmptyLibSample 
; https://github.com/stjeong/NetCoreEmptyLibSample

using System;

namespace NetCoreEmptyLibSample
{
    public class SampleType
    {
        public void DoNothing()
        {
            Console.WriteLine("Really!");
        }
    }
}

예제 프로젝트가 마련되었으니, 이제 이 프로젝트에 SourceLink 기능을 추가하는데 방법은, 해당 프로젝트의 csproj 파일에 다음의 내용을 추가하는 것입니다.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>

    <!-- Optional: Declare that the Repository URL can be published to NuSpec -->
    <PublishRepositoryUrl>true</PublishRepositoryUrl>
 
    <!-- Optional: Embed source files that are not tracked by the source control manager to the PDB -->
    <EmbedUntrackedSources>true</EmbedUntrackedSources>

    <!-- Optional: Build symbol package (.snupkg) to distribute the PDB containing Source Link -->
    <IncludeSymbols>true</IncludeSymbols>
    <SymbolPackageFormat>snupkg</SymbolPackageFormat>

  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
  </ItemGroup>

</Project>

참고로, 자신의 프로젝트가 github 이외의 소스 제어를 사용하고 있다면 각각 다음의 PackageReference를 사용할 수 있습니다.

[Azure Repos (former Visual Studio Team Services)]
<PackageReference Include="Microsoft.SourceLink.AzureRepos.Git" Version="1.0.0" PrivateAssets="All"/>

[Azure DevOps Server (former Team Foundation Server)]
<PackageReference Include="Microsoft.SourceLink.AzureDevOpsServer.Git" Version="1.0.0" PrivateAssets="All"/>

[GitLab]
<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.0.0" PrivateAssets="All"/>

이제 빌드하면 끝입니다. 빌드한 바이너리(DLL과 PDB)를 다른 사람의 PC에 복사해 실습할 수 있습니다.

(주의 사항: 빌드 시점의 github commit을 기준으로 하기 때문에 변경된 소스 코드가 있다면 반드시 commit을 한 후 빌드해 다른 개발자에게 전달해야 합니다.)




정말로 디버깅 중에 소스 코드 진입이 가능한지 ^^ .NET Core 콘솔 프로젝트를 하나 만들고, 복사한 NetCoreEmptyLibSample.dll을 참조 추가한 후, 해당 라이브러리에서 제공하는 타입을 사용합니다.

using NetCoreEmptyLibSample;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            SampleType st = new SampleType();
            st.DoNothing();

            Console.WriteLine("Hello World!");
        }
    }
}

비주얼 스튜디오에서 BP를 걸고 디버깅 시작 후 F11(Step Into) 키를 눌러 진입하려고 시도하면 다음의 창이 뜹니다. (만약, 아래의 창이 뜨지 않는다면 Tools / Options 창에서 "Debugging" 옵션의 "Enable Just My Code"를 해제합니다.)

sourcelink_1.png

당연히 "Download Source and Continue Debugging"이나 "...(Don't Ask Again)"이 붙은 항목을 선택 후 진행하면 곧바로 소스 코드를 github로부터 다운로드해 다음과 같이 메서드 내의 코드로 디버깅이 들어갑니다.

sourcelink_2.png

와~~~ ^^ 소스 서버 구성하는 것보다 훨씬 간단해서 좋습니다.

참고로 다운로드한 소스 코드는 다음의 폴더에 캐시됩니다.

%LOCALAPPDATA%\SourceServer\51066fd97788357507fc4214d608f858a5a8c0be23357e8587faf90868540688\NetCoreEmptyLibSample




이런 기능이 동작할 수 있었던 것은 바로 Microsoft.SourceLink.GitHub에 의해 pdb 파일 안에 다음과 같이 소스 코드 파일에 대한 경로를 github 경로로 대응시킨 설정을 포함하고 있기 때문입니다.

{
    "documents":
    {
        "E:\\NetCoreEmptyLibSample\\*":"https://raw.githubusercontent.com/stjeong/NetCoreEmptyLibSample/92eb70045cce2227195feb6b4e1de17bf767e1ca/*"
    }
}

(중간의 "92eb70045cce2227195feb6b4e1de17bf767e1ca" 문자열은 github commit에 대한 id입니다.)

그리고 Visual Studio 2017의 15.3 버전부터는 저 항목에 대한 설정을 읽어 처리를 해주기 때문에 디버깅이 가능했던 것이고, 따라서 SourceLink 기능이 정상 동작하려면 (물론 일반 디버깅에서도 마찬가지지만) 반드시 pdb 파일이 있어야 합니다. 이것 때문에 nuget에 올릴 패키지인 경우 csproj에 다음의 설정을 포함시켜야 합니다.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>

    ...[생략]...

    <!-- Optional: Include PDB in the built .nupkg -->
    <AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

  </PropertyGroup>

    ...[생략]...

</Project>

위의 설정이 없으면 nupkg 압축 파일 패키지에 pdb 파일이 누락되므로 SourceLink 기능이 동작하지 않게 됩니다.




위에서도 한 번 언급했지만, pdb 파일에 포함되는 경로는 github에 마지막으로 commit한 시점을 기준으로 합니다. 따라서 마지막 commit 이후 소스 코드를 변경하고 빌드한 경우, 여전히 지난 시점의 commit ID를 기준으로 pdb 파일에 경로가 들어가기 때문에 이 시점의 바이너리를 다른 개발자에게 배포하면 이후에 github commit을 했어도 유효하지 않은 소스 코드 경로가 되어 다음과 같은 식의 오류 화면이 뜨거나, 이전 버전의 소스 코드를 보여줄 수 있습니다.

You need to find Class1.cs to view the source for the current call stack frame

Source Link Error:
git-credential-manager.exe: Could not obtain credentials. Process failed with exit code -1.
ERROR: The request failed with code 404: "Not Found".

Source Link URL: https://raw.githubusercontent.com/stjeong/NetCoreEmptyLibSample/92eb70045cce2227195feb6b4e1de17bf767e1ca/ClassLibrary1/Class1.cs

Try one of the following options:
    - Authenticate for Source Link (https://raw.githubusercontent.com/stjeong/NetCoreEmptyLibSample/92eb70045cce2227195feb6b4e1de17bf767e1ca/ClassLibrary1/Class1.cs)

    - Browse and find Class1.cs...




csproj에 PackageReference 추가 이후, 빌드 시 다음과 같은 오류가 발생할 수 있습니다.

Error Unable to locate repository containing directory 'E:\NetCoreEmptyLibSample\NetCoreEmptyLibSample'.
%USERPROFILE%\.nuget\packages\microsoft.build.tasks.git\1.0.0-beta-63102-01\build\Microsoft.Build.Tasks.Git.targets 20  

원인 파악을 위해 Microsoft.Build.Tasks.Git.targets 파일의 20번째 라인을 보면,

<Microsoft.Build.Tasks.Git.LocateRepository Directory="$(MSBuildProjectDirectory)" >
    <Output TaskParameter="Id" PropertyName="_SourceControlLocalRepositoryId" />
</Microsoft.Build.Tasks.Git.LocateRepository>

그러니까, Git 소스 컨트롤을 추가하지 않은 프로젝트에 대해 저런 오류가 발생하는 것입니다.

또는 다음과 같은 오류가 발생한다면?

Error The value of SourceRoot.RepositoryUrl with identity 'E:\NetCoreEmptyLibSample\' is invalid: ''"
%USERPROFILE%\.nuget\packages\microsoft.sourcelink.github\1.0.0\build\Microsoft.SourceLink.GitHub.targets 38  

<Microsoft.SourceLink.GitHub.GetSourceLinkUrl SourceRoot="@(SourceRoot)" Hosts="@(SourceLinkGitHubHost)" ImplicitHost="$(SourceLinkImplicitRepositoryHost)">
    <Output TaskParameter="SourceLinkUrl" PropertyName="_SourceLinkUrlToUpdate"/>
</Microsoft.SourceLink.GitHub.GetSourceLinkUrl>"

Git 소스 컨트롤은 추가했지만 아직 github의 원격 repo를 연결하지 않은 상태의 프로젝트이기 때문입니다.




재미있는 것은, 원래 이 기능은 다음의 개발자가 구현했던 것이라고 합니다. ^^

ctaggart/SourceLink 
; https://github.com/ctaggart/SourceLink

문서에 보면, Visual Studio의 "Enable source server support" 기능을,

VS.NET SP1 + .NET Framework 소스 코드 디버깅
; https://www.sysnet.pe.kr/2/0/623

"SourceLink"의 버전 1로, "Enable Source Link support" 옵션을 버전 2로, 그리고 이번 글에서 설명한 SourceLink를 버전 3으로 표현하고 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/23/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2018-07-30 03시55분
[이성환] 얽... 본문에 이미지가 안 보여욤..ㅜ.ㅜ
[guest]
2018-07-30 05시50분
^^ 제가 실수했군요. 올렸으니 이제 보일 것입니다.
정성태
2023-06-23 10시05분
Improved .NET Debugging Experience with Source Link
; https://blog.ndepend.com/improved-net-debugging-experience-with-source-link
정성태

... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13230정성태1/26/20234105개발 환경 구성: 660. WSL 2 내부로부터 호스트 측의 네트워크로 UDP 데이터가 1개의 패킷으로만 제한되는 문제
13229정성태1/25/20235102.NET Framework: 2090. C# - UDP Datagram의 최대 크기
13228정성태1/24/20235227.NET Framework: 2089. C# - WMI 논리 디스크가 속한 물리 디스크의 정보를 얻는 방법 [2]파일 다운로드1
13227정성태1/23/20234926개발 환경 구성: 659. Windows - IP MTU 값을 바꿀 수 있을까요? [1]
13226정성태1/23/20234595.NET Framework: 2088. .NET 5부터 지원하는 GetRawSocketOption 사용 시 주의할 점
13225정성태1/21/20233848개발 환경 구성: 658. Windows에서 실행 중인 소켓 서버를 다른 PC 또는 WSL에서 접속할 수 없는 경우
13224정성태1/21/20234206Windows: 221. Windows - Private/Public/Domain이 아닌 네트워크 어댑터 단위로 방화벽을 on/off하는 방법
13223정성태1/20/20234407오류 유형: 838. RDP 연결 오류 - The two computers couldn't connect in the amount of time allotted
13222정성태1/20/20234093개발 환경 구성: 657. WSL - DockerDesktop.vhdx 파일 위치를 옮기는 방법
13221정성태1/19/20234321Linux: 57. C# - 리눅스 프로세스 메모리 정보파일 다운로드1
13220정성태1/19/20234433오류 유형: 837. NETSDK1045 The current .NET SDK does not support targeting .NET ...
13219정성태1/18/20234007Windows: 220. 네트워크의 인터넷 접속 가능 여부에 대한 판단 기준
13218정성태1/17/20233944VS.NET IDE: 178. Visual Studio 17.5 (Preview 2) - 포트 터널링을 이용한 웹 응용 프로그램의 외부 접근 허용
13217정성태1/13/20234524디버깅 기술: 185. windbg - 64비트 운영체제에서 작업 관리자로 뜬 32비트 프로세스의 덤프를 sos로 디버깅하는 방법
13216정성태1/12/20234776디버깅 기술: 184. windbg - 32비트 프로세스의 메모리 덤프인 경우 !peb 명령어로 나타나지 않는 환경 변수
13215정성태1/11/20236344Linux: 56. 리눅스 - /proc/pid/stat 정보를 이용해 프로세스의 CPU 사용량 구하는 방법 [1]
13214정성태1/10/20235880.NET Framework: 2087. .NET 6부터 SourceGenerator와 통합된 System.Text.Json [1]파일 다운로드1
13213정성태1/9/20235425오류 유형: 836. docker 이미지 빌드 시 "RUN apt install ..." 명령어가 실패하는 이유
13212정성태1/8/20235176기타: 85. 단정도/배정도 부동 소수점의 정밀도(Precision)에 따른 형변환 손실
13211정성태1/6/20235196웹: 42. (https가 아닌) http 다운로드를 막는 웹 브라우저
13210정성태1/5/20234296Windows: 219. 윈도우 x64의 경우 0x00000000`7ffe0000 아래의 주소는 왜 사용하지 않을까요?
13209정성태1/4/20234195Windows: 218. 왜 윈도우에서 가상 메모리 공간은 64KB 정렬이 된 걸까요?
13208정성태1/3/20234180.NET Framework: 2086. C# - Windows 운영체제의 2MB Large 페이지 크기 할당 방법파일 다운로드1
13207정성태12/26/20224444.NET Framework: 2085. C# - gpedit.msc의 "User Rights Assignment" 특권을 코드로 설정/해제하는 방법파일 다운로드1
13206정성태12/24/20224682.NET Framework: 2084. C# - GetTokenInformation으로 사용자 SID(Security identifiers) 구하는 방법 [3]파일 다운로드1
13205정성태12/24/20224990.NET Framework: 2083. C# - C++과의 연동을 위한 구조체의 fixed 배열 필드 사용 (2)파일 다운로드1
... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...