From 842b4ac3d3c182a3f808f7ede861f8c7405d5020 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 15 Aug 2024 11:40:36 -0400 Subject: [PATCH] Improve VS Code debugger support --- .vscode/launch.json | 13 +++++++++++-- Library/Homebrew/dev-cmd/debugger.rb | 9 +++++---- Library/Homebrew/shims/gems/rdbg | 6 ++++++ bin/brew | 12 ++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100755 Library/Homebrew/shims/gems/rdbg diff --git a/.vscode/launch.json b/.vscode/launch.json index 108f3d8486..867e7ff68c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -3,9 +3,18 @@ "configurations": [ { "type": "rdbg", - "name": "Attach with rdbg", + "name": "Debug Homebrew command", + "request": "launch", + "rdbgPath": "${workspaceFolder}/Library/Homebrew/shims/gems/rdbg", + "command": "brew debugger --", + "script": "${fileBasenameNoExtension}", + "askParameters": true + }, + { + "type": "rdbg", + "name": "Attach to Homebrew debugger", "request": "attach", - "rdbgPath": "${workspaceFolder}/Library/Homebrew/vendor/portable-ruby/current/lib/ruby/gems/3.3.0/gems/debug-1.9.1/exe/rdbg", + "rdbgPath": "${workspaceFolder}/Library/Homebrew/shims/gems/rdbg", "env": { "TMPDIR": "/private/tmp/", } diff --git a/Library/Homebrew/dev-cmd/debugger.rb b/Library/Homebrew/dev-cmd/debugger.rb index 255766207e..9783eafc63 100644 --- a/Library/Homebrew/dev-cmd/debugger.rb +++ b/Library/Homebrew/dev-cmd/debugger.rb @@ -11,8 +11,6 @@ module Homebrew To pass flags to the command, use `--` to separate them from the `brew` flags. For example: `brew debugger -- list --formula`. EOS - switch "-s", "--stop", - description: "Stop at the beginning of the script." switch "-O", "--open", description: "Start remote debugging over a Unix socket." @@ -28,14 +26,17 @@ module Homebrew end brew_rb = (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path - nonstop = "1" unless args.stop? debugger_method = if args.open? "open" else "start" end - with_env RUBY_DEBUG_NONSTOP: nonstop, RUBY_DEBUG_FORK_MODE: "parent" do + env = {} + env[:RUBY_DEBUG_FORK_MODE] = "parent" + env[:RUBY_DEBUG_NONSTOP] = "1" unless ENV["HOMEBREW_RDBG"] + + with_env(**env) do system(*HOMEBREW_RUBY_EXEC_ARGS, "-I", $LOAD_PATH.join(File::PATH_SEPARATOR), "-rdebug/#{debugger_method}", diff --git a/Library/Homebrew/shims/gems/rdbg b/Library/Homebrew/shims/gems/rdbg new file mode 100755 index 0000000000..3c4f3b0b28 --- /dev/null +++ b/Library/Homebrew/shims/gems/rdbg @@ -0,0 +1,6 @@ +#!/bin/bash + +export HOMEBREW_RDBG="1" + +HOMEBREW_PORTABLE_RUBY_BIN="$(cd "$(dirname "$0")"/../../ && pwd)/vendor/portable-ruby/current/bin/" +exec "${HOMEBREW_PORTABLE_RUBY_BIN}"rdbg "$@" diff --git a/bin/brew b/bin/brew index a4b06170b7..972bfa3eda 100755 --- a/bin/brew +++ b/bin/brew @@ -258,6 +258,18 @@ then FILTERED_ENV+=("${VAR}=${!VAR}") done fi + +if [[ -n "${HOMEBREW_RDBG:-}" ]] +then + for VAR in "${!RUBY_DEBUG_@}" + do + # Skip if variable value is empty. + [[ -z "${!VAR:-}" ]] && continue + + FILTERED_ENV+=("${VAR}=${!VAR}") + done +fi + unset VAR ENV_VAR_NAMES exec /usr/bin/env -i "${FILTERED_ENV[@]}" /bin/bash -p "${HOMEBREW_LIBRARY}/Homebrew/brew.sh" "$@"