vscode-js-debug icon indicating copy to clipboard operation
vscode-js-debug copied to clipboard

Support external terminal option

Open Avivbens opened this issue 10 months ago • 3 comments

Is your feature request related to a problem? Please describe.

The issue

I was trying to find a way to run the debug session whitin an external terminal (such as Warp / iTerm2) - based on the default terminal setting, for MacOS it is terminal.external.osxExec.

Describe the feature you'd like

My suggestion

A nice checkbox for enabling this feature, so once I'm hitting on the new debug session (by the code lens on the package.json OR with any menu exists, such as the NPM SCRIPTS) - the new debug session would trigger commands over my default terminal.

Screenshots

CleanShot 2024-04-15 at 15 08 56@2x CleanShot 2024-04-15 at 15 08 04@2x

Avivbens avatar Apr 15 '24 12:04 Avivbens

HI @connor4312 👋

Any idea about that? I can help with the PR 👍

Avivbens avatar May 21 '24 07:05 Avivbens

Sure, a PR is welcome. Note this is actually implemented in https://github.com/microsoft/vscode/tree/main/extensions/npm

connor4312 avatar May 21 '24 16:05 connor4312

I was trying to find a way to run the debug session whitin an external terminal (such as Warp / iTerm2)

I managed to have external processes (i.e. launched from outside vscode integrated terminals) auto-attach to vscode debug sessions by hacking with it a bit.

Step 1:

Create an automatic task that exports the debugging env vars.

.vscode/tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Capture Debug Vars",
      "type": "shell",
      "command": "bash",
      "args": [".vscode/captureDebugVars.sh"],
      "problemMatcher": [],
      "options": {
        "env": {
          "ENV_FILE": "${workspaceFolder}/.env.local" // set any file you want
        }
      },
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}

.vscode/captureDebugVars.sh

#!/usr/bin/env bash
set -euo pipefail

# Check if required environment variables are set
if [[ -z "${ENV_FILE:-}" ]]; then
  echo "You forgot to set ENV_FILE"
  exit 1
fi

if [[ -z "${VSCODE_INSPECTOR_OPTIONS:-}" || -z "${NODE_OPTIONS:-}" ]]; then
  echo "VS Code has not defined VSCODE_INSPECTOR_OPTIONS or NODE_OPTIONS"
  exit 1
fi

# Load current configuration if the file exists
declare -A env
if [[ -f "$ENV_FILE" ]]; then
  while IFS='=' read -r key value; do
    env["$key"]="$value"
  done < "$ENV_FILE"
fi

# Merge new configuration
env["NODE_OPTIONS"]="${NODE_OPTIONS}"
env["VSCODE_INSPECTOR_OPTIONS"]="${VSCODE_INSPECTOR_OPTIONS}"

# Write the new configuration back to the file
{
  for key in "${!env[@]}"; do
    echo "$key=${env[$key]}"
  done
} > "$ENV_FILE"

echo "Environment variables captured to $ENV_FILE"

Step 2

Find a way to automatically load the env vars into your terminal. I use and recommend direnv:

.envrc

source .env.local # or whatever you used as ENV_FILE on step 1

With that in place, all your scripts will be automatically debugged in vscode as long as vscode is running and you have opened it before running your external script.

zvictor avatar Sep 12 '24 12:09 zvictor