Fix STDOUT leaking into LSP messages #217
Open
+340
−26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses the problem of the
print
andinspect
commands leaking their output to the LSP, causing their outputs to be misconstrued as LSP messages.It does so by prepending shadowed definitions for these commands to a copy of the user's
env.nu
file, then saving that prependedenv.nu
as a temp file, and using that temp file's path and passing it along as--env-config
to thenu
binary.This makes it so that the
print
andinspect
commands will check if there is an environment variable namedNUSHELL_LSP
, and if it's value is one of[ 1, '1', true, 'true' ]
. If so, those commands will not print, thereby solving the problem of those commands leaking their output to STDOUT / the LSP.Some notes:
env.nu
file is determined by launching thenu
binary with--commands '$nu | select -o env-path | to json
, then parsing the output. This might be unnecessary complexity. It also may fail, if the user does not have aenv.nu
file. We may want to address that.env.nu
file is read into memory, since we need to copy it and prepend to it, in order to shadow theprint
andinspect
commands, while still respecting any changes the user has made to theirenv.nu
file. Some might object ot the text being read into memory, although we do not do anything with it other than copy it to a temp file-- it's not uncommon for dev secrets to be exposed in plain text inenv
files.I originally wanted to try to just pass the shadowed commands via
--commands
or--execute
, but I couldn't get that to work. So I used the--env-config
trick instead.This entire PR is made obsolete if nushell's LSP mode is changed upstream, so that the
print
andinspect
commands do not interfere with the LSP's output.