Skip to content

fix(git): spawn git by its resolved path so macOS skips a PATH search per call - #869

Merged
jeff-r2026 merged 4 commits into
Tencent:mainfrom
SaulMoro:fix/spawn-git-by-resolved-path
Sep 28, 2026
Merged

jeff-r2026 merged 4 commits into
Tencent:mainfrom
SaulMoro:fix/spawn-git-by-resolved-path

Conversation

@SaulMoro

@SaulMoro SaulMoro commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

On macOS 26, looking up a bare-name git spawn costs a few milliseconds for every PATH entry ahead of git's directory. That is 30-67 ms per git call with an ordinary PATH, while git itself takes about 8 ms (#868). createGit now gives simple-git the absolute path of the git that lookup would pick:

 export function createGit(basePath?: string): SimpleGit {
   if (basePath) {
-    return simpleGit({ baseDir: basePath });
+    return simpleGit({ baseDir: basePath, binary: gitBinary() });
   }
-  return simpleGit();
+  return simpleGit({ binary: gitBinary() });
 }

gitBinary() (src/utils/git.ts) walks PATH the way a bare-name spawn does. It caches the result per PATH value and looks PATH up again when PATH changes, or when the cached git is gone or no longer executable (one access(2) per call):

for dir in PATH
  relative or empty dir          -> 'git'   (spawn resolves it against cwd)
  <dir>/git missing              -> next dir
  <dir>/git unreadable (ELOOP…)  -> 'git'
  not a file, or unsafe for simple-git -> 'git'
  `<dir>/git --version` spawn fails -> 'git'   (bare lookup would move past it)
  else                           -> <dir>/git
none found, or win32             -> 'git'

The one --version spawn per PATH value is what makes the absolute path safe. A bare-name spawn moves past an entry whose spawn fails, such as a script whose interpreter is gone (ENOENT) or a file without execute permission, and a spawn by path does not. So the absolute path is used only when spawning it works, which is exactly the file the bare lookup would run. Whenever that does not hold, the bare name is kept, with today's lookup and today's errors.

initRepo now goes through createGit too. simple-git is built only in these two places, and every pull, push, reports and learnings operation goes through them, 18 to 70 git calls each.

Left as they are: the direct spawnSync/execFile('git', …) sites (provider clone, git --version and user.name probes, mr-hint, code-collector). Each runs once or a few times per command, and their tests assert that the spawned command is git. The test fixtures' own simpleGit() calls are left too.

Closes #868

Evidence

First git on PATH does not start (review P1). Real CLI, isolated HOME, a local bare git team repo, PATH=<dir with #!/nonexistent/sh git>:/opt/homebrew/bin:/usr/bin:/bin:<node>:

  • Before (a4161ca): ✖ [user] Pull failed: Error: spawn /tmp/869-bench/broken/git ENOENT
  • After (bf39067): ✔ [user] Team repo: already up to date

In git-binary.test.ts: an earlier git with a missing interpreter plus real git later on PATH → gitBinary returns git and createGit(repo).init() succeeds. This fails at a4161ca and passes now. A resolved git removed mid-process → the next gitBinary returns the later one; one that loses execute permission → git (review P3). This fails at bf39067 and passes now.

Speed, macOS 26 (Darwin 25.5), Node 22.23.3, measured at a4161ca with the same fixture (teamai pull already up to date). Seconds, 3 runs each. Before is origin/main (5fb316c7):

PATH git's entry pull before pull after push --dry-run before push --dry-run after
4 entries 2 0.69 0.67 0.73 0.44 0.45 0.44 0.42 0.44 0.42 0.30 0.30 0.30
42 entries (a shell) 11 1.37 1.38 1.39 0.44 0.43 0.43 0.78 0.76 0.77 0.33 0.33 0.32
51 entries (the shape npx gives) 20 2.12 2.14 2.12 0.43 0.43 0.43 1.14 1.12 1.29 0.36 0.36 0.36
Real-git test file, alone under npx vitest run Tests Before After
git-kind-reports.test.ts 19 79-83 s 42 s
git-kind-learnings.test.ts 12 40 s 23 s
learnings-import-remnants.test.ts 32 139 s 74 s
the three together, --maxWorkers=4 63 217 s 89-90 s

macOS 27 (Darwin 27) does not show the lookup cost. #868's repro now gives 4.6 ms bare against 3.6 ms by path with 30 PATH entries, and 4.5 against 3.7 ms under npm exec (38 entries). So at bf39067, before and after match within noise on Node 22, 24 and 26: an up-to-date pull takes 0.6-0.9 s either way with a 54-entry PATH, and push --dry-run 0.15 s. The macOS 26 numbers above are from a4161ca. The --version probe adds one spawn per process (about 4 ms). Output is unchanged: ✔ [user] Team repo: already up to date, ℹ No new or modified resources to push.

Checks at the head commit: npx vitest run --maxWorkers=4: 337 files, 5256 passed, 1 skipped. npx tsc --noEmit is clean. npm run lint reports 0 warnings. The e2e files that drive real git through the built CLI pass, 62 tests: self-mode-worktrees-808, deleted-worktree-scope-810, import-mr-publish-823, learnings-remnants-823, learnings-sync-704-705, reports-writer-sync-561, members-legacy-roster, init-unattended. The POSIX-only gitBinary cases skip on win32, where there are no execute bits or shebangs (review P2).

