Repository navigation
Running Flutter CLI in Git Hooks within a Submodule Corrupts Dart SDK #165390
Description
Executing Flutter CLI commands within Git hooks inside a submodule repository can result in the Dart SDK being corrupted. This issue makes all Flutter/Dart commands unusable, requiring a full reinstallation of the Flutter SDK to recover.
Steps to Reproduce
-
Clone a minimal reproduction repository that contains a submodule where the Flutter project resides:
git clone [email protected]:Xm798/example-project --recurse-submodules
-
Create a Git pre-commit hook inside the submodule at
.git/modules/submodules/example-submodule/hooks/pre-commitand add any Flutter-related commands, such as:flutter doctor dart format . -
In the submodule directory (submodules/example-submodule), commit any change to trigger the hook:
git commit -m "Trigger pre-commit hook" -
Observe the following error:
Downloading Darwin arm64 Dart SDK from Flutter engine 780e976079f7c9e9cc1f106566bc155e4fbf0297... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 258 100 258 0 0 692 0 --:--:-- --:--:-- --:--:-- 693 [/Users/Cyrus/opt/flutter/bin/cache/dart-sdk-darwin-arm64.zip] End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. unzip: cannot find zipfile directory in one of /Users/Cyrus/opt/flutter/bin/cache/dart-sdk-darwin-arm64.zip or /Users/Cyrus/opt/flutter/bin/cache/dart-sdk-darwin-arm64.zip.zip, and cannot find /Users/Cyrus/opt/flutter/bin/cache/dart-sdk-darwin-arm64.zip.ZIP, period. It appears that the downloaded file is corrupt; please try again. If this problem persists, please report the problem at: https://github.com/flutter/flutter/issues/new?template=1_activation.yml -
After this failure, running any Flutter or Dart commands results in:
$flutter --version /Users/Cyrus/opt/flutter/bin/internal/shared.sh: line 268: /Users/Cyrus/opt/flutter/bin/cache/dart-sdk/bin/dart: No such file or directory -
Checking the Flutter cache directory reveals that the Dart SDK has been entirely removed:
ls -lah /Users/Cyrus/opt/flutter/bin/cache/dart-sdk/ drwxr-xr-x@ 2 Cyrus staff 64B 3 18 20:18 . drwxr-xr-x@ 31 Cyrus staff 992B 3 18 20:18 ..
Actual results
The issue originates from the script bin/internal/update_engine_version.sh, which attempts to determine the Flutter branch and engine version using:
BRANCH=$(git -C "$FLUTTER_ROOT" rev-parse --abbrev-ref HEAD)
ENGINE_VERSION=$(git -C "$FLUTTER_ROOT" merge-base HEAD origin/master)However, when running inside a Git hook in a submodule, git sets the environment variable GIT_DIR to the submodule’s repository. This causes -C "$FLUTTER_ROOT" to be ignored, leading to incorrect values being retrieved. For example, in the error log above, the commit hash 780e976079f7c9e9cc1f106566bc155e4fbf0297 actually belongs to the latest commit in the submodule:
commit 780e976079f7c9e9cc1f106566bc155e4fbf0297 (HEAD -> master, origin/master, origin/HEAD)
Author: Xm798 <[email protected]>
Date: Tue Mar 18 19:57:44 2025 +0800
Additionally, bin/internal/update_dart_sdk.sh deletes the old Dart SDK before successfully downloading a new one, leading to a completely broken development environment if the download fails.
This issue forces users to completely reinstall Flutter SDK to restore functionality, significantly disrupting development workflows.
Logs
No response
Flutter Doctor output
Doctor output
/Users/Cyrus/opt/flutter/bin/internal/shared.sh: line268: /Users/Cyrus/opt/flutter/bin/cache/dart-sdk/bin/dart: No such file or directory
Activity
darshankawar commentedon Mar 19, 2025
@Xm798
Please check if this issue resembles your case or not.
Xm798 commentedon Mar 19, 2025
@darshankawar
Thank you for your response. I have reviewed the issue you mentioned, but it does not seem related to my case. This issue is specifically caused by the behavior of
bin/internal/update_engine_version.shwhen executed inside a Git submodule, and not related to Xcode but rather to how Flutter’s update scripts interact with Git in a submodule environment.hyiso commentedon Mar 19, 2025
@Xm798
You can check whether #140688 (comment) can help
Xm798 commentedon Mar 19, 2025
Yeah, that comment does help as a temporary workaround. But there’s one thing to keep in mind—issue #140688 was just about failing to get the Flutter version, while the latest script actually breaks the Dart SDK, making all Flutter CLI commands unusable. This is a much bigger hassle for developers since it forces a full Flutter SDK reinstall.
I really think Flutter needs a more reliable way to check the version and update the Dart SDK, so that even if something goes wrong, it doesn’t completely mess up the dev environment.
@hyiso
darshankawar commentedon Mar 20, 2025
Keeping the issue open for team's tracking.
5 remaining items
Unset GIT_DIR and other variables before updating
Unset GIT_DIR and other variables before updating
jtmcdole commentedon Mar 24, 2025
You could also just delete the "bin/cache" folder - the scripts (ps1 & sh) will download the contents again.
Unset GIT_DIR and other variables before updating (#165818)
Unset GIT_DIR and other variables before updating (flutter#165818)
Unset GIT_DIR and other variables before updating (flutter#165818)
github-actions commentedon Apr 8, 2025
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.jtmcdole commentedon Apr 14, 2025
Landed in stable 3.29.3
Unset GIT_DIR and other variables before updating (flutter#165818)
Unset GIT_DIR and other variables before updating (flutter#165818)