diff --git a/src/main/git/command-runner/git-exec-file.ts b/src/main/git/command-runner/git-exec-file.ts index b114c92bcce..f6359218d81 100644 --- a/src/main/git/command-runner/git-exec-file.ts +++ b/src/main/git/command-runner/git-exec-file.ts @@ -49,7 +49,8 @@ async function gitExecFileAsyncUnlocked( if (readEnvironmentReady) { await readEnvironmentReady } - let resolved = resolveGitCommand(args, options, false, options.captureWslLoginShellOutput) + // Buffered reads always fence login rc output; only streaming (gitStreamStdout) must stay unfenced. + let resolved = resolveGitCommand(args, options, false, true) const environmentReady = prepareWindowsHostGitEnvironment( resolved, options.env, @@ -57,12 +58,7 @@ async function gitExecFileAsyncUnlocked( ) const env = environmentReady ? await environmentReady : options.env const effectiveOptions = env === options.env ? options : { ...options, env } - resolved = resolveGitCommand( - args, - effectiveOptions, - false, - effectiveOptions.captureWslLoginShellOutput - ) + resolved = resolveGitCommand(args, effectiveOptions, false, true) const policy = effectiveOptions.useConfiguredSshCommandForNetwork || classifyGitCommand(args) === 'network' ? await buildNetworkSshPolicyEnv(effectiveOptions, args) @@ -114,7 +110,7 @@ async function gitExecFileAsyncUnlocked( : execFileCapture(command.binary, command.args, captureOptions) // Add a cause when a dead WSL distro returns empty stderr. return captured.catch((error: unknown) => { - throw annotateWslHostFailure(error, command) + throw withCapturedErrorStdout(annotateWslHostFailure(error, command), command) }) } const runCapturedCommand = async (): Promise<{ stdout: string; stderr: string }> => { @@ -131,12 +127,7 @@ async function gitExecFileAsyncUnlocked( ) { await terminationState.current const wasMissing = invalidateMissingDirectWslGit(error, resolved) - const fallback = resolveGitCommand( - args, - effectiveOptions, - true, - effectiveOptions.captureWslLoginShellOutput - ) + const fallback = resolveGitCommand(args, effectiveOptions, true, true) result = await capture(fallback) // Why: matching failures can be normal Git control flow; only a successful login retry proves the direct environment was insufficient. disableDirectWslGitAfterSuccessfulFallback(wasMissing, resolved) @@ -175,6 +166,16 @@ async function gitExecFileAsyncUnlocked( ) } +/** Callers parse stdout of non-zero exits (check-ignore exit 1), so strip the fence there too. */ +function withCapturedErrorStdout(error: unknown, command: ResolvedCommand): unknown { + if (command.captured && error && typeof error === 'object' && 'stdout' in error) { + if (typeof error.stdout === 'string') { + error.stdout = readCapturedGitString(error.stdout, command) + } + } + return error +} + export function gitExecFileAsync( args: string[], callerOptions: GitExecOptions diff --git a/src/main/git/command-runner/git-exec-options.ts b/src/main/git/command-runner/git-exec-options.ts index 3963c0ebefe..23fa45b0d32 100644 --- a/src/main/git/command-runner/git-exec-options.ts +++ b/src/main/git/command-runner/git-exec-options.ts @@ -25,7 +25,6 @@ export type GitExecOptions = { preferWslDirectGit?: boolean useConfiguredSshCommandForNetwork?: boolean terminationBarrier?: boolean - captureWslLoginShellOutput?: boolean /** Scheduler priority for this child; status is the safe default. */ admissionTier?: GitAdmissionTier /** Skips general admission; only for a caller that bounds its own concurrency (worktree deletes). */ diff --git a/src/main/git/command-runner/wsl-command-resolution.ts b/src/main/git/command-runner/wsl-command-resolution.ts index 8b7ea8bf0ba..aee44d5d2ca 100644 --- a/src/main/git/command-runner/wsl-command-resolution.ts +++ b/src/main/git/command-runner/wsl-command-resolution.ts @@ -49,7 +49,7 @@ export type ResolvedCommand = { cwd: string | undefined wsl: WslPathInfo | null wslMode: 'direct-git' | 'login-shell' | 'non-login-shell' | null - /** Present only when the caller opted into a fenced login-shell read. */ + /** Present only for a fenced (buffered) login-shell read. */ captured?: WslCapturedLoginShellCommand termination?: WslProcessGroupTermination } diff --git a/src/main/git/remote-rebase.ts b/src/main/git/remote-rebase.ts index d978f644695..edce789c490 100644 --- a/src/main/git/remote-rebase.ts +++ b/src/main/git/remote-rebase.ts @@ -30,8 +30,7 @@ async function gitPullRebaseFromBaseUnlocked( await runWithGitReadCacheInvalidation(async () => { const operationOptions = { ...gitOptionsForWorktree(worktreePath, options), - terminationBarrier: true, - captureWslLoginShellOutput: true + terminationBarrier: true } let rebaseRef: string | null = null try { diff --git a/src/main/git/remote.test.ts b/src/main/git/remote.test.ts index 69e19fcae17..f2c01a355ff 100644 --- a/src/main/git/remote.test.ts +++ b/src/main/git/remote.test.ts @@ -14,8 +14,7 @@ import { gitFastForward, gitFetch, gitPull, gitPullRebaseFromBase, gitPush } fro const REBASE_OPERATION_OPTIONS = { cwd: '/repo', - terminationBarrier: true, - captureWslLoginShellOutput: true + terminationBarrier: true } describe('git remote operations', () => { diff --git a/src/main/git/repo-branch-conflict-batched-probe.test.ts b/src/main/git/repo-branch-conflict-batched-probe.test.ts deleted file mode 100644 index e1a366c1a0d..00000000000 --- a/src/main/git/repo-branch-conflict-batched-probe.test.ts +++ /dev/null @@ -1,101 +0,0 @@ -// Fence login banners so batched probes keep one stdout record per ref and avoid per-remote fallback. - -import { beforeEach, describe, expect, it, vi } from 'vitest' - -const { gitExecFileAsyncMock } = vi.hoisted(() => ({ gitExecFileAsyncMock: vi.fn() })) - -vi.mock('./runner', () => ({ gitExecFileAsync: gitExecFileAsyncMock })) - -import { getBranchConflictKind } from './repo-branch-conflict' - -const REMOTES = Array.from({ length: 58 }, (_, index) => `r${index}`) -const BRANCH = 'user/feature' -const WSL_BANNER = - 'Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64)\n' + - 'To run a command as administrator (user "root"), use "sudo ".\n' - -type GitExecOptions = { stdin?: string; captureWslLoginShellOutput?: boolean } - -/** Simulate a login banner unless fenced capture was requested. */ -function installLoginShellRunner(): { argv: string[][] } { - const argv: string[][] = [] - gitExecFileAsyncMock.mockImplementation(async (args: string[], options: GitExecOptions = {}) => { - argv.push(args) - if (args[0] === 'for-each-ref') { - return { stdout: '', stderr: '' } - } - if (args[0] === 'rev-parse') { - throw new Error('local branch is absent') - } - if (args[0] === 'remote') { - return { stdout: `${WSL_BANNER}${REMOTES.join('\n')}\n`, stderr: '' } - } - if (args[0] === 'show-ref') { - throw Object.assign(new Error('missing ref'), { code: 1, stderr: '' }) - } - if (args[0] === 'cat-file') { - const payload = `${(options.stdin ?? '') - .split('\n') - .filter(Boolean) - .map((ref) => `${ref} missing`) - .join('\n')}\n` - return { - stdout: options.captureWslLoginShellOutput ? payload : `${WSL_BANNER}${payload}`, - stderr: '' - } - } - throw new Error(`unexpected git command: ${args.join(' ')}`) - }) - return { argv } -} - -function countSubcommand(argv: readonly string[][], subcommand: string): number { - return argv.filter((args) => args[0] === subcommand).length -} - -describe('getBranchConflictKind batched remote probe', () => { - beforeEach(() => { - gitExecFileAsyncMock.mockReset() - }) - - it('asks the WSL login shell to fence the batch payload it parses', async () => { - installLoginShellRunner() - - await getBranchConflictKind('/repo', BRANCH) - - const batchCall = gitExecFileAsyncMock.mock.calls.find(([args]) => args[0] === 'cat-file') - expect(batchCall?.[1]).toMatchObject({ captureWslLoginShellOutput: true }) - }) - - it('answers from one batched subprocess instead of one show-ref per remote', async () => { - const { argv } = installLoginShellRunner() - - await expect(getBranchConflictKind('/repo', BRANCH)).resolves.toBeNull() - - expect(countSubcommand(argv, 'cat-file')).toBe(1) - expect(countSubcommand(argv, 'show-ref')).toBe(0) - }) - - it('still falls back to per-ref probes when the batch itself fails', async () => { - gitExecFileAsyncMock.mockImplementation(async (args: string[]) => { - if (args[0] === 'for-each-ref') { - return { stdout: '', stderr: '' } - } - if (args[0] === 'rev-parse') { - throw new Error('local branch is absent') - } - if (args[0] === 'remote') { - return { stdout: `${REMOTES.join('\n')}\n`, stderr: '' } - } - if (args[0] === 'cat-file') { - throw new Error('cat-file is unavailable on this host') - } - if (args[0] === 'show-ref') { - return { stdout: '', stderr: '' } - } - throw new Error(`unexpected git command: ${args.join(' ')}`) - }) - - await expect(getBranchConflictKind('/repo', BRANCH)).resolves.toBe('remote') - }) -}) diff --git a/src/main/git/repo-branch-conflict.ts b/src/main/git/repo-branch-conflict.ts index 63408b405d2..5b0a5280389 100644 --- a/src/main/git/repo-branch-conflict.ts +++ b/src/main/git/repo-branch-conflict.ts @@ -190,23 +190,20 @@ export function getBranchConflictKind( const execOptions = gitExecOptions(path, options) const runLocalGit = ( argv: string[], - commandOptions?: ExactRefProbeExecOptions & { stdin?: string }, - captureWslLoginShellOutput = false + commandOptions?: ExactRefProbeExecOptions & { stdin?: string } ): Promise<{ stdout: string }> => gitExecFileAsync(argv, { ...execOptions, - ...(captureWslLoginShellOutput ? { captureWslLoginShellOutput: true } : {}), ...(commandOptions?.maxBuffer === undefined ? {} : { maxBuffer: commandOptions.maxBuffer }), ...(commandOptions?.timeoutMs === undefined ? {} : { timeout: commandOptions.timeoutMs }), ...(commandOptions?.stdin === undefined ? {} : { stdin: commandOptions.stdin }) }) return getBranchConflictKindViaExec( - (argv, commandOptions) => runLocalGit(argv, commandOptions, argv[0] === 'for-each-ref'), + runLocalGit, branchName, allowedBaseRef, {}, - // Fence login banners to preserve one stdout line per ref; quiet exit-code probes need no markers. - (argv, commandOptions) => runLocalGit(argv, commandOptions, true), + runLocalGit, allowLocalBranch ) } diff --git a/src/main/git/runner-wsl-direct-read.test.ts b/src/main/git/runner-wsl-direct-read.test.ts index 3bfb793bd23..a90389191b9 100644 --- a/src/main/git/runner-wsl-direct-read.test.ts +++ b/src/main/git/runner-wsl-direct-read.test.ts @@ -283,7 +283,6 @@ describe('WSL direct Git reads', () => { cwd: String.raw`C:\repo`, env: { GIT_CONFIG_GLOBAL: '/home/user/custom.gitconfig' }, wslDistro: DISTRO, - captureWslLoginShellOutput: true, terminationBarrier: true }) ).resolves.toEqual({ stdout: 'fork-point\n', stderr: '' }) diff --git a/src/main/git/runner-wsl-login-shell-capture.test.ts b/src/main/git/runner-wsl-login-shell-capture.test.ts index 3354dc9c0ac..dd3b78d9b5f 100644 --- a/src/main/git/runner-wsl-login-shell-capture.test.ts +++ b/src/main/git/runner-wsl-login-shell-capture.test.ts @@ -38,8 +38,8 @@ function createMockChild(): EventEmitter & { stdout: EventEmitter; stderr: Event } /** Stand in for the guest shell: banner first, then the payload inside the command's fence. */ -function respondWithFencedPayload(payload: Buffer | string): void { - execFileMock.mockImplementation((_command, args, _options, callback) => { +function respondWithFencedPayload(payload: Buffer | string, error: Error | null = null): void { + execFileMock.mockImplementation((_command, args, options, callback) => { const nonce = /__ORCA_WSL_CAPTURE_BEGIN_ ([^_]+)__/.exec(String(args.at(-1)))?.[1] ?? '' const body = typeof payload === 'string' ? Buffer.from(payload, 'utf8') : payload const stdout = Buffer.concat([ @@ -48,7 +48,10 @@ function respondWithFencedPayload(payload: Buffer | string): void { body, Buffer.from(`__ORCA_WSL_CAPTURE_END_${nonce}__`, 'utf8') ]) - queueMicrotask(() => callback?.(null, stdout, Buffer.alloc(0))) + const asText = options?.encoding !== 'buffer' + queueMicrotask(() => + callback?.(error, asText ? stdout.toString('utf8') : stdout, asText ? '' : Buffer.alloc(0)) + ) return createMockChild() }) } @@ -90,6 +93,29 @@ describe('WSL login-shell reads are fenced', () => { expect(Buffer.compare(stdout, binary)).toBe(0) }) + it('fences buffered text reads without a per-call opt-in', async () => { + respondWithFencedPayload('3\t1\ta.txt\0') + + const { stdout } = await gitExecFileAsync(['diff', '--numstat', '-z'], { + cwd: WSL_CWD, + wslDistro: DISTRO + }) + + expect(stdout).toBe('3\t1\ta.txt\0') + }) + + it('strips the fence from stdout of a non-zero exit', async () => { + respondWithFencedPayload('ignored.log\0', Object.assign(new Error('exit 1'), { code: 1 })) + + const failure = await gitExecFileAsync(['check-ignore', '-z', '--stdin'], { + cwd: WSL_CWD, + wslDistro: DISTRO, + stdin: 'ignored.log\0' + }).catch((error: unknown) => error) + + expect(failure).toMatchObject({ code: 1, stdout: 'ignored.log\0' }) + }) + /** Answer the core.sshCommand probe with `configured`, and any other command with ok. */ function respondToSshPolicyProbe(configured: string): void { execFileMock.mockImplementation((_command, args, _options, callback) => { diff --git a/src/main/git/source-control/bulk-pathspec-command-line-budget.test.ts b/src/main/git/source-control/bulk-pathspec-command-line-budget.test.ts index 4a0e190aa67..4994cfb3f11 100644 --- a/src/main/git/source-control/bulk-pathspec-command-line-budget.test.ts +++ b/src/main/git/source-control/bulk-pathspec-command-line-budget.test.ts @@ -34,10 +34,14 @@ const WSL_WORKTREE = `\\\\wsl$\\${WSL_DISTRO}\\home\\emilio\\projects\\orca` /** Windows-side length of the line `wsl.exe` is spawned with, wrapper included. */ function finishedCommandLineLength(args: readonly string[], wslDistro?: string): number { - const resolved = resolveGitCommandWithoutProbe([...args], { - cwd: wslDistro ? WSL_WORKTREE : '/home/emilio/projects/orca', - ...(wslDistro ? { wslDistro } : {}) - }) + const resolved = resolveGitCommandWithoutProbe( + [...args], + { + cwd: wslDistro ? WSL_WORKTREE : '/home/emilio/projects/orca', + ...(wslDistro ? { wslDistro } : {}) + }, + true + ) return commandLineLength([resolved.binary, ...resolved.args]) } diff --git a/src/main/git/source-control/discard-changes.ts b/src/main/git/source-control/discard-changes.ts index f4bb434653a..2222a4b16b2 100644 --- a/src/main/git/source-control/discard-changes.ts +++ b/src/main/git/source-control/discard-changes.ts @@ -65,11 +65,7 @@ async function listTrackedPathSpecs( const trackedPaths: string[] = [] const commands = bulkPathspecCommands(['ls-files', '-z', '--'], filePaths, worktreePath, options) for (const args of commands) { - const { stdout } = await gitExecFileAsync(args, { - ...gitOptionsForWorktree(worktreePath, options), - // Fence WSL rc banners: a corrupted first NUL record could misclassify a tracked path as untracked. - captureWslLoginShellOutput: true - }) + const { stdout } = await gitExecFileAsync(args, gitOptionsForWorktree(worktreePath, options)) // Iterate instead of spreading: tracked directories can exceed the JS argument limit. for (const trackedPath of stdout.split('\0')) { if (trackedPath) { diff --git a/src/main/git/source-control/drive-checkout-suspect-writes.ts b/src/main/git/source-control/drive-checkout-suspect-writes.ts index fc22989417f..e92c56c88fb 100644 --- a/src/main/git/source-control/drive-checkout-suspect-writes.ts +++ b/src/main/git/source-control/drive-checkout-suspect-writes.ts @@ -22,11 +22,7 @@ async function listMergedTrackedPaths( worktreePath, options )) { - const { stdout } = await gitExecFileAsync(args, { - ...gitOptionsForWorktree(worktreePath, options), - // Why: buffered NUL records must not carry the WSL login shell's rc banner. - captureWslLoginShellOutput: true - }) + const { stdout } = await gitExecFileAsync(args, gitOptionsForWorktree(worktreePath, options)) for (const record of stdout.split('\0')) { const match = /^\d+ [0-9a-f]+ 0\t(.+)$/s.exec(record) if (match) { diff --git a/src/main/git/source-control/git-pathspec.ts b/src/main/git/source-control/git-pathspec.ts index 2c7654e786e..c34c5c6abee 100644 --- a/src/main/git/source-control/git-pathspec.ts +++ b/src/main/git/source-control/git-pathspec.ts @@ -45,10 +45,15 @@ function finishedCommandLineLength( ): number { // Include config that Windows-checkout routing may add after chunking. const budgetArgs = options.wslDistro ? [...WINDOWS_CHECKOUT_GIT_CONFIG_ARGS, ...args] : [...args] - const resolved = resolveGitCommandWithoutProbe(budgetArgs, { - cwd: worktreePath, - ...(options.wslDistro ? { wslDistro: options.wslDistro } : {}) - }) + // Measure the longer fenced form that buffered login-shell reads spawn. + const resolved = resolveGitCommandWithoutProbe( + budgetArgs, + { + cwd: worktreePath, + ...(options.wslDistro ? { wslDistro: options.wslDistro } : {}) + }, + true + ) return commandLineLength([resolved.binary, ...resolved.args]) } diff --git a/src/main/git/source-control/submodule-paths.ts b/src/main/git/source-control/submodule-paths.ts index 7d353094df5..2f0260f1fde 100644 --- a/src/main/git/source-control/submodule-paths.ts +++ b/src/main/git/source-control/submodule-paths.ts @@ -101,8 +101,7 @@ export async function listSubmodulePaths( ['config', '-z', '--file', '.gitmodules', '--get-regexp', '^submodule\\..*\\.path$'], { ...gitOptionsForWorktree(worktreePath, options), - env: gitOptionalLocksDisabledEnv(), - captureWslLoginShellOutput: true + env: gitOptionalLocksDisabledEnv() } ) for (const record of iterateNulDelimitedFields(stdout)) { diff --git a/src/main/git/status-discard-and-bulk-staging.test.ts b/src/main/git/status-discard-and-bulk-staging.test.ts index 83bb6868c48..88519ab6ab1 100644 --- a/src/main/git/status-discard-and-bulk-staging.test.ts +++ b/src/main/git/status-discard-and-bulk-staging.test.ts @@ -183,11 +183,7 @@ describe('bulk git helpers', () => { ':(literal)docs', ':(literal)scratch' ], - { - cwd: '/repo', - // Keep the WSL login banner out of the first buffered NUL record. - captureWslLoginShellOutput: true - } + { cwd: '/repo' } ) // A tracked descendant keeps directory pathspecs on the restore path. expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith( diff --git a/src/main/runtime/runtime-worktree-create-git.test.ts b/src/main/runtime/runtime-worktree-create-git.test.ts index 47ff892606d..f0ad38cab4d 100644 --- a/src/main/runtime/runtime-worktree-create-git.test.ts +++ b/src/main/runtime/runtime-worktree-create-git.test.ts @@ -45,7 +45,7 @@ describe('explicit local branch reuse on the execution host', () => { ], [ ['for-each-ref', '--format=%(refname)', 'refs/heads/feature/test'], - { cwd: '/repo', wslDistro: 'Ubuntu', captureWslLoginShellOutput: true } + { cwd: '/repo', wslDistro: 'Ubuntu' } ] ]) expect(worktrees).toHaveBeenCalledWith('/repo', { wslDistro: 'Ubuntu' }) @@ -82,7 +82,7 @@ describe('explicit local branch reuse on the execution host', () => { ).rejects.toThrow('already checked out at "/busy"') expect(git).toHaveBeenLastCalledWith( ['for-each-ref', '--format=%(refname)', 'refs/heads/feature/TEST'], - { cwd: '/repo', wslDistro: 'Ubuntu', captureWslLoginShellOutput: true } + { cwd: '/repo', wslDistro: 'Ubuntu' } ) }) diff --git a/src/main/runtime/runtime-worktree-create-git.ts b/src/main/runtime/runtime-worktree-create-git.ts index 926cffbeb36..8a2f2488713 100644 --- a/src/main/runtime/runtime-worktree-create-git.ts +++ b/src/main/runtime/runtime-worktree-create-git.ts @@ -69,8 +69,7 @@ export async function canCheckoutExistingLocalBranch( const ref = `refs/heads/${name}` const { stdout } = await gitExecFileAsync(['for-each-ref', '--format=%(refname)', ref], { cwd: repoPath, - ...gitOptions, - captureWslLoginShellOutput: true + ...gitOptions }) return stdout.split(/\r?\n/).includes(ref) }