dotnet / sourcelink

Source Link enables a great source debugging experience for your users, by adding source control metadata to your built assets
MIT License
1.25k stars 122 forks source link

Sourcelink not working #617

Open sam-wheat opened 4 years ago

sam-wheat commented 4 years ago

Same symptoms as here.

In my case I am building a netcoreapp3.1 library and referencing it in a netcoreapp3.1 .exe.

Nuget package explorer gives same info as shown in referenced ticket above.

See also this.

I also tried this just trying anything I thought would work.

Triple checked that I configured VS according to this.

First time trying to implement this. Spent all day working on this with zero success. What am I doing wrong?

dotnet --version 3.1.300

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Version>0.0.30</Version>
    <Authors>me</Authors>
    <Company>me</Company>
    <Product />
    <Description>project</Description>
    <FileVersion>0.0.30.0</FileVersion>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageProjectUrl>https://github.com</PackageProjectUrl>
    <PackageLicenseUrl></PackageLicenseUrl>
    <RepositoryUrl>https://github.com</RepositoryUrl>
    <RepositoryType>git</RepositoryType>
    <Copyright>2020 me</Copyright>
    <PackageReleaseNotes>See Repository</PackageReleaseNotes>
    <PackageTags></PackageTags>
  </PropertyGroup>

  <PropertyGroup>

    <!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
    <PublishRepositoryUrl>true</PublishRepositoryUrl>

    <!-- Optional: Embed source files that are not tracked by the source control manager in the PDB -->
    <EmbedUntrackedSources>true</EmbedUntrackedSources>

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

  <!--START Workaround https://github.com/dotnet/sourcelink/issues/572 should be able to remove this after new sdk 3.1.3xx released -->
  <PropertyGroup>
    <TargetFrameworkMonikerAssemblyAttributesPath>$([System.IO.Path]::Combine('$(IntermediateOutputPath)','$(TargetFrameworkMoniker).AssemblyAttributes$(DefaultLanguageSourceExtension)'))</TargetFrameworkMonikerAssemblyAttributesPath>
  </PropertyGroup>
  <ItemGroup>
    <EmbeddedFiles Include="$(GeneratedAssemblyInfoFile)" />
  </ItemGroup>
  <!--END Workaround https://github.com/dotnet/sourcelink/issues/572-->

  <ItemGroup>
    <!-- Add PackageReference specific for your source control provider (see below) -->
    <!-- https://github.com/dotnet/sourcelink/blob/master/README.md -->
    <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
  </ItemGroup>
  <ItemGroup>
    <None Include="..\LICENSE">
      <Pack>True</Pack>
      <PackagePath></PackagePath>
    </None>
  </ItemGroup>

</Project>
0xced commented 4 years ago

Do you have a (non global) .gitignore in your repository containing obj? If you don't have one, it might be the reason why NuGet Package Explorer reports that your package contains untracked sources.

sam-wheat commented 4 years ago

Thanks - I do have a .gitignore. It is standard-issue unmodified and contains obj.

tmat commented 4 years ago

@0xced See https://github.com/PrismLibrary/Prism/issues/2119#issuecomment-637909263

tmat commented 4 years ago

Could you share the package?

sam-wheat commented 4 years ago

LeaderAnalytics.Caching.0.0.30.zip

tmat commented 4 years ago

The Source Link record in the PDB is empty:

<sourceLink><![CDATA[{"documents":{}}]]></sourceLink>
tmat commented 4 years ago

Do you not see a warning reported from the build? Can you run dotnet build /bl and share the resulting msbuild.binlog file?

sam-wheat commented 4 years ago

I was building from VS and this error is not shown in the UI.

C:\Users\sam.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : Source control information is not available - the generated source link is empty. [C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj]

The Source Link record in the PDB is empty:

I feel like I should know what this means........ but I don't sorry.

sam-wheat commented 4 years ago

Sorry for not getting it - can you kindly point me in the right direction to fix this? I am specifying the path to the repo.. what else do I need?

https://github.com/leaderanalytics/Caching
tmat commented 4 years ago

