microsoft / vscode-js-debug

A DAP-compatible JavaScript debugger. Used in VS Code, VS, + more
MIT License
1.67k stars 283 forks source link

Debugger Attaches After Test Command Executes Twice #2083

Closed iivaylov closed 2 months ago

iivaylov commented 2 months ago

Describe the bug When running unit tests using Jest and the VSCode debugger (Node.js version 20.9.0), the test command is executed twice before the debugger successfully connects. This results in redundant environment setup and delays in the debugging process.

This behavior occurs even when autoAttachMode is set to "smart" and a proper launch.json configuration is used for the debugger.

To Reproduce Steps to reproduce the behavior:

  1. Configure the launch.json file with the following configuration for debugging Jest tests:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Unit Tests",
      "type": "node",
      "request": "launch",
      "runtimeArgs": [
        "--inspect-brk",
        "../../../node_modules/.bin/jest", 
        "--runInBand", 
        "--config", 
        "./jest-unit.config.ts", 
        "--forceExit"
      ],
      "env": {
        "NODE_ENV": "test"
      },
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
    }
  ]
}
  1. Ensure autoAttachMode is set to "smart" in the VSCode settings.
  2. Run the debugger using the Debug Unit Tests configuration.
  3. Node.js version in use: 20.9.0

Expected Behavior The test command should execute once, and the debugger should attach immediately after the test environment is set up.

Actual Behavior The test command is executed twice, and only after the second execution does the debugger successfully attach. The integrated terminal shows the following command running twice before attaching:

/usr/bin/env NODE_ENV=test 'NODE_OPTIONS= --require "/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/ms-vscode.js-debug/src/bootloader.js" --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/var/folders/tc/.../node-cdp.<ID>.sock"}' /Users/<USER>/.nvm/versions/node/v20.9.0/bin/node ../../node_modules/.bin/jest --runInBand --config ./jest-unit.config.ts --forceExit
➜  account /usr/bin/env NODE_ENV=test 'NODE_OPTIONS= --require "/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/ms-vscode.js-debug/src/bootloader.js" --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/var/folders/tc/.../node-cdp.<ID>.sock"}' /Users/<USER>/.nvm/versions/node/v20.9.0/bin/node ../../node_modules/.bin/jest --runInBand --config ./jest-unit.config.ts --forceExit
Debugger attached.

After the second run, the terminal prints:

Debugger attached.

Additional Information

  1. Node.js version: 20.9.0
  2. autoAttachMode is already set to "smart".
  3. The issue persists even after adjusting the configuration in launch.json.
  4. The test environment is being loaded twice, which causes performance issues and delays in attaching the debugger.

Expected Fix The debugger should attach immediately after the first execution of the command without running the test environment twice.

connor4312 commented 2 months ago

Duplicates https://github.com/microsoft/vscode/issues/228191