Exclude PublishDir from default items to prevent recursive artifacts in file-based apps #51417
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.
Summary
Fixes an issue where publishing file-based apps multiple times would include previously published artifacts in subsequent publishes, causing file-in-use errors and recursive artifact directories.
Problem
When publishing file-based apps (e.g.,
dotnet publish app.cs), the default output directory./artifacts/<app name>is created next to the app file. However, this directory was not excluded from the app's default non-compile items. This is particularly problematic for apps usingMicrosoft.NET.Sdk.Web, which includes**/*.jsonfiles in its default items.Reproduction:
This resulted in:
./artifacts/app/artifacts/app/*.jsonSolution
Added
$(PublishDir)/**to theDefaultItemExcludesproperty inMicrosoft.NET.Sdk.DefaultItems.targets, conditioned onPublishDirbeing set. This follows the same pattern as existing exclusions forBaseOutputPathandBaseIntermediateOutputPath.The exclusion is placed alongside other output path-related exclusions and only applies when
PublishDiris defined, ensuring it doesn't affect projects where the property isn't set.Testing
It_excludes_items_in_publish_directorythat verifies JSON files inPublishDirare not included in None itemsGivenThereAreDefaultItemspassFixes #44721
Original prompt
Fixes #51391
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.