Linux CI is not expected to change much: a PATH miss there is a failed execvp in the forked child, which is cheap.

Merge Danger

Door: two-way. No state or config is written. Reverting restores the bare-name spawn.

Blast Radius: every simple-git call. Every case where the resolved path might not be the file the bare lookup would run falls back to the bare name, so the failure left is a git that passes --version and then behaves differently from the bare-name one, which is not expected.

… per call

On macOS, a bare-name spawn of git costs a few milliseconds for every PATH
entry ahead of git's directory: 30-67 ms per call with a typical PATH, while
git itself takes about 8 ms. createGit now passes simple-git the absolute
path of the git that lookup would pick, resolved once per PATH value.

Refs Tencent#868
A stat error other than ENOENT/ENOTDIR (a symlink loop) now keeps the bare
name instead of moving on to a later git. Narrow the CHANGELOG to the
simple-git paths and correct two comments.

Refs Tencent#868
@jeff-r2026 jeff-r2026 self-assigned this Sep 28, 2026
@github-actions

Copy link
Copy Markdown

Findings

  • [P1 blocking] src/utils/git.ts:52 treats stat plus X_OK as proof that PATH lookup will select this candidate. For example, an executable git script whose shebang interpreter no longer exists passes these checks; a bare-name spawn continues to a later valid PATH entry after the first candidate returns ENOENT, while the new absolute spawn fails immediately. This turns a currently working CLI into one where every simple-git operation fails. Preserve the bare-name fallback unless the candidate’s spawn behavior can be matched. (nodejs.org)
  • [P2 non-blocking] src/__tests__/git-binary.test.ts:50 is not portable to Windows: Windows does not implement POSIX execute permission bits, so chmodSync(..., 0o644) will not make accessSync(..., X_OK) fail. The test therefore resolves the file instead of returning git; skip this POSIX-only case on Windows or make permission checking injectable.

The PR description includes sufficient real-CLI/e2e testing evidence for the runtime change.

A bare-name spawn moves past a PATH entry whose spawn fails, such as a
script whose interpreter is gone, while a spawn by path just fails.
gitBinary now spawns the candidate once (git --version) and keeps the
bare name unless it starts. POSIX-only gitBinary cases skip on win32.
@github-actions

Copy link
Copy Markdown

Findings

  • [P3 nit] src/utils/git.ts:38 caches the resolved executable solely by the PATH string. If that executable is removed or loses execute permission during a long-running command while PATH remains unchanged, later createGit() calls keep spawning the stale absolute path and fail, whereas bare-name lookup would continue to a later valid git. Consider revalidating the cached candidate before reuse.

Resolved

  • The earlier missing-interpreter issue is fixed by the spawn probe.
  • The POSIX permission test is now skipped on Windows.
  • The PR description includes sufficient real-CLI/e2e testing evidence.

…cutable

A bare-name spawn walks PATH on every call, so a git removed or chmod'ed
mid-process is skipped. gitBinary's cache kept the stale absolute path.
One access(2) per call now drops a cached path that is no longer
executable.
@github-actions

Copy link
Copy Markdown

Findings

  • [P3 nit] src/utils/git.ts:38 revalidates the cached path only with X_OK, not the original “can spawn” check. If the cached script’s interpreter disappears—or the path is replaced by an executable directory—during a long-running process, subsequent calls use the stale absolute path and fail, while bare-name lookup would skip it and find a later git.

Resolved

  • The earlier missing-interpreter and Windows permission-test findings are fixed.
  • The PR description includes sufficient real-CLI/e2e testing evidence.

@jeff-r2026
jeff-r2026 merged commit 53c096e into Tencent:main Sep 28, 2026
13 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
… per call (Tencent#869)

* fix(git): spawn git by its resolved path so macOS skips a PATH search per call

On macOS, a bare-name spawn of git costs a few milliseconds for every PATH
entry ahead of git's directory: 30-67 ms per call with a typical PATH, while
git itself takes about 8 ms. createGit now passes simple-git the absolute
path of the git that lookup would pick, resolved once per PATH value.

Refs Tencent#868

* fix(git): leave unreadable git candidates to the spawn's own lookup

A stat error other than ENOENT/ENOTDIR (a symlink loop) now keeps the bare
name instead of moving on to a later git. Narrow the CHANGELOG to the
simple-git paths and correct two comments.

Refs Tencent#868

* fix(git): keep the bare name when the first git on PATH does not start

A bare-name spawn moves past a PATH entry whose spawn fails, such as a
script whose interpreter is gone, while a spawn by path just fails.
gitBinary now spawns the candidate once (git --version) and keeps the
bare name unless it starts. POSIX-only gitBinary cases skip on win32.

* fix(git): look PATH up again when the resolved git is gone or not executable

A bare-name spawn walks PATH on every call, so a git removed or chmod'ed
mid-process is skipped. gitBinary's cache kept the stale absolute path.
One access(2) per call now drops a cached path that is no longer
executable.
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] git subprocesses are slow on macOS: bare-name spawn costs about 30-67 ms each via PATH lookup

2 participants