From 896b20f49004831fa2378c5e344f1804cdf35fa6 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Sun, 2 Feb 2020 17:31:05 +0000 Subject: [PATCH 1/2] Hackaround for missing 'env' in termopen and jobstart in neovim --- autoload/vimspector/internal/neojob.vim | 30 ++++++++++++------- autoload/vimspector/internal/neoterm.vim | 38 ++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/autoload/vimspector/internal/neojob.vim b/autoload/vimspector/internal/neojob.vim index f885337..2a87c4c 100644 --- a/autoload/vimspector/internal/neojob.vim +++ b/autoload/vimspector/internal/neojob.vim @@ -44,17 +44,27 @@ function! vimspector#internal#neojob#StartDebugSession( config ) abort return v:false endif - let s:job = jobstart( a:config[ 'command' ], - \ { - \ 'on_stdout': funcref( 's:_OnEvent' ), - \ 'on_stderr': funcref( 's:_OnEvent' ), - \ 'on_exit': funcref( 's:_OnEvent' ), - \ 'cwd': a:config[ 'cwd' ], - \ 'env': a:config[ 'env' ], - \ } - \ ) - " FIXME: env might not work: Missing in neovim 0.4. But in master: + " HACK: Workaround for 'env' not being supported. + + let old_env={} + try + let old_env = vimspector#internal#neoterm#PrepareEnvironment( + \ a:config[ 'env' ] ) + let s:job = jobstart( a:config[ 'command' ], + \ { + \ 'on_stdout': funcref( 's:_OnEvent' ), + \ 'on_stderr': funcref( 's:_OnEvent' ), + \ 'on_exit': funcref( 's:_OnEvent' ), + \ 'cwd': a:config[ 'cwd' ], + \ 'env': a:config[ 'env' ], + \ } + \ ) + finally + call vimspector#internal#neoterm#ResetEnvironment( a:config[ 'env' ], + \ old_env ) + endtry + return v:true endfunction diff --git a/autoload/vimspector/internal/neoterm.vim b/autoload/vimspector/internal/neoterm.vim index ab71b2f..627f570 100644 --- a/autoload/vimspector/internal/neoterm.vim +++ b/autoload/vimspector/internal/neoterm.vim @@ -25,6 +25,27 @@ set cpoptions&vim " FIXME: Tidy this map when buffers are closed ? let s:buffer_to_id = {} +function! vimspector#internal#neoterm#PrepareEnvironment( env ) abort + let old_env = {} + + let new_env = copy( environ() ) + for key in keys( a:env ) + if has_key( new_env, key ) + let old_env[ key ] = new_env[ key ] + endif + call setenv( key, a:env[ key ] ) + endfor + + return old_env +endfunction + +function! vimspector#internal#neoterm#ResetEnvironment( env, old_env ) abort + for key in keys( a:env ) + let value = get( a:old_env, key, v:null ) + call setenv( key, value ) + endfor +endfunction + function! vimspector#internal#neoterm#Start( cmd, opts ) abort if ! get( a:opts, 'curwin', 0 ) if get( a:opts, 'vertical', 0 ) @@ -34,8 +55,21 @@ function! vimspector#internal#neoterm#Start( cmd, opts ) abort endif endif - " FIXME: 'env' doesn't work - let id = termopen( a:cmd, { 'cwd': a:opts[ 'cwd' ] } ) + " HACK: Neovim's termopen doesn't support env + + let old_env={} + try + let old_env = vimspector#internal#neoterm#PrepareEnvironment( + \ a:opts[ 'env' ] ) + let id = termopen( a:cmd, { + \ 'cwd': a:opts[ 'cwd' ], + \ 'env': a:opts[ 'env' ], + \ } ) + finally + call vimspector#internal#neoterm#ResetEnvironment( a:opts[ 'env' ], + \ old_env ) + endtry + let bufnr = bufnr() let s:buffer_to_id[ bufnr ] = id return bufnr From 6878c80cfbfe993fe3a2213c98d5762f588a373e Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Sun, 2 Feb 2020 17:33:33 +0000 Subject: [PATCH 2/2] Update readme for environment hackaround --- README.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/README.md b/README.md index 76df4a7..1811a04 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,6 @@ neovim doesn't implement some features Vimspector relies on: the output window's current output. * Prompt Buffers - used to send commands in the Console and add Watches * Balloons - used to display the values of variables when debugging. -* Environment variables when laucnhing debugee in embedded terminal. Workarounds are in place as follows: @@ -187,9 +186,6 @@ Workarounds are in place as follows: There is no workaroud for the lack of balloons; you'll just have to use `:VimspectorEval` or `:VimspectorWatch`, or switch to Vim. -The only workaround for the missing environment variables feature is to use -neovim master (it doesn't work in neovim 0.4). - ## Language dependencies The debug adapters themselves have certain runtime dependencies: @@ -732,10 +728,6 @@ session. ### Alternative: Use debugpy directly -*** NOTE: This solution does not work in NeoVim 0.4 due to missing environment -variables support when launching a terminal. Do not raise issues about this if -you are using NeoVim 0.4. *** - If you can't get a node 10 environment set up for whatver reason, then you can avoid that issue by using `debugpy` (formerly `ptvsd`) directly.