MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / buildExecCommand

Function buildExecCommand

source code/utils/shell/bashProvider.ts:79–200  ·  view source on GitHub ↗
(
      command: string,
      opts: {
        id: number | string
        sandboxTmpDir?: string
        useSandbox: boolean
      },
    )

Source from the content-addressed store, hash-verified

77 detached: true,
78
79 async buildExecCommand(
80 command: string,
81 opts: {
82 id: number | string
83 sandboxTmpDir?: string
84 useSandbox: boolean
85 },
86 ): Promise<{ commandString: string; cwdFilePath: string }> {
87 let snapshotFilePath = await snapshotPromise
88 // This access() check is NOT pure TOCTOU — it's the fallback decision
89 // point for getSpawnArgs. When the snapshot disappears mid-session
90 // (tmpdir cleanup), we must clear lastSnapshotFilePath so getSpawnArgs
91 // adds -l and the command gets login-shell init. Without this check,
92 // `source ... || true` silently fails and commands run with NO shell
93 // init (neither snapshot env nor login profile). The `|| true` on source
94 // still guards the race between this check and the spawned shell.
95 if (snapshotFilePath) {
96 try {
97 await access(snapshotFilePath)
98 } catch {
99 logForDebugging(
100 `Snapshot file missing, falling back to login shell: ${snapshotFilePath}`,
101 )
102 snapshotFilePath = undefined
103 }
104 }
105 lastSnapshotFilePath = snapshotFilePath
106
107 // Stash sandboxTmpDir for use in getEnvironmentOverrides
108 currentSandboxTmpDir = opts.sandboxTmpDir
109
110 const tmpdir = osTmpdir()
111 const isWindows = getPlatform() === 'windows'
112 const shellTmpdir = isWindows ? windowsPathToPosixPath(tmpdir) : tmpdir
113
114 // shellCwdFilePath: POSIX path used inside the bash command (pwd -P >| ...)
115 // cwdFilePath: native OS path used by Node.js for readFileSync/unlinkSync
116 // On non-Windows these are identical; on Windows, Git Bash needs POSIX paths
117 // but Node.js needs native Windows paths for file operations.
118 const shellCwdFilePath = opts.useSandbox
119 ? posixJoin(opts.sandboxTmpDir!, `cwd-${opts.id}`)
120 : posixJoin(shellTmpdir, `claude-${opts.id}-cwd`)
121 const cwdFilePath = opts.useSandbox
122 ? posixJoin(opts.sandboxTmpDir!, `cwd-${opts.id}`)
123 : nativeJoin(tmpdir, `claude-${opts.id}-cwd`)
124
125 // Defensive rewrite: the model sometimes emits Windows CMD-style `2>nul`
126 // redirects. In POSIX bash (including Git Bash on Windows), this creates a
127 // literal file named `nul` — a reserved device name that breaks git.
128 // See anthropics/claude-code#4928.
129 const normalizedCommand = rewriteWindowsNullRedirect(command)
130 const addStdinRedirect = shouldAddStdinRedirect(normalizedCommand)
131 let quotedCommand = quoteShellCommand(normalizedCommand, addStdinRedirect)
132
133 // Debug logging for heredoc/multiline commands to trace trailer handling
134 // Only log when commit attribution is enabled to avoid noise
135 if (
136 feature('COMMIT_ATTRIBUTION') &&

Callers

nothing calls this directly

Calls 10

logForDebuggingFunction · 0.85
getPlatformFunction · 0.85
shouldAddStdinRedirectFunction · 0.85
quoteShellCommandFunction · 0.85
rearrangePipeCommandFunction · 0.85
quoteFunction · 0.85
getDisableExtglobCommandFunction · 0.85
formatShellPrefixCommandFunction · 0.85

Tested by

no test coverage detected