Skip to content

Shell environment causes failures invoking gh CLI in scripts involving WSL and Git Bash #972

@tclem

Description

@tclem

Additional context

See internal Slack thread for in-depth reproduction and agent debug logs:

  • Scripts can fail with exit code 2 (error/unknown state) due to gh missing or environment mismatch
  • gh works in bash script only if PATH/environment and shell match (Git Bash OK, WSL fails by default)
  • Release note (recent gh version): https://github.com/github/github-app/pull/6781
  • Frequent breakage in agent merge flows
  • Subtle bugs around shell environment detection, bare vs. .exe invocation, path style (Windows vs. Linux), and auth context
  • Not necessarily related to a single release – outstanding WSL issues

What happened?

The agent scripts or workflows invoking the gh CLI (gh binary) fail in mixed shell environments, such as when using WSL bash (bash.exe) versus Git Bash. Key factors identified:

  • If called from a WSL bash, only a Windows gh.exe is present. Bare gh fails with 'command not found'.
  • Script expects to run in Git Bash (where gh resolves to the Windows CLI and handles C:/ paths).
  • Bare bash is WSL (C:\WINDOWS\system32\bash.exe), not Git Bash; script relies on the wrong shell in some paths.
  • Environment/path confusion causes gh calls to fail (exit code 2, error/unknown state); review steps show: authentication state confusion, bash environment varying, incorrect PATH resolution, and some C:/... paths passed which are not compatible outside Git Bash.
  • When re-run with explicit Windows-path gh.exe in PATH and under Git Bash, the script works.

This is causing frequent prompt/agent failures and merge failures, especially in environments with WSL and mixed shells. The errors are subtle and can depend on path handling and shell invocation details.

Expected behavior

gh CLI invocations in shell scripts should work regardless of whether the script is running inside WSL, Git Bash, or Windows bash as long as the CLI is on PATH, and paths and environment are resolved consistently. Ideally, detecting the host shell and adjusting the command/path is enough to avoid all failures.

Installation context

Reproducible in Windows environments running WSL, bash and Git Bash.

Steps to reproduce

  1. On a Windows machine with both Git Bash and WSL installed, in WSL bash, run a workflow or script that internally executes bare gh CLI commands and expects GitHub auth.
  2. Note that 'gh' is not found, or certain path arguments (e.g., --state-dir with C:/... values) do not resolve as in Git Bash.
  3. Review that invoking gh.exe directly via full path, or running the script under Git Bash, succeeds (with working interop and auth).
  4. Observe that when run as default, the script fails before gh can print the authorization line.

Short summary

Shell environment causes failures invoking gh CLI in scripts involving WSL and Git Bash

Affected version or release

Latest (as of the gh 2.69.0 release, see https://github.com/github/github-app/pull/6781)

View original Slack conversation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions