From bc4fc5ffb98478340b53c097d83245582b776940 Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Mon, 2 Dec 2024 13:45:14 -0500 Subject: [PATCH] [ Tool ] Fix "Error: Unable to find git in your PATH" when Command Processor `AutoRun` registry key is defined (#159424) cmd.exe will read from the AutoRun registry key at launch and execute the commands listed in the key. Since `FOR /F IN (command) ...` causes a new terminal instance to start, the AutoRun commands will be run again, possibly changing the current working directory. In this case, the `git rev-parse HEAD` check run in `shared.bat` could fail, which we were assuming meant `git` was not on the user's PATH. This change ensures that `git rev-parse HEAD` will always run in %FLUTTER_ROOT% and explicitly adds a separate check for `git` using `WHERE git` to more accurately determine if `git` is on the PATH. Fixes https://github.com/flutter/flutter/issues/159018 --------- Co-authored-by: Andrew Kolos --- bin/internal/shared.bat | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/internal/shared.bat b/bin/internal/shared.bat index 97eee5a9b3..504540b230 100644 --- a/bin/internal/shared.bat +++ b/bin/internal/shared.bat @@ -52,18 +52,24 @@ GOTO :after_subroutine CALL "%bootstrap_path%" ) - REM Check that git exists and get the revision - SET git_exists=false + REM Check that git exists and get the revision. + WHERE git >NUL 2>&1 + IF "%ERRORLEVEL%" NEQ "0" ( + REM Could not find git. Exit without /B to avoid retrying. + ECHO Error: Unable to find git in your PATH. && EXIT 1 + ) 2>NUL ( - PUSHD "%flutter_root%" - FOR /f %%r IN ('git rev-parse HEAD') DO ( - SET git_exists=true + REM 'FOR /f' spawns a new terminal instance to run the command. If an + REM 'AutoRun' command is defined in the user's registry, that command could + REM change the working directory, and then we wouldn't be in the directory + REM we expect to be in. To prevent this, we need to 'PUSHD %FLUTTER_ROOT%' + REM before getting the git revision. + REM + REM See https://github.com/flutter/flutter/issues/159018 + FOR /f %%r IN ('PUSHD %FLUTTER_ROOT% ^& $git rev-parse HEAD') DO ( SET revision=%%r ) - POPD ) - REM If git didn't execute we don't have git. Exit without /B to avoid retrying. - if %git_exists% == false echo Error: Unable to find git in your PATH. && EXIT 1 SET compilekey="%revision%:%FLUTTER_TOOL_ARGS%" REM Invalidate cache if: