Allow FileWritesShared to be tracked and cleaned with an opt-in flag - #12096
Merged
Conversation
This allows features like the .NET SDK's Artifacts Layout to correctly track and clean outputs that aren't under the project directory, but _are_ in project-isolated bubbles.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds an opt-in flag to allow the tracking and cleanup of FileWritesShareable items located outside the MSBuildProjectDirectory, enabling scenarios like the .NET SDK's Artifacts Layout.
- Introduces the TrackFileWritesShareableOutsideOfProjectDirectory property with a default value of false.
- Updates the FilesToTrackFromOutputDirectories item group to conditionally include FileWritesShareable based on the opt-in flag.
- Modifies the FindUnderPath tasks for the output and intermediate directories to use the updated FilesToTrackFromOutputDirectories.
YuliiaKovalova
approved these changes
Jul 1, 2025
JanProvaznik
approved these changes
Jul 9, 2025
baronfel
commented
Jul 18, 2025
| <FilesToTrackFromOutputDirectories Include="@(FileWritesShareable)" Condition=" $(TrackFileWritesShareableOutsideOfProjectDirectory) == true "/> | ||
| </ItemGroup> | ||
|
|
||
| <FindUnderPath Condition="!$(TrackFileWritesShareableOutsideOfProjectDirectory)" Path="$(MSBuildProjectDirectory)" Files="@(FileWritesShareable)" UpdateToAbsolutePaths="true"> |
Member
Author
There was a problem hiding this comment.
@rainersigwald I introduced a bug here. If we're not allowing cleaning FileWritesShared outside of the bubble, we should still clean them from inside the bubble. Instead of this Task's output going to FileWrites, it should go to FilesToTrackFromOutputDirectories.
<!-- Even if we don't allow cleaning filewrites from outside of the project bubble, we should still include FileWritesShareable that are inside the bubble -->
<FindUnderPath Condition="!$(TrackFileWritesShareableOutsideOfProjectDirectory)" Path="$(MSBuildProjectDirectory)" Files="@(FileWritesShareable)" UpdateToAbsolutePaths="true">
<Output TaskParameter="InPath" ItemName="FilesToTrackFromOutputDirectories"/>
</FindUnderPath>This is the cause of several of the test failures in dotnet/sdk#49740.
baronfel
added a commit
that referenced
this pull request
Jul 18, 2025
There are tests in the SDK that verify clean works in various scenarios:
```csharp
[Fact]
public void It_cleans_the_project_successfully_with_static_graph_and_isolation()
{
var (testAsset, outputDirectories) = BuildAppWithTransitiveDependenciesAndTransitiveCompileReference(new[] { "/graph", "/bl:build-{}.binlog" });
var cleanCommand = new DotnetCommand(
Log,
"msbuild",
Path.Combine(testAsset.TestRoot, "1", "1.csproj"),
"/t:clean",
"/graph",
"/bl:clean-{}.binlog");
cleanCommand
.Execute()
.Should()
.Pass();
foreach (var outputDirectory in outputDirectories)
{
outputDirectory.Value.GetFileSystemInfos()
.Should()
.BeEmpty();
}
}
```
This one and several others started failing when VMR codeflow with #12096 in it flowed to SDK.
The root of the problem is that in the case where we aren't allowing FileWritesShareable to be deleted from outside the project bubble, we should still add FileWritesShared from within the bubble to the list of files to be cleaned.
This was referenced Jul 18, 2025
YuliiaKovalova
pushed a commit
that referenced
this pull request
Jul 18, 2025
…#12192) There are tests in the SDK that verify clean works in various scenarios: ```csharp [Fact] public void It_cleans_the_project_successfully_with_static_graph_and_isolation() { var (testAsset, outputDirectories) = BuildAppWithTransitiveDependenciesAndTransitiveCompileReference(new[] { "/graph", "/bl:build-{}.binlog" }); var cleanCommand = new DotnetCommand( Log, "msbuild", Path.Combine(testAsset.TestRoot, "1", "1.csproj"), "/t:clean", "/graph", "/bl:clean-{}.binlog"); cleanCommand .Execute() .Should() .Pass(); foreach (var outputDirectory in outputDirectories) { outputDirectory.Value.GetFileSystemInfos() .Should() .BeEmpty(); } } ``` This one and several others started failing when VMR codeflow with #12096 in it flowed to SDK. The root of the problem is that in the case where we aren't allowing FileWritesShareable to be deleted from outside the project bubble, we should still add FileWritesShared from within the bubble to the list of files to be cleaned.
Member
Author
|
Reverted due to a performance regression: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This allows features like the .NET SDK's Artifacts Layout to correctly track and clean outputs that aren't under the project directory, but are in project-isolated bubbles.
Part of dotnet/sdk#49582.
Context
The core problem is that during the Publish, one of the things that happens is that we call the
_CleanGetCurrentAndPriorFileWritestarget fromMicrosoft.Common.CurrentVersion.targets. The purpose of this Target is to identify all of the files that were written and that are deleteable from the current build. These files are then tracked in a file that future builds use as an input to their own cleanup operations. Thedotnet build -c Releasecommand does read this file to delete files as necessary, but in this circumstance the file simply lacks all of the content that the self-contained build dumps into theartifacts/bin/myproj/releasedirectory.I'll inline the Target so we can discuss it:
msbuild/src/Tasks/Microsoft.Common.CurrentVersion.targets
Lines 5733 to 5806 in 25d1e4c
The specific part of this Target that is failing is the part that tries to locate any
FileWritesShareableMSBuild Items that live under theMSBuildProjectDirectory(meaning, the directory of the project file currently being built.FileWritesShareableare automatically tracked by the build, and for the publish do contain all of the runtime files that need to be cleaned. However, when theBaseOutputPathis directed into someplace that isn't underMSBuildProjectDirectory(as is the case with the artifacts layout), none of theseFileWritesShareablewill be found (because they are all in some location not underMSBuildProjectDirectory).Changes Made
Adds an opt-in flag that consumers like the .NET SDK can set so that potentially-shared files outside of the directory bubble are tracked for cleanup.
Testing
Manual testing by passing the property in and verifying FileListAbsolute.txt content.
Notes