Skip to content

The agent that grows with you

fix(desktop): find UGit's bundled git.exe in the Windows resolver (#61494)

Hermes Agenta5e95fb27edc
diff --git a/apps/desktop/electron/git-binary-candidates.test.ts b/apps/desktop/electron/git-binary-candidates.test.ts
new file mode 100644
index 00000000000..351f667bf01
--- /dev/null
+++ b/apps/desktop/electron/git-binary-candidates.test.ts
@@ -0,0 +1,108 @@
+import assert from 'node:assert/strict'
+import path from 'node:path'
+import { describe, test } from 'vitest'
+
+import { ugitGitBinaries, windowsGitCandidates, type GitCandidateFs } from './git-binary-candidates'
+
+const LAD = path.join('C:', 'Users', 'suceru', 'AppData', 'Local')
+
+function fakeFs(dirs: Record<string, string[]>, files: string[]): GitCandidateFs {
+ return {
+ existsSync: candidate => files.includes(candidate),
+ readdirSync: dir => {
+ const entries = dirs[dir]
+
+ if (!entries) {
+ throw Object.assign(new Error(`ENOENT: no such file or directory, scandir '${dir}'`), { code: 'ENOENT' })
+ }
+
+ return [...entries]
+ }
+ }
+}
+
+const ugitGitExe = (version: string): string =>
+ path.join(LAD, 'UGit', `app-${version}`, 'resources', 'app', 'git', 'cmd', 'git.exe')
+
+describe('ugitGitBinaries (#61494)', () => {
+ test('enumerates the UGit app-* glob and returns each bundled git.exe, newest first', () => {
+ const fs = fakeFs(
+ { [path.join(LAD, 'UGit')]: ['app-5.50.1', 'app-5.51.0', 'Update.exe', 'tools'] },
+ [ugitGitExe('5.50.1'), ugitGitExe('5.51.0')]
+ )
+
+ assert.deepEqual(ugitGitBinaries(LAD, fs), [ugitGitExe('5.51.0'), ugitGitExe('5.50.1')])
+ })
+
+ test('sorts app dirs by version, not lexically (app-10.0.0 beats app-9.0.0)', () => {
+ const fs = fakeFs(
+ { [path.join(LAD, 'UGit')]: ['app-9.0.0', 'app-10.0.0'] },
+ [ugitGitExe('9.0.0'), ugitGitExe('10.0.0')]
+ )
+
+ assert.deepEqual(ugitGitBinaries(LAD, fs), [ugitGitExe('10.0.0'), ugitGitExe('9.0.0')])
+ })
+
+ test('returns [] when UGit is not installed (a missing dir must not throw)', () => {
+ assert.deepEqual(ugitGitBinaries(LAD, fakeFs({}, [])), [])
+ })
+
+ test('skips app-* dirs whose bundled git.exe is missing', () => {
+ const fs = fakeFs({ [path.join(LAD, 'UGit')]: ['app-5.50.1', 'app-6.0.0'] }, [])
+
+ assert.deepEqual(ugitGitBinaries(LAD, fs), [])
+ })
+})
+
+describe('windowsGitCandidates (#61494)', () => {
+ const env = {
+ localAppData: LAD,
+ programFiles: path.join('C:', 'Program Files'),
+ programFilesX86: path.join('C:', 'Program Files (x86)')
+ }
+
+ test('includes the UGit candidate and the resolver selection finds it when nothing earlier exists', () => {
+ // The reported machine (#61494): no hermes portable git, no Program Files
+ // Git, nothing on PATH — only the UGit-bundled copy exists. An Electron
+ // process launched from Explorer inherits the login-time environment
+ // block, which lacks the PATH entry the UGit installer added later, so
+ // the fixed candidate list must find it on disk.
+ const ugitGit = ugitGitExe('5.50.1')
+ const fs = fakeFs({ [path.join(LAD, 'UGit')]: ['app-5.50.1'] }, [ugitGit])
+
+ const candidates = windowsGitCandidates(env, fs)
+
+ assert.ok(candidates.includes(ugitGit))
+ // Ordered after the hermes-bundled portable git (preferred), before the
+ // system-wide defaults.
+ assert.ok(
+ candidates.indexOf(ugitGit) > candidates.indexOf(path.join(env.localAppData, 'hermes', 'git', 'cmd', 'git.exe'))
+ )
+ assert.ok(candidates.indexOf(ugitGit) < candidates.indexOf(path.join(env.programFiles, 'Git', 'cmd', 'git.exe')))
+
+ // resolveGitBinary's selection rule: the first existing candidate wins.
+ assert.equal(candidates.find(fs.existsSync), ugitGit)
+ })
+
+ test('still prefers the hermes-bundled portable git over UGit', () => {
+ const ugitGit = ugitGitExe('5.50.1')
+ const portable = path.join(env.localAppData, 'hermes', 'git', 'cmd', 'git.exe')
+ const fs = fakeFs({ [path.join(LAD, 'UGit')]: ['app-5.50.1'] }, [portable, ugitGit])
+
+ const candidates = windowsGitCandidates(env, fs)
+
+ assert.equal(candidates.find(fs.existsSync), portable)
+ })
+
+ test('keeps the historical candidates when UGit is absent', () => {
+ const candidates = windowsGitCandidates(env, fakeFs({}, []))
+
+ assert.deepEqual(candidates, [
+ path.join(env.localAppData, 'hermes', 'git', 'cmd', 'git.exe'),
+ path.join(env.localAppData, 'hermes', 'git', 'bin', 'git.exe'),
+ path.join(env.programFiles, 'Git', 'cmd', 'git.exe'),
+ path.join(env.programFilesX86, 'Git', 'cmd', 'git.exe'),
+ path.join(env.localAppData, 'Programs', 'Git', 'cmd', 'git.exe')
+ ])
+ })
+})
diff --git a/apps/desktop/electron/git-binary-candidates.ts b/apps/desktop/electron/git-binary-candidates.ts
new file mode 100644
index 00000000000..b7823621735
--- /dev/null
+++ b/apps/desktop/electron/git-binary-candidates.ts
@@ -0,0 +1,77 @@
+import path from 'node:path'
+
+/** The filesystem surface the candidate builders need — injectable for tests. */
+export interface GitCandidateFs {
+ existsSync: (candidate: string) => boolean
+ readdirSync: (dir: string) => string[]
+}
+
+/** Windows env slice resolveGitBinary reads (injectable for tests). */
+export interface WindowsGitEnv {
+ localAppData: string
+ programFiles: string
+ programFilesX86: string
+}
+
+/** The dir prefix a UGit (https://github.com/ugit/UGit) install lives under. */
+const UGIT_DIR = 'UGit'
+/** UGit's Electron app dirs are versioned (`app-5.50.1`, …); git ships inside. */
+const UGIT_APP_PREFIX = 'app-'
+/** Where the UGit-bundled Git-for-Windows puts git.exe inside an app dir. */
+const UGIT_GIT_REL = path.join('resources', 'app', 'git', 'cmd', 'git.exe')
+
+/**
+ * Every `%LOCALAPPDATA%\UGit\app-*\resources\app\git\cmd\git.exe` on disk,
+ * sorted newest-first by version (descending dir name).
+ *
+ * UGit bundles its own Git-for-Windows copy under a versioned app dir, so the
+ * exact path moves with every update — no fixed candidate can name it. The
+ * path IS usually on the user's PATH, but an Electron process launched from
+ * Explorer inherits the login-time environment block, which can lack entries
+ * added later by the UGit installer, so the update check's `git` spawn
+ * ENOENTs and "Check for updates" fails (#61494).
+ *
+ * Dirs whose bundled git.exe is missing are skipped (an app dir mid-update);
+ * a missing UGit dir returns [] — the glob is best-effort, never fatal.
+ */
+export function ugitGitBinaries(localAppData: string, fs: GitCandidateFs): string[] {
+ const ugitRoot = path.join(localAppData, UGIT_DIR)
+
+ let entries: string[]
+
+ try {
+ entries = fs.readdirSync(ugitRoot)
+ } catch {
+ return []
+ }
+
+ return entries
+ .filter(entry => entry.startsWith(UGIT_APP_PREFIX))
+ .sort((a, b) => b.localeCompare(a, undefined, { numeric: true }))
+ .map(entry => path.join(ugitRoot, entry, UGIT_GIT_REL))
+ .filter(fs.existsSync)
+}
+
+/**
+ * resolveGitBinary's fixed Windows candidate list, in preference order:
+ * the Hermes-bundled PortableGit first, then UGit's bundled copies, then the
+ * standard Git-for-Windows locations.
+ */
+export function windowsGitCandidates(env: WindowsGitEnv, fs: GitCandidateFs): string[] {
+ const candidates: string[] = []
+
+ if (env.localAppData) {
+ candidates.push(path.join(env.localAppData, 'hermes', 'git', 'cmd', 'git.exe'))
+ candidates.push(path.join(env.localAppData, 'hermes', 'git', 'bin', 'git.exe'))
+ candidates.push(...ugitGitBinaries(env.localAppData, fs))
+ }
+
+ candidates.push(path.join(env.programFiles, 'Git', 'cmd', 'git.exe'))
+ candidates.push(path.join(env.programFilesX86, 'Git', 'cmd', 'git.exe'))
+
+ if (env.localAppData) {
+ candidates.push(path.join(env.localAppData, 'Programs', 'Git', 'cmd', 'git.exe'))
+ }
+
+ return candidates
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 88c0f59cf8f..2c2bb542b07 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -256,6 +256,7 @@ import { downloadViaOauthSessionToFile, downloadViaTokenToFile } from './gateway
import { stopGatewayBeforeUpdate } from './gateway-stop-before-update'
import { resolveGatewayVersion } from './gateway-version'
import { probeGatewayWebSocket, spawnedBackendProbeOptions } from './gateway-ws-probe'
+import { windowsGitCandidates } from './git-binary-candidates'
import { registerGitIpc } from './git-ipc'
import { desktopBackendSpawnEnv, guestOnboardingEnabled, skipIntroEnabled } from './guest-onboarding'
import { readAndConsumeHandoffResult } from './handoff-result'
@@ -2996,8 +2997,9 @@ function makeDashboardReadyFile() {
// resolveGitBinary — locate git.exe on Windows. A fresh installer-driven
// install only has PortableGit under %LOCALAPPDATA%\hermes\git (never on
// PATH), so a bare spawn('git') ENOENTs and self-update checks fail with
-// "Couldn't check for updates". PortableGit first, then standard
-// Git-for-Windows locations, then PATH. Cached after first probe.
+// "Couldn't check for updates". PortableGit first, then UGit's bundled copy
+// (see ./git-binary-candidates), then standard Git-for-Windows locations,
+// then PATH. Cached after first probe.
let _gitBinaryCache = null
// A binary can exist on disk and still be unlaunchable — on macOS an
@@ -3058,19 +3060,18 @@ function resolveGitBinary() {
}
const localAppData = process.env.LOCALAPPDATA || ''
- const candidates = []
-
- if (localAppData) {
- candidates.push(path.join(localAppData, 'hermes', 'git', 'cmd', 'git.exe'))
- candidates.push(path.join(localAppData, 'hermes', 'git', 'bin', 'git.exe'))
- }
-
- candidates.push(path.join(process.env['ProgramFiles'] || 'C:\\Program Files', 'Git', 'cmd', 'git.exe'))
- candidates.push(path.join(process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)', 'Git', 'cmd', 'git.exe'))
-
- if (localAppData) {
- candidates.push(path.join(localAppData, 'Programs', 'Git', 'cmd', 'git.exe'))
- }
+ // Fixed candidates + the UGit-bundled glob (a UGit install moves with every
+ // app-* version, so it can only be found by enumerating the dir). UGit's
+ // git.exe is usually on PATH, but an Explorer-launched Electron inherits the
+ // login-time environment block and can miss it (#61494).
+ const candidates = windowsGitCandidates(
+ {
+ localAppData,
+ programFiles: process.env['ProgramFiles'] || 'C:\\Program Files',
+ programFilesX86: process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)'
+ },
+ { existsSync: fileExists, readdirSync: dir => fs.readdirSync(dir) }
+ )
_gitBinaryCache = candidates.find(fileExists) || findOnPath('git') || 'git'