Fix nested cmd.exe shell when rerunning init.cmd (#12312)

## Fixes

No linked issue.

## PR Type

- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no API changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## Description

### Current Behavior

Rerunning `init.cmd` in an already initialized command window can
unexpectedly enter a nested `cmd.exe` shell, making initialization
appear to stop until the user types `exit`.

`DevCmd.cmd` records `/PreserveContext` using a marker file under
`%TEMP%`. A previous initialization points `%TEMP%` into `BuildOutput`,
which `git clean -xdf` can remove. If the directory is missing, marker
creation fails and `DevCmd.cmd` incorrectly starts `cmd /k`.

### New Behavior

Replace the marker file with an environment flag explicitly carried
across `endlocal`. `/PreserveContext` no longer depends on a writable,
existing temporary directory.

Calls from `init.cmd` return without opening a nested shell. Invoking
`DevCmd.cmd` without `/PreserveContext` retains its interactive-shell
behavior.

## Customer Impact

WinUI developer inner loop tooling only. Prevents interrupted
initialization after cleaning build output. No changes to WinUI runtime
behavior or public APIs.

## Regression Potential

- [x] Low risk — isolated change, limited scope
- [ ] Medium risk — touches shared components or public APIs
- [ ] High risk — architectural or breaking API change

Limited to argument-state handling in `DevCmd.cmd` and so only affects
the WinUI developer inner loop.

## How Has This Been Tested?

- [x] I have performed a self-review of my own code
- [ ] I have added tests to cover my changes
- [ ] Existing tests pass locally

Ran `init`, ran `git clean -xdf`, ran `init`, verified that the nested
`cmd.exe` shell no longer appears.

## Screenshots (if appropriate)

N/A; no visual changes.

Copilot-Session: fc793207-256a-4658-b725-bbc0e551b5ed
This commit is contained in:
Evelyn Wu authored and GitHub committed 2026-10-21 02:56:10 +05:30
1 parent 09cfc8ff3f
commit a5d85af03f
1 file changed
+5 -4
+5 -4
View File
@@ -39,16 +39,16 @@ set PrereleaseArg=
setlocal enableextensions enabledelayedexpansion
set _ARGS=
set _PreserveContext=0
rem We are targeting VS 2019 (version 16.x) or later
set VsVersion=16.0
if exist %temp%\WinUI.PreserveContext.marker del %temp%\WinUI.PreserveContext.marker
:ParseArgs
if "%1" EQU "" (
goto :DoneParsing
) else if /i "%1" EQU "/PreserveContext" (
echo. > %temp%\WinUI.PreserveContext.marker
set _PreserveContext=1
) else if /i "%1" EQU "/Prerelease" (
set PrereleaseArg=-prerelease
) else (
@@ -90,6 +90,7 @@ if exist %~dp0.buildtools (
)
endlocal & (
set "_PreserveContext=%_PreserveContext%"
echo "Initializing VS command prompt from %MSBuildInstallPath%\Common7\Tools\VsDevCmd.bat ..."
call "%MSBuildInstallPath%\Common7\Tools\VsDevCmd.bat" /no_logo %_ARGS%
)
@@ -99,11 +100,11 @@ call :SetEnviromentVariable VCToolsInstallDir "%VCToolsInstallDir%"
call :SetEnviromentVariable VCToolsRedistDir "%VCToolsRedistDir%"
call :SetEnviromentVariable ExtensionSdkDir "%ExtensionSdkDir%"
if not exist %temp%\WinUI.PreserveContext.marker (
if not "%_PreserveContext%"=="1" (
echo Starting cmd /k
cmd /k "cd /d %RepoRoot%"
)
if exist %temp%\WinUI.PreserveContext.marker (del %temp%\WinUI.PreserveContext.marker)
set "_PreserveContext="
exit /b 0