Skip to content

Commit 86fff7f

Browse files
authored
fix: fix java_tool_option in same terminal (#1581)
1 parent 52bd418 commit 86fff7f

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

bundled/scripts/noConfigScripts/debugjava

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
# Java No-Config Debug Wrapper Script for Unix/Linux/macOS
33
# This script intercepts java commands and automatically enables JDWP debugging
44

5-
# Export the endpoint file path for JDWP port communication
6-
export JDWP_ADAPTER_ENDPOINTS=$VSCODE_JDWP_ADAPTER_ENDPOINTS
7-
8-
# Set JDWP options only for this debugjava invocation
9-
# This overrides the global JAVA_TOOL_OPTIONS to avoid affecting other Java processes
10-
export JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0"
11-
125
# Get the directory of this script
136
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
147

15-
# Use Node.js wrapper to capture JDWP port
8+
# Set environment variables only for the node process, not the current shell
9+
# This ensures JAVA_TOOL_OPTIONS doesn't affect subsequent commands in the terminal
10+
JDWP_ADAPTER_ENDPOINTS=$VSCODE_JDWP_ADAPTER_ENDPOINTS \
11+
JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0" \
1612
exec node "$SCRIPT_DIR/jdwp-wrapper.js" "$@"

bundled/scripts/noConfigScripts/debugjava.bat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
REM Java No-Config Debug Wrapper Script for Windows
33
REM This script intercepts java commands and automatically enables JDWP debugging
44

5+
REM Use setlocal to ensure environment variables don't persist after this script exits
6+
setlocal
7+
58
REM Export the endpoint file path for JDWP port communication
69
set JDWP_ADAPTER_ENDPOINTS=%VSCODE_JDWP_ADAPTER_ENDPOINTS%
710

@@ -11,3 +14,6 @@ set JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,addr
1114

1215
REM Use Node.js wrapper to capture JDWP port
1316
node "%~dp0jdwp-wrapper.js" %*
17+
18+
REM endlocal is implicit at script exit, but we can call it explicitly for clarity
19+
endlocal

bundled/scripts/noConfigScripts/debugjava.fish

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
# Java No-Config Debug Wrapper Script for Fish Shell
33
# This script intercepts java commands and automatically enables JDWP debugging
44

5-
# Export the endpoint file path for JDWP port communication
6-
set -x JDWP_ADAPTER_ENDPOINTS $VSCODE_JDWP_ADAPTER_ENDPOINTS
7-
8-
# Set JDWP options only for this debugjava invocation
9-
# This overrides the global JAVA_TOOL_OPTIONS to avoid affecting other Java processes
10-
set -x JAVA_TOOL_OPTIONS "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0"
11-
125
# Get the directory of this script
136
set script_dir (dirname (status -f))
147

15-
# Use Node.js wrapper to capture JDWP port
16-
exec node "$script_dir/jdwp-wrapper.js" $argv
8+
# Set environment variables only for the node process, not the current shell
9+
# This ensures JAVA_TOOL_OPTIONS doesn't affect subsequent commands in the terminal
10+
env JDWP_ADAPTER_ENDPOINTS=$VSCODE_JDWP_ADAPTER_ENDPOINTS \
11+
JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0" \
12+
node "$script_dir/jdwp-wrapper.js" $argv
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
# Java No-Config Debug Wrapper Script for PowerShell
22
# This script intercepts java commands and automatically enables JDWP debugging
33

4-
# Export the endpoint file path for JDWP port communication
5-
$env:JDWP_ADAPTER_ENDPOINTS = $env:VSCODE_JDWP_ADAPTER_ENDPOINTS
6-
7-
# Set JDWP options only for this debugjava invocation
8-
# This overrides the global JAVA_TOOL_OPTIONS to avoid affecting other Java processes
9-
$env:JAVA_TOOL_OPTIONS = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0"
10-
114
# Get the directory of this script
125
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
136

14-
# Use Node.js wrapper to capture JDWP port
15-
& node (Join-Path $scriptDir "jdwp-wrapper.js") $args
7+
# Save current environment variables to restore later
8+
$oldJavaToolOptions = $env:JAVA_TOOL_OPTIONS
9+
$oldJdwpAdapterEndpoints = $env:JDWP_ADAPTER_ENDPOINTS
10+
11+
try {
12+
# Set environment variables only for this debugjava invocation
13+
$env:JDWP_ADAPTER_ENDPOINTS = $env:VSCODE_JDWP_ADAPTER_ENDPOINTS
14+
$env:JAVA_TOOL_OPTIONS = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0"
15+
16+
# Use Node.js wrapper to capture JDWP port
17+
& node (Join-Path $scriptDir "jdwp-wrapper.js") $args
18+
}
19+
finally {
20+
# Restore original environment variables to avoid affecting subsequent commands
21+
if ($null -eq $oldJavaToolOptions) {
22+
Remove-Item Env:\JAVA_TOOL_OPTIONS -ErrorAction SilentlyContinue
23+
} else {
24+
$env:JAVA_TOOL_OPTIONS = $oldJavaToolOptions
25+
}
26+
27+
if ($null -eq $oldJdwpAdapterEndpoints) {
28+
Remove-Item Env:\JDWP_ADAPTER_ENDPOINTS -ErrorAction SilentlyContinue
29+
} else {
30+
$env:JDWP_ADAPTER_ENDPOINTS = $oldJdwpAdapterEndpoints
31+
}
32+
}

0 commit comments

Comments
 (0)