Skip to content

Incorrect TerminalShellExecution exit code #237517

Closed

Description

@njiang747

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:

  1. Listen for onDidEndTerminalShellExecution events
  2. Run a command that returns some exit code
  3. Type the same command and interrupt with ^C (i.e. interrupt without actually running the command)
  4. The exit code reported by the TerminalShellExecution event for the second execution (expected to be undefined since 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 undefined

From a quick look, this seems like caused by this code (marked as a hack):

// 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;
}
}

Activity

  1. vs-code-engineering commented on Jan 8, 2025

    @vs-code-engineering
    Contributor

    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!

  2. added
    bug
    terminal-shell-bash
    on Jan 10, 2025
  3. added this to the Backlog milestone on Jan 10, 2025
  4. added
    help wanted
    on Jan 10, 2025
  5. brthom commented on Apr 11, 2025

    @brthom
    Contributor

    The way I am thinking about working through this, there are two paths:

    1. The robust fix suggested by the hack, feeding the command ID and exit code to more correctly detect whether a command is a duplicate.
    2. Keeping the hack, but improving the heuristic such that it doesn't happen in nearly as many cases.

    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 previousCommand
    exitCode = previousExitCode

    A 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 previousCommand
    exitCode = previousExitCode
    else if undefined and interrupted:
    exitCode = 130

    This 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?

  6. meganrogge commented on Apr 14, 2025

    @meganrogge
    Collaborator

    I think approach 2 sounds good bc it will cover 99.999% of cases.

  7. added a commit that references this issue on Apr 1, 2026
    92ffdf2
  8. removed this from the 1.116.0 milestone on Apr 13, 2026
  9. added a commit that references this issue on Jul 30, 2026
    3670eec
  10. added
    unreleased
    on Jul 30, 2026
  11. added this to the 1.132.0 milestone on Jul 30, 2026
  12. added
    insiders-released
    and removed
    unreleased
    on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bughelp wantedinsiders-releasedterminal-shell-bashverified

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions