fix(git): fence WSL login-shell stdout for all buffered git reads (#35053)

Buffered gitExecFileAsync reads now always fence login-shell rc output, and
non-zero exits de-fence error.stdout. The per-call captureWslLoginShellOutput
opt-in is removed since it is no longer meaningful.

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
This commit is contained in:
OrcaWin
2026-10-30 06:14:18 -07:00
committed by GitHub
co-authored by OrcaWin
parent e65171ae01
commit d1aff3a5be
17 changed files with 74 additions and 160 deletions
+15 -14
View File
@@ -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
@@ -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). */
@@ -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
}
+1 -2
View File
@@ -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 {
+1 -2
View File
@@ -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', () => {
@@ -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 <command>".\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')
})
})
+3 -6
View File
@@ -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
)
}
@@ -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: '' })
@@ -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) => {
@@ -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])
}
@@ -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) {
@@ -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) {
+9 -4
View File
@@ -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])
}
@@ -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)) {
@@ -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(
@@ -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' }
)
})
@@ -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)
}