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
printandinspectcommands 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.nufile, then saving that prependedenv.nuas a temp file, and using that temp file's path and passing it along as--env-configto thenubinary.This makes it so that the
printandinspectcommands 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.nufile is determined by launching thenubinary 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.nufile. We may want to address that.env.nufile is read into memory, since we need to copy it and prepend to it, in order to shadow theprintandinspectcommands, while still respecting any changes the user has made to theirenv.nufile. 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 inenvfiles.I originally wanted to try to just pass the shadowed commands via
--commandsor--execute, but I couldn't get that to work. So I used the--env-configtrick instead.This entire PR is made obsolete if nushell's LSP mode is changed upstream, so that the
printandinspectcommands do not interfere with the LSP's output.