Skip to content

Skip locked files in Clean task instead of prompting (build script) - #27781

Open
Guillermo López-Anglada (guillermooo) wants to merge 7 commits into
PowerShell:masterfrom
guillermooo:users/guillermo/build-script-clean
Open

Skip locked files in Clean task instead of prompting (build script)#27781
Guillermo López-Anglada (guillermooo) wants to merge 7 commits into
PowerShell:masterfrom
guillermooo:users/guillermo/build-script-clean

Conversation

@guillermooo

@guillermooo Guillermo López-Anglada (guillermooo) commented Aug 6, 2026

Copy link
Copy Markdown

PR Summary

Avoid git clean prompting during build when Visual Studio is open.

⚠️ Some commits need to be dropped before merging. Kept for convenience.

PR Context

Details

The Before data cleans less file system items.

The After data cleans more file system items.

Clean operation measurements follow based on

  • start-psbuild -Clean -PSModuleRestore -UseNuGetOrg, and
  • start-psbuild -Clean -PSModuleRestore -UseNuGetOrg -InformationAction continue (only After)

Before

git clean always prints to the screen.

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 2
Milliseconds      : 733
Ticks             : 27330567
TotalDays         : 3.16326006944444E-05
TotalHours        : 0.000759182416666667
TotalMinutes      : 0.045550945
TotalSeconds      : 2.7330567
TotalMilliseconds : 2733.0567
---
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 3
Milliseconds      : 309
Ticks             : 33093171
TotalDays         : 3.830228125E-05
TotalHours        : 0.00091925475
TotalMinutes      : 0.055155285
TotalSeconds      : 3.3093171
TotalMilliseconds : 3309.3171

After

No screen printing included because Measure-Command suppresses it?

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 2
Milliseconds      : 695
Ticks             : 26957542
TotalDays         : 3.12008587962963E-05
TotalHours        : 0.000748820611111111
TotalMinutes      : 0.0449292366666667
TotalSeconds      : 2.6957542
TotalMilliseconds : 2695.7542
---
# -InformationAction Continue
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 3
Milliseconds      : 99
Ticks             : 30998317
TotalDays         : 3.5877681712963E-05
TotalHours        : 0.000861064361111111
TotalMinutes      : 0.0516638616666667
TotalSeconds      : 3.0998317
TotalMilliseconds : 3099.8317

Notes

The script says it must support Windows PowerShell, but it doesn't run?

The old implementation calls git clean naively, and residual files/directories are left behind. The new implementation cleans exhaustively.

It might be possible to use git ls-file --cached --others --ignore --exclude-standard, but the returned files are not obviously the same, and directory removal might require additional work. However, there would be less dumb string parsing than using git clean --dry-run.

This is what happens now when a file cannot be cleaned:

image

PR Checklist

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@guillermooo
Guillermo López-Anglada (guillermooo) marked this pull request as ready for review August 6, 2026 22:48
Copilot AI lite review requested due to automatic review settings August 6, 2026 22:48
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@guillermooo Guillermo López-Anglada (guillermooo) changed the title Clean task in build script skips locked files Skip locked files in Clean task instead of prompting (build script) Aug 6, 2026
Before

--exclude .vs/PowerShell/v16/Server/sqlite3 doesn't exist.
--exclude src/Modules/nuget.config is not cleaned up.
--exclude nuget.config is not cleaned up.

You can verify that the nuget.config files are not cleaned up like so:

    git clean -fdX
    git ls-file -ico --standard-exclude | sls nuget.config

Therefore, the --exclude options have been removed because they are
no-ops.

Caveat: The sqlite3 file/folder may depend on the Visual Studio version.

If Visual Studio is open, depending on its state git clean may fail to
unlink files under the .vs\ directory and prompt interactively during
the build.

After

gi clean ... may return different sets of paths in subsequent calls:

   git clean --dry-run -dX # some paths
   git clean -fdX
   git clean --dry-run -dX # more paths
   git clean -fdX
   git clean --dry-run -dX # empty

...therefore, we use a while loop.

If a file cannot be deleted, we skip it (and its directory).

Caveat: Using the -Verbose switch results in very slow cleaning.

After this change, files locked by VS don't cause the build script to
prompt the user.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Updates the build clean step to avoid git clean prompting (notably when Visual Studio holds locks) by switching to a PowerShell-driven deletion flow based on git clean --dry-run.

Changes:

  • Added CleanLenient helper to iteratively collect paths from git clean --dry-run -dX and delete them via .NET APIs.
  • Replaced direct git clean -fdX ... --exclude ... call with Measure-Command { CleanLenient }.

Comment thread build.psm1
Comment thread build.psm1 Outdated
Comment thread build.psm1 Outdated
Comment thread build.psm1
Comment thread build.psm1 Outdated
The problem doesn't seem to be that `git clean --dry-run` keeps
returning paths, but rather that the following keeps returning paths:

- git clean -fdX
- git ls-files --ignore --standard-exclude --cached --others

But the second command isn't used in the script so far and this was only
noticed during debugging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

build.psm1 :: Start-PSBuild: Bad interaction between CLI and Visual Studio

2 participants