sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Add ArtifactsPath to SourceRoot for deterministic builds outside repo

Open Copilot opened this issue 1 month ago • 1 comments

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: Add ArtifactsPath to SourceRoot items when defined, handling both trailing and non-trailing slash variants
  • ArtifactsOutputPathTests.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 ArtifactsPath to a location outside a repo root, generated files in the obj dir (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, the Build.ArtifactStagingDirectory location 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.targets solves 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.

Copilot avatar Dec 09 '25 21:12 Copilot

@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.

marcpopMSFT avatar Dec 13 '25 00:12 marcpopMSFT