How to set Repository in custom nuspec #1392
Copy link
Copy link
Open
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
jherby2k commentedon Mar 21, 2025
I feel like this is closer, but still no dice:
.csproj snippet:
Pack.nuspec snippet:
it packs, but
<repository>is still entirely missingtmat commentedon Mar 21, 2025
Is it missing
</metadata>tag?tmat commentedon Mar 21, 2025
@nkolev92
jherby2k commentedon Mar 21, 2025
No sorry, snippet failure. Corrected.
nkolev92 commentedon Mar 21, 2025
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.
jherby2k commentedon Mar 21, 2025
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.
jeffkl commentedon Mar 21, 2025
I'm not able to reproduce the issue
ClassLibrary1.csproj
Pack.nuspec
MyPackage.2.0.0.0.nupkgMyPackage.nuspec
jherby2k commentedon Mar 21, 2025
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
jeffkl commentedon Mar 21, 2025
🤦 sorry I missed that detail. Can you attach an MSBuild binary log of the repro?
Then attach the resulting
msbuild.binlog.jherby2k commentedon Mar 28, 2025
sure, here you are.
msbuild.binlog.txt
jeffkl commentedon Mar 28, 2025
SouceLink is setting the properties in a
.targetswhich is imported at the bottom of your project. When you're setting theNuspecPropertiesvalue in the project, properties likeRepositoryBranchhaven't been set yet.It might work if you set them in
Directory.Build.targetswhich 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.
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.
jherby2k commentedon Mar 28, 2025
Oh wow, adding that Target block to my .csproj, in conjunction with setting this in the .nuspec:
works! Thank you.
This should probably be documented, yeah? Can't imagine i'm the only one using a .nuspec.