wrap bash compound commands in bash -c before nohup#312202
Merged
meganrogge merged 3 commits intomainfrom Apr 23, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the terminal background-detach command rewriter so that POSIX background commands using shell compound syntax or builtins (which nohup can’t exec directly) are wrapped in bash -c '...' before being passed to nohup, and adds coverage for these cases.
Changes:
- Add
_needsBashCWrapper()detection for compound commands/builtins and wrap withbash -c '...'(including single-quote escaping). - Adjust POSIX rewrite logic to run
nohupagainst the wrapped command. - Add a dedicated test suite covering compound constructs, builtins, grouping, and quote escaping.
Show a summary per file
| File | Description |
|---|---|
| src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/tools/commandLineRewriter/commandLineBackgroundDetachRewriter.ts | Adds detection + wrapping logic for compound commands/builtins before nohup. |
| src/vs/workbench/contrib/terminalContrib/chatAgentTools/test/electron-browser/commandLineBackgroundDetachRewriter.test.ts | Adds tests verifying bash -c wrapping and correct escaping/behavior. |
Copilot's findings
Comments suppressed due to low confidence (2)
src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/tools/commandLineRewriter/commandLineBackgroundDetachRewriter.ts:90
- Comment says dot-source is detected by requiring “a space or end-of-string after the dot”, but the regex only matches
^\.(dot + space). Either update the comment, or adjust the regex to also match the.builtin with no arguments if that’s intended.
// `. file` (dot-source builtin). Exclude `./script` (relative path) by requiring
// a space or end-of-string after the dot.
/^\. /.test(trimmed) ||
// Compound groupings: subshell `( ... )` or brace group `{ ...; }`.
src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/tools/commandLineRewriter/commandLineBackgroundDetachRewriter.ts:66
endsWithBackgroundAmpis now tested againstcommandToWrapafter wrapping. When the original command ends with a trailing&and_needsBashCWrapperis true, the&ends up inside the quotedbash -c '...&'string so the regex won’t match and an extra outer&gets appended (producingnohup bash -c '... &' &). Consider detecting/stripping a trailing background&from the original trimmed command before escaping/wrapping, and then only adding a single outer&to the finalnohupinvocation.
let commandToWrap = trimmed;
if (this._needsBashCWrapper(trimmed)) {
// Escape single quotes for use inside a single-quoted bash -c '...' string.
const escaped = trimmed.replace(/'/g, `'\\''`);
commandToWrap = `bash -c '${escaped}'`;
}
// If the command already ends with a single trailing `&` (background operator,
// as opposed to `&&` for command chaining), don't append another one.
const endsWithBackgroundAmp = /(?:^|[^&])&$/.test(commandToWrap);
const rewritten = endsWithBackgroundAmp
? `nohup ${commandToWrap}`
: `nohup ${commandToWrap} &`;
- Files reviewed: 2/2 changed files
- Comments generated: 1
…apper Agent-Logs-Url: https://github.com/microsoft/vscode/sessions/19961900-7abb-4844-8d89-c98d2b900974 Co-authored-by: meganrogge <29464607+meganrogge@users.noreply.github.com>
mjbvz
approved these changes
Apr 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
nohupcan only exec external commands — it cannot run bash compoundstatements (
for,while,if,case,eval,export, etc.) orshell builtins directly. Commands like:
nohup for i in $(seq 1 90); do echo $i; sleep 1; done &
fail silently because
foris not an executable.Adds
_needsBashCWrapper()to detect compound constructs and builtins,wrapping them as:
nohup bash -c 'for i in $(seq 1 90); do echo $i; sleep 1; done' &
Single quotes inside the command are escaped with the POSIX
'\''pattern.
Fixes eval regressions on
terminal_long_running_prompt(confirmedbroken nohup rewrite) and improves reliability of all background
terminal commands involving shell syntax.
Only affects POSIX paths when
chat.tools.terminal.detachBackgroundProcessesis enabled (experimental, default off).