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
정성태

1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13298정성태3/27/20233683Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
13297정성태3/26/20234359Windows: 235. Win32 - Code Modal과 UI Modal
13296정성태3/25/20233697Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
13295정성태3/24/20233966Windows: 233. Win32 - modeless 대화창을 modal처럼 동작하게 만드는 방법파일 다운로드1
13294정성태3/22/20234140.NET Framework: 2105. LargeAddressAware 옵션이 적용된 닷넷 32비트 프로세스의 가용 메모리 - 두 번째
13293정성태3/22/20234206오류 유형: 853. dumpbin - warning LNK4048: Invalid format file; ignored
13292정성태3/21/20234332Windows: 232. C/C++ - 일반 창에도 사용 가능한 IsDialogMessage파일 다운로드1
13291정성태3/20/20234744.NET Framework: 2104. C# Windows Forms - WndProc 재정의와 IMessageFilter 사용 시의 차이점
13290정성태3/19/20234250.NET Framework: 2103. C# - 윈도우에서 기본 제공하는 FindText 대화창 사용법파일 다운로드1
13289정성태3/18/20233443Windows: 231. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 자식 윈도우를 생성하는 방법파일 다운로드1
13288정성태3/17/20233554Windows: 230. Win32 - 대화창의 DLU 단위를 pixel로 변경하는 방법파일 다운로드1
13287정성태3/16/20233711Windows: 229. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 윈도우를 직접 띄우는 방법파일 다운로드1
13286정성태3/15/20234168Windows: 228. Win32 - 리소스에 포함된 대화창 Template의 2진 코드 해석 방법
13285정성태3/14/20233760Windows: 227. Win32 C/C++ - Dialog Procedure를 재정의하는 방법파일 다운로드1
13284정성태3/13/20233955Windows: 226. Win32 C/C++ - Dialog에서 값을 반환하는 방법파일 다운로드1
13283정성태3/12/20233499오류 유형: 852. 파이썬 - TypeError: coercing to Unicode: need string or buffer, NoneType found
13282정성태3/12/20233822Linux: 58. WSL - nohup 옵션이 필요한 경우
13281정성태3/12/20233739Windows: 225. 윈도우 바탕화면의 아이콘들이 넓게 퍼지는 경우 [2]
13280정성태3/9/20234499개발 환경 구성: 670. WSL 2에서 호스팅 중인 TCP 서버를 외부에서 접근하는 방법
13279정성태3/9/20234048오류 유형: 851. 파이썬 ModuleNotFoundError: No module named '_cffi_backend'
13278정성태3/8/20234022개발 환경 구성: 669. WSL 2의 (init이 아닌) systemd 지원 [1]
13277정성태3/6/20234665개발 환경 구성: 668. 코드 사인용 인증서 신청 및 적용 방법(예: Digicert)
13276정성태3/5/20234338.NET Framework: 2102. C# 11 - ref struct/ref field를 위해 새롭게 도입된 scoped 예약어
13275정성태3/3/20234695.NET Framework: 2101. C# 11의 ref 필드 설명
13274정성태3/2/20234278.NET Framework: 2100. C# - ref 필드로 ref struct 타입을 허용하지 않는 이유
13273정성태2/28/20233973.NET Framework: 2099. C# - 관리 포인터로서의 ref 예약어 의미
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...