Skip to content

Conversation

jamesonknutson
Copy link

This PR addresses the problem of the print and inspect 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 prepended env.nu as a temp file, and using that temp file's path and passing it along as --env-config to the nu binary.

This makes it so that the print and inspect commands will check if there is an environment variable named NUSHELL_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:

  • The path to the user's env.nu file is determined by launching the nu 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 a env.nu file. We may want to address that.
  • The text of the user's env.nu file is read into memory, since we need to copy it and prepend to it, in order to shadow the print and inspect commands, while still respecting any changes the user has made to their env.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 in env 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 and inspect commands do not interfere with the LSP's output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant