Skip to content

Running Flutter CLI in Git Hooks within a Submodule Corrupts Dart SDK #165390

@Xm798

Description

@Xm798

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

  1. Clone a minimal reproduction repository that contains a submodule where the Flutter project resides:

    git clone [email protected]:Xm798/example-project --recurse-submodules
  2. Create a Git pre-commit hook inside the submodule at .git/modules/submodules/example-submodule/hooks/pre-commit and add any Flutter-related commands, such as:

    flutter doctor
    dart format .
  3. In the submodule directory (submodules/example-submodule), commit any change to trigger the hook:

    git commit -m "Trigger pre-commit hook"
  4. 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
    
  5. 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
  6. 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

Metadata

Metadata

Assignees

Labels

r: fixedIssue is closed as already fixed in a newer versionteam-engineOwned by Engine teamtoolAffects the "flutter" command-line tool. See also t: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions