Repository navigation
Closed
Labels
bugIssue identified by VS Code Team member as probable bughelp wantedIssues identified as good community contribution opportunitiesinsiders-releasedPatch has been released in VS Code Insidersterminal-shell-bashAn issue in the terminal specific to bash, including shell integrationverifiedVerification succeeded
Milestone
Description
Does this issue occur when all extensions are disabled?: Yes/No
- VS Code Version: 1.94.0
- OS Version: macOS Sonoma 14.4
Steps to Reproduce:
- Listen for
onDidEndTerminalShellExecutionevents - Run a command that returns some exit code
- Type the same command and interrupt with ^C (i.e. interrupt without actually running the command)
- The exit code reported by the
TerminalShellExecutionevent for the second execution (expected to beundefinedsince the command didn't actually run) will be the same as the previous command's exit code
For example:
$ echo test^C
-> exit code undefined
$ echo test
-> exit code 0
$ echo test^C
-> exit code 0
$ echo test^C
-> exit code 0
$ echo not test^C
-> exit code undefined
$ echo test^C
-> exit code undefinedFrom a quick look, this seems like caused by this code (marked as a hack):
vscode/src/vs/platform/terminal/common/capabilities/commandDetectionCapability.ts
Lines 360 to 370 in 3b30f94
| // HACK: Handle a special case on some versions of bash where identical commands get merged | |
| // in the output of `history`, this detects that case and sets the exit code to the the last | |
| // command's exit code. This covered the majority of cases but will fail if the same command | |
| // runs with a different exit code, that will need a more robust fix where we send the | |
| // command ID and exit code over to the capability to adjust there. | |
| if (exitCode === undefined) { | |
| const lastCommand = this.commands.length > 0 ? this.commands[this.commands.length - 1] : undefined; | |
| if (this._currentCommand.command && this._currentCommand.command.length > 0 && lastCommand?.command === this._currentCommand.command) { | |
| exitCode = lastCommand.exitCode; | |
| } | |
| } |
Metadata
Metadata
Assignees
Labels
bugIssue identified by VS Code Team member as probable bughelp wantedIssues identified as good community contribution opportunitiesinsiders-releasedPatch has been released in VS Code Insidersterminal-shell-bashAn issue in the terminal specific to bash, including shell integrationverifiedVerification succeeded
Activity
vs-code-engineering commentedon Jan 8, 2025
Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.96.2. Please try upgrading to the latest version and checking whether this issue remains.
Happy Coding!
brthom commentedon Apr 11, 2025
The way I am thinking about working through this, there are two paths:
1 is obviously ideal, but I think 2 could approximate it with a lot less effort. Currently the hack heuristic is:
if exitCode equals undefined:if command equals previousCommandexitCode = previousExitCodeA trivial improvement could be to read an interrupt at the end of the command and modify the heuristic like this:
if exitCode equals undefined and not interrupted:if command equals previousCommandexitCode = previousExitCodeelse if undefined and interrupted:exitCode = 130This would solve the false positives of the hack, but it would also introduce an (I think) far less common bug; if a message were printed to the terminal that validly ended with a ^c input, it would return a false positive. A user would have to have been playing with fire to print a ^c without escaping anyways, so I would personally tend towards this being fine. IMO even with this false positive this is a better solution than the current hack, which blindly lies to the user any time it sees an undefined exit code.
Would the maintainers prefer holding off for a more robust fix, or would it be acceptable just to improve the hack?
meganrogge commentedon Apr 14, 2025
I think approach 2 sounds good bc it will cover 99.999% of cases.
fix: detect interrupted commands in terminal exit code hack
fix: detect interrupted commands in terminal exit code hack (#307256)