Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<DefaultItemExcludes>$(DefaultItemExcludes);bin/**;obj/**</DefaultItemExcludes>
</PropertyGroup>

<!-- Add ArtifactsPath to SourceRoot for deterministic builds when ArtifactsPath is outside the repo -->
<ItemGroup Condition="'$(ArtifactsPath)' != ''">
<SourceRoot Condition="!HasTrailingSlash('$(ArtifactsPath)')" Include="$(ArtifactsPath)\" />
<SourceRoot Condition="HasTrailingSlash('$(ArtifactsPath)')" Include="$(ArtifactsPath)" />
</ItemGroup>

<!--
Append $(TargetFramework) directory to output and intermediate paths to prevent bin clashes between
targets.
Expand Down
55 changes: 55 additions & 0 deletions test/Microsoft.NET.Build.Tests/ArtifactsOutputPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,61 @@ void LocateAndRunApp(DirectoryInfo root)
.Pass();
}
}

[Fact]
public void ArtifactsPathIsAddedToSourceRoot()
{
var testProject = new TestProject()
{
Name = "SourceRootTest",
IsExe = true,
TargetFrameworks = ToolsetInfo.CurrentTargetFramework
};

var testAsset = _testAssetsManager.CreateTestProject(testProject);

// Create a temporary directory outside of the repo to use as ArtifactsPath
var tempArtifactsPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempArtifactsPath);

try
{
// Set ArtifactsPath to a location outside the repo
File.WriteAllText(Path.Combine(testAsset.Path, "Directory.Build.props"), $"""
<Project>
<PropertyGroup>
<ArtifactsPath>{tempArtifactsPath}</ArtifactsPath>
</PropertyGroup>
</Project>
""");

// Build the project
var buildResult = new BuildCommand(testAsset)
.Execute();

buildResult.Should().Pass();

// Verify that the SourceRoot was added by checking that the build succeeded
// and that the ArtifactsPath was used. The output path should be under ArtifactsPath.
var outputDirectory = Path.Combine(tempArtifactsPath, "bin", testProject.Name, "debug");
new DirectoryInfo(outputDirectory)
.Should()
.Exist();

// Verify that the dll was created
new FileInfo(Path.Combine(outputDirectory, testProject.Name + ".dll"))
.Should()
.Exist();
}
finally
{
// Clean up the temporary directory
if (Directory.Exists(tempArtifactsPath))
{
Directory.Delete(tempArtifactsPath, recursive: true);
}
}
}
}

namespace ArtifactsTestExtensions
Expand Down
Loading