Skip to content

How to set Repository in custom nuspec #1392

Open

Description

@jherby2k

I'm using a custom NuSpec file with an SDK project like this, and can't get sourcelink to work correctly.

.csproj snippet:

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

  <PropertyGroup>
    <PublishRepositoryUrl>true</PublishRepositoryUrl>
    <NuspecFile>Pack.nuspec</NuspecFile>
    <NuspecProperties>$(NuspecProperties);Repository=$(RepositoryUrl)</NuspecProperties>
  </PropertyGroup>

</Project>

Pack.nuspec snippet:

<?xml version="1.0" encoding="utf-8"?>
<package>
  <metadata>
    <repository>$Repository$</repository>
  </metadata>
</package>

But the repository isn't set in the resulting .nupkg's .nuspec file. Any tips? Is this possible?

My other projects in the same solution (which don't need a custom Nuspec) have it set just fine.

Activity

  1. jherby2k commented on Mar 21, 2025

    @jherby2k
    Author

    I feel like this is closer, but still no dice:

    .csproj snippet:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <PublishRepositoryUrl>true</PublishRepositoryUrl>
        <NuspecFile>Pack.nuspec</NuspecFile>
        <NuspecProperties>$(NuspecProperties);RepositoryType=$(RepositoryType)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryUrl=$(RepositoryUrl)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryBranch=$(RepositoryBranch)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryCommit=$(RepositoryCommit)</NuspecProperties>
      </PropertyGroup>
    
    </Project>

    Pack.nuspec snippet:

    <?xml version="1.0" encoding="utf-8"?>
    <package>
      <metadata>
        <repository type="$RepositoryType$" url="$RepositoryUrl$" branch="$RepositoryBranch$" commit="$RepositoryCommit$" />
      </metadata>
    </package>

    it packs, but <repository> is still entirely missing

  2. tmat commented on Mar 21, 2025

    @tmat
    Member

    Is it missing </metadata> tag?

  3. tmat commented on Mar 21, 2025

    @tmat
    Member
  4. jherby2k commented on Mar 21, 2025

    @jherby2k
    Author

    Is it missing </metadata> tag?

    No sorry, snippet failure. Corrected.

  5. nkolev92 commented on Mar 21, 2025

    @nkolev92

    Based on the docs: https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#packing-using-a-nuspec-file
    Where is the nuspec relative to the project?
    Do you set NuspecBasePath?

    It looks like it should work. Tagging @jeffkl who is NuGet's hotseat.

  6. jherby2k commented on Mar 21, 2025

    @jherby2k
    Author

    The nuspec is in the same directory as the project file. It's packing very well, except for the lack of repository. Other substituted fields (author, projectURL etc) are filled in as expected using clunky NuspecProperties properties like in my second post.

    I hadn't set NuspecBasePath, no. everything else works fine except sourcelink. Just tried setting it and no difference whatsoever.

  7. jeffkl commented on Mar 21, 2025

    @jeffkl

    I'm not able to reproduce the issue

    ClassLibrary1.csproj

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net9.0</TargetFramework>
        <RepositoryType>repotype</RepositoryType>
        <RepositoryUrl>https://example</RepositoryUrl>
        <RepositoryBranch>main</RepositoryBranch>
        <RepositoryCommit>1</RepositoryCommit>
      </PropertyGroup>
      
      <PropertyGroup>
        <PublishRepositoryUrl>true</PublishRepositoryUrl>
        <NuspecFile>Pack.nuspec</NuspecFile>
        <NuspecProperties>PackageId=MyPackage</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);PackageVersion=2.0.0</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryType=$(RepositoryType)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryUrl=$(RepositoryUrl)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryBranch=$(RepositoryBranch)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryCommit=$(RepositoryCommit)</NuspecProperties>
      </PropertyGroup>
    </Project>

    Pack.nuspec

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
      <metadata>
        <id>$PackageId$</id>
        <version>$PackageVersion$</version>
        <authors>jeffkl</authors>
        <description>Description</description>
        <license type="expression">MIT</license>
        <repository type="$RepositoryType$" url="$RepositoryUrl$" branch="$RepositoryBranch$" commit="$RepositoryCommit$" />
      </metadata>
      <files>
        <file src="bin\Debug\net9.0\*.dll" target="lib\net9.0" />
      </files>
    </package>

    MyPackage.2.0.0.0.nupkg

    Image

    MyPackage.nuspec

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
      <metadata>
        <id>MyPackage</id>
        <version>2.0.0</version>
        <authors>jeffkl</authors>
        <owners></owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <license type="expression">MIT</license>
        <description>Description</description>
        <repository type="repotype" url="https://example" branch="main" commit="1" />
      </metadata>
    </package>
  8. jherby2k commented on Mar 21, 2025

    @jherby2k
    Author

    Sure that works if you hardcode the values, but I'm saying that source link doesn't fill em in if you don't.

    Sorry guess I didn't make that clear beyond posting it in the source link repo

  9. jeffkl commented on Mar 21, 2025

    @jeffkl

    Sorry guess I didn't make that clear beyond posting it in the source link repo

    🤦 sorry I missed that detail. Can you attach an MSBuild binary log of the repro?

    dotnet pack -bl
    

    Then attach the resulting msbuild.binlog.

  10. jherby2k commented on Mar 28, 2025

    @jherby2k
    Author

    sure, here you are.

    msbuild.binlog.txt

  11. jeffkl commented on Mar 28, 2025

    @jeffkl

    SouceLink is setting the properties in a .targets which is imported at the bottom of your project. When you're setting the NuspecProperties value in the project, properties like RepositoryBranch haven't been set yet.

    It might work if you set them in Directory.Build.targets which gets imported at the very bottom of a project, after SourceLink.

    To be really sure, you can just define a target to run which will set the properties during Pack rather than when the project is evaluated.

    <Target Name="SetCustomNuspecProperties" BeforeTargets="GenerateNuspec" DependsOnTargets="InitializeSourceControlInformation">
      <PropertyGroup>
        <NuspecProperties>$(NuspecProperties);RepositoryType=$(RepositoryType)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryUrl=$(RepositoryUrl)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryBranch=$(RepositoryBranch)</NuspecProperties>
        <NuspecProperties>$(NuspecProperties);RepositoryCommit=$(RepositoryCommit)</NuspecProperties>
      </PropertyGroup>
    </Target>

    The functionality is built in if you're not using a custom nuspec, as NuGet injects a similar target: https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Build.Tasks.Pack/NuGet.Build.Tasks.Pack.targets#L288

    It just doesn't set NuspecProperties, only input parameters to the Pack task.

  12. jherby2k commented on Mar 28, 2025

    @jherby2k
    Author

    Oh wow, adding that Target block to my .csproj, in conjunction with setting this in the .nuspec:

        <repository type="$RepositoryType$" url="$RepositoryUrl$" branch="$RepositoryBranch$" commit="$RepositoryCommit$" />
    

    works! Thank you.

    This should probably be documented, yeah? Can't imagine i'm the only one using a .nuspec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions