Add ArtifactsPath to SourceRoot for deterministic builds outside repo
When ArtifactsPath is set to a location outside the repository root, generated files in intermediate output directories (e.g., Project.Version.cs) are not properly mapped in PDBs, breaking build reproducibility.
Changes
Microsoft.NET.DefaultOutputPaths.targets: AddArtifactsPathtoSourceRootitems when defined, handling both trailing and non-trailing slash variantsArtifactsOutputPathTests.cs: Add test verifying artifacts path outside repo root produces correct output location
Context
Source mapping requires all source file locations to be declared in SourceRoot items. When artifacts live outside the repo, the intermediate output path becomes unmapped, causing full paths to leak into PDBs instead of deterministic mapped paths (_1/, _2/, etc.).
<!-- Added to Microsoft.NET.DefaultOutputPaths.targets -->
<ItemGroup Condition="'$(ArtifactsPath)' != ''">
<SourceRoot Condition="!HasTrailingSlash('$(ArtifactsPath)')" Include="$(ArtifactsPath)\" />
<SourceRoot Condition="HasTrailingSlash('$(ArtifactsPath)')" Include="$(ArtifactsPath)" />
</ItemGroup>
Original prompt
This section details on the original issue you should resolve
<issue_title>Custom ArtifactsPath outside repo does not add SourceRoot, making build not reproducible</issue_title> <issue_description><!-- Please keep in mind that the GitHub issue tracker is not intended as a general support forum, but for reporting non-security bugs and feature requests.
If you believe you have an issue that affects the SECURITY of the platform, please do NOT create an issue and instead email your issue details to [email protected]. Your report may be eligible for our bug bounty but ONLY if it is reported through email. For other types of questions, consider using StackOverflow.
-->
Describe the bug
When setting
ArtifactsPathto a location outside a repo root, generated files in theobjdir (e.g.Project.Version.cs) are not escaped properly and have the full path in the pdb instead of_1/,_2, etc.To Reproduce
In an AzDo pipeline, create a package with
dotnet pack -p:ArtifactsPath=$(Build.ArtifactStagingDirectory)and examine the result in NuGet Package Explorer. In AzDo, theBuild.ArtifactStagingDirectorylocation lives outside of the source location.Exceptions (if any)
Further technical details
Tested with .NET SDK 10 RC2
Workaround
Adding this into the
Directory.Build.targetssolves it:<SourceRoot Condition="'$(ArtifactsPath)' != '' and !HasTrailingSlash('$(ArtifactsPath)')" Include="$(ArtifactsPath)\" /> <SourceRoot Condition="'$(ArtifactsPath)' != '' and HasTrailingSlash('$(ArtifactsPath)')" Include="$(ArtifactsPath)" /> ```</issue_description> <agent_instructions>When we set ArtifactsPath, let's add it to SourceRoot. Can you add a test as well?</agent_instructions> ## Comments on the Issue (you are @copilot in this section) <comments> </comments>
- Fixes dotnet/sdk#51551
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
@tmat I believe you know source link well so may know if this is the right solution here. I just had copilot follow what the original issue stated but wanted someone to confirm.