MCPcopy Create free account
hub / github.com/Noumena-Network/code / buildExecCommand

Function buildExecCommand

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected