Skip to content

fix: add PATH-based Unix shell detection for stripped environments#3372

Open
Zireael wants to merge 1 commit intocode-yeongyu:devfrom
Zireael:fix/git-bash-shell-detection-edge-cases
Open

fix: add PATH-based Unix shell detection for stripped environments#3372
Zireael wants to merge 1 commit intocode-yeongyu:devfrom
Zireael:fix/git-bash-shell-detection-edge-cases

Conversation

@Zireael
Copy link
Copy Markdown

@Zireael Zireael commented Apr 12, 2026

Summary

Adds a third detection layer for Unix shells on Windows that catches cases where all shell-related environment variables have been stripped by the parent process.

Problem

PR #3370 fixed the primary issue where PSModulePath (always set on Windows) was checked before Unix shell indicators. However, there's an edge case where:

  1. A parent process spawns bash with a clean/minimal environment
  2. All shell indicators (SHELL, BASH_VERSION, MSYSTEM, WSL_DISTRO_NAME) are stripped
  3. Only PSModulePath and process.platform remain

In this case, detectShellType() would still return "powershell".

Solution

Added detectUnixPathInPATH() function that checks PATH for Unix shell indicators:

function detectUnixPathInPATH(): boolean {
  const path = (process.env.PATH || process.env.Path || "").toLowerCase()
  if (path.includes("\usr\bin") || path.includes("/usr/bin")) return true
  if (path.includes("\msys64\bin") || path.includes("/msys64/bin")) return true
  if (path.includes("\cygwin\bin") || path.includes("/cygwin/bin")) return true
  if (path.includes("\git\usr\bin") || path.includes("/git/usr/bin")) return true
  return false
}

This is scoped to process.platform === "win32" and runs as layer 3 (after env vars, before PSModulePath).

Detection Priority (updated)

  1. SHELL env var → Unix shell (explicit)
  2. Windows indicators → BASH_VERSION, MSYSTEM, WSL_DISTRO_NAME
  3. NEW: PATH fallback → detectUnixPathInPATH()
  4. PSModulePath → PowerShell
  5. Platform fallback → cmd/unix

Verification

  • Analyzed all 54 PATH entries on development machine
  • No false positives: generic /bin dirs (.cargo/bin, user directories) correctly ignored
  • Only actual Unix shell paths detected: MSYS2, Git Bash, Cygwin

Testing

PR #3370 introduced regression tests. This edge case fix adds:

  1. detectUnixPathInPATH() unit tests with mocked PATH
  2. Integration test for stripped environment scenario

Closes #3366


Summary by cubic

Adds a PATH-based fallback to correctly detect Unix shells on Windows when env vars are stripped, preventing false PowerShell detection. Improves detectShellType() behavior for Git Bash, MSYS2, and Cygwin.

  • Bug Fixes
    • Added detectUnixPathInPATH() to scan PATH for /usr/bin, /msys64/bin, /cygwin/bin, and /git/usr/bin on win32.
    • Updated detection order: SHELLBASH_VERSION/MSYSTEM/WSL_DISTRO_NAME → PATH fallback → PSModulePath → platform default.
    • Added unit tests for PATH cases and an integration test for stripped environments.

Written for commit 9a7b567. Summary will update on new commits.

When environment variables are completely stripped (e.g., parent process spawns bash with clean env), detect Unix shells by checking PATH for MSYS2/Cygwin/Git Bash indicators.

This is a belt-and-suspenders fallback layer (layer 3) that runs after:
1. SHELL env var (primary)
2. BASH_VERSION, MSYSTEM, WSL_DISTRO_NAME (Windows indicators)

Indicators checked:
- \usr\bin or /usr/bin (MSYS2, Git Bash)
- \msys64\bin or /msys64/bin (MSYS2)
- \cygwin\bin or /cygwin/bin (Cygwin)
- \git\usr\bin or /git/usr/bin (Git Bash)

Fixes edge case where detectShellType() would incorrectly return
'powershell' when all shell env vars are stripped by parent process.
@Zireael
Copy link
Copy Markdown
Author

Zireael commented Apr 12, 2026

I have read the CLA Document and I hereby sign the CLA

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: High risk of regression on Windows: checking PATH for Unix directories may misdetect the current shell as 'unix' even when running in PowerShell/CMD if Git or MSYS2 is installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: non-interactive-env hook generates PowerShell $env: syntax on Git Bash (Windows)

1 participant