I need binlog file from command line build dotnet build /bl in order to find out why it doesn't work.

0xced commented 4 years ago

You need to reference the Microsoft.SourceLink.GitHub NuGet package for Source Link to work. I have created a pull request that will help you generate a NuGet package with Source Link: https://github.com/leaderanalytics/Caching/pull/1

sam-wheat commented 4 years ago

msbuild.zip

sam-wheat commented 4 years ago

@0xced thanks for the PR however the changes you made are already reflected in the .csproj shown above. The exceptions are this line:

<DebugType>embedded</DebugType>

and the addition of global.json.

I added those two changes to my local repo and ran the build command you suggested. Unfortunately I am now getting errors:

C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching>dotnet build -c Release /p:ContinuousIntegrationBuild=true
Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Users\sam\.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : Source control information is not available - the generated source link is empty. [C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj]
  LeaderAnalytics.Caching -> C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\bin\Release\netcoreapp3.1\LeaderAnalytics.Caching.dll
  Successfully created package 'C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\bin\Release\LeaderAnalytics.Caching.0.0.30.nupkg'.
C:\Program Files\dotnet\sdk\3.1.300\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5017: Cannot create a package that has no dependencies nor content. [C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj]

Build FAILED.

C:\Users\sam\.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : Source control information is not available - the generated source link is empty. [C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj]
C:\Program Files\dotnet\sdk\3.1.300\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5017: Cannot create a package that has no dependencies nor content. [C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj]
    1 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.39
0xced commented 4 years ago

Please commit the exact changes you have performed on the LeaderAnalytics.Caching.csproj file and push them to your repository. Thus, we can be sure we are on the same page. It's very important that we are are performing the dotnet build command on the exact same git repository state, without uncommited changes, especially when dealing with Source Link.

0xced commented 4 years ago

Also, it looks like you have several git remotes configured (Azure DevOps + GitHub), so you might need to set the remote name to the remote corresponding to GitHub. You can run the git remote command to see the name of your configured remotes.

Here is how to specify the remote to use for Source Link. In this example, the remote is named github:

<PropertyGroup>
  <GitRepositoryRemoteName>github</GitRepositoryRemoteName>
</PropertyGroup>
sam-wheat commented 4 years ago

@0xced I want to make the project build before I check in.

0xced commented 4 years ago

You can push them on another branch if you don't want to commit your Source Link experiments on the master branch.

I just had the same warning as you (Source control information is not available - the generated source link is empty) on another project and it was indeed because I had multiple git remotes and the GitHub remote wasn't the first one configured. I solved it by editing the .git/config file manually by moving the github remote above all other remotes.

sam-wheat commented 4 years ago

Ok, against my better judgement I edited the config file in .git:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[submodule]
    active = .
[remote "github"]
    url = https://github.com/leaderanalytics/Caching.git
    fetch = +refs/heads/*:refs/remotes/github/*
[remote "origin"]
    url = https://leaderanalytics.visualstudio.com/_git/LeaderAnalytics.Caching
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

I pushed changes to a branch named sourcelink_test

The only difference I can see is than when I build the error message appears in visual studio:

Error NU5017 Cannot create a package that has no dependencies nor content. LeaderAnalytics.Caching C:\Program Files\dotnet\sdk\3.1.300\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets 198

0xced commented 4 years ago

I opened https://github.com/leaderanalytics/Caching/pull/2 that should fix source link. I removed:

With commit e0549b023265c15771c541b933ef6cefbaafa4c4 I was able to generate a NuGet package with the following command: dotnet build -c Release /p:ContinuousIntegrationBuild=true LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj. This package passes NuGet Package Explorer validation.

leaderanalytics commented 4 years ago

Well, it builds so I think we made progress, thanks Cédric. I still get a warning on the nuget package generation:

C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching>dotnet build -c Release /p:ContinuousIntegrationBuild=true
Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj (in 2.5 sec).
C:\Users\sam\.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : Source control information is not available - the generated source link is empty. [C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj]
  LeaderAnalytics.Caching -> C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\bin\Release\netcoreapp3.1\LeaderAnalytics.Caching.dll
  Successfully created package 'C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\bin\Release\LeaderAnalytics.Caching.0.0.30.nupkg'.

Build succeeded.

C:\Users\sam\.nuget\packages\microsoft.sourcelink.common\1.0.0\build\Microsoft.SourceLink.Common.targets(52,5): warning : Source control information is not available - the generated source link is empty. [C:\Git\LeaderAnalytics.Caching\LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj]
    1 Warning(s)
    0 Error(s)

Time Elapsed 00:00:05.41

Also Nuget Package Explorer gives me a warning on Source Link and Deterministic.

This is unmodified .csproj after merging your PR:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Version>0.0.30</Version>
    <Authors>Leader Analytics</Authors>
    <Company>Leader Analytics</Company>
    <Product />
    <Description>Cache and MultiIndexCache</Description>
    <FileVersion>0.0.30.0</FileVersion>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageProjectUrl>https://github.com/leaderanalytics/Caching</PackageProjectUrl>
    <PackageLicenseUrl></PackageLicenseUrl>
    <RepositoryUrl>https://github.com/leaderanalytics/Caching</RepositoryUrl>
    <RepositoryType>git</RepositoryType>
    <Copyright>2020 Leader Analytics</Copyright>
    <PackageReleaseNotes>See Repository</PackageReleaseNotes>
    <PackageTags>cache multi key keys multi index indexes</PackageTags>
  </PropertyGroup>

  <PropertyGroup>

    <!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
    <PublishRepositoryUrl>true</PublishRepositoryUrl>

    <!-- Optional: Embed source files that are not tracked by the source control manager in the PDB -->
    <EmbedUntrackedSources>true</EmbedUntrackedSources>

    <PackageLicenseFile>LICENSE</PackageLicenseFile>
    <DebugType>embedded</DebugType>
  </PropertyGroup>  

  <ItemGroup>
    <!-- Add PackageReference specific for your source control provider (see below) -->
    <!-- https://github.com/dotnet/sourcelink/blob/master/README.md -->
    <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
  </ItemGroup>
  <ItemGroup>
    <None Include="..\LICENSE">
      <Pack>True</Pack>
      <PackagePath></PackagePath>
    </None>
  </ItemGroup>

</Project>
leaderanalytics commented 4 years ago

I tried to use the nuget in a host app despite the warning. Here are steps to reproduce and results:

Renamed directory containing source for LeaderAnalytics.Caching so VS would not use that as source

Copied LeaderAnalytics.Caching....nuget to c:\nugets (this is also a package source in Visual Studio)

Cleared nuget cache in Visual Studio

Created new console app, Added LeaderAnalytics.Caching nuget package from c:\nugets source

Verified timestamp on .dll to make sure I was not using a cached version

Ran the project and tried to step into code. Visual Studio put up a file dialog box asking for the location of the source code on my local disk..

Host app:

using System;
using LeaderAnalytics.Caching;

namespace CachingSourceLinkTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Cache<int> cache = new Cache<int>();
            cache.Set("one", 1);
        }
    }
}
0xced commented 4 years ago

It looks like it's still the issue about the git remote. You can try this, explicitly specifying the git remote name: dotnet build -c Release /p:ContinuousIntegrationBuild=true /p:GitRepositoryRemoteName=github LeaderAnalytics.Caching\LeaderAnalytics.Caching.csproj

leaderanalytics commented 4 years ago

Yep that was it. It is working now. Thanks for all your help. A couple questions to wrap it up:

Here it says "Including PDBs in the .nupkg is generally no longer recommended". Is the configuration you have given me temporary until the .snupkg can be generated on its own?

Will I always need to specify the repo url on the build command line?

Edit: for the record when I first posted this question my local git repo only had one remote: origin. I don't know how you concluded that there were two remotes involved but in fact I had copied the github repo to a new repo on Azure Devops. I added the second repo to my local because it was something I was needing to do anyway. The tip about specifying the repo on the build command line works but is probably unrelated to my original issue. I just want to mention that in case someone else has a similar problem.