Skip to content

flutter doctor reports wrong channel/version when run from git hooks due to inherited git environment variables #180421

Description

@luksladky

Steps to reproduce

  1. Have a Flutter project that is a git repository (with a non-GitHub remote, e.g., Bitbucket)
  2. Create a git pre-commit hook that runs Flutter commands:
    #!/bin/bash
    export PATH="$HOME/path/to/flutter/bin:$PATH"
    flutter doctor
    flutter test
  3. Stage some changes and run git commit

Expected results

flutter doctor should report the correct Flutter channel and version:

[✓] Flutter (Channel stable, 3.38.5, on macOS ...)

The Flutter installation should correctly identify its repository URL as https://github.com/flutter/flutter.git.

Actual results

flutter doctor reports an incorrect channel and version:

[!] Flutter (Channel [user-branch], 0.0.0-unknown, on macOS ...)
    ! Upstream repository https://[my-project-git-url] is not a standard remote.

Inspecting $FLUTTER_ROOT/bin/cache/flutter.version.json shows:

{
  "frameworkVersion": "0.0.0-unknown",
  "channel": "[user-branch]",
  "repositoryUrl": "https://[my-project-git-url]",
  ...
}

The repositoryUrl field contains the project's git remote URL instead of Flutter's GitHub repository URL.

Workaround: Unset git environment variables in the hook before calling Flutter:

unset GIT_DIR GIT_INDEX_FILE GIT_WORK_TREE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES

Or delete the corrupted cache file:

rm -f $FLUTTER_ROOT/bin/cache/flutter.version.json
flutter --version

Code sample

During git hook execution, git sets environment variables (GIT_DIR, GIT_INDEX_FILE, GIT_WORK_TREE, etc.) that point to the project repository. These variables affect all child git processes.

In packages/flutter_tools/lib/src/version.dart (lines 632-636):

_repositoryUrl = _runGit(
  'git ls-remote --get-url $remote',
  globals.processUtils,
  flutterRoot,
);

Despite passing flutterRoot as the working directory parameter, the git command respects the GIT_DIR environment variable, which overrides the working directory. This causes git ls-remote --get-url origin to return the project's remote URL instead of Flutter's, which then gets incorrectly cached.

Suggested Fix

The _runGit function (or callers) should explicitly unset git environment variables to ensure git commands operate on the Flutter SDK repository, not any parent repository context:

// Clear git environment variables that could point to a different repo
final environment = Map<String, String>.from(Platform.environment)
  ..remove('GIT_DIR')
  ..remove('GIT_INDEX_FILE')
  ..remove('GIT_WORK_TREE')
  ..remove('GIT_OBJECT_DIRECTORY')
  ..remove('GIT_ALTERNATE_OBJECT_DIRECTORIES');

Screenshots or Video

No response

Logs

Flutter Doctor output

[!] Flutter (Channel [user-branch], 0.0.0-unknown, on macOS 15.6.1 24G90 darwin-arm64, locale en-US)
! Flutter version 0.0.0-unknown on channel [user-branch] at /Users/user/Documents/flutter
Currently on an unknown channel. Run flutter channel to switch to an official channel.
Cannot resolve current version, possibly due to local changes.
Reinstall Flutter by following instructions at https://flutter.dev/setup.
! Upstream repository https://bitbucket.org/user/project.git is not a standard remote.
Set environment variable "FLUTTER_GIT_URL" to https://bitbucket.org/user/project.git to dismiss this error.
[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.3)
[✓] VS Code (version 1.105.1)
[✓] Connected device (2 available)
[✓] Network resources

Metadata

Metadata

Labels

Bot is counting down the days until it unassigns the issueP2Important issues not at the top of the work listt: flutter doctorProblem related to the "flutter doctor" toolteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions