nushell / vscode-nushell-lang

A Nushell grammar for Visual Studio Code with IDE support
https://www.nushell.sh/
MIT License
122 stars 27 forks source link

Exported constants from modules are not handled properly #154

Closed kubouch closed 1 year ago

kubouch commented 1 year ago

If you have export const FOO = 'bar' in a module, you'll notice a squiggle under the =. Also, importing the constant and using it generates errors, despite the code itself running correctly.

fdncred commented 1 year ago

I'm guessing that's because I need to do another release. Wait, squiggles aren't dependent on the vscode code. It's calling out to nushell. I wonder if your nushell is setup to an older version. You can check by looking at the extension settings.

On Windows it's ctrl-comma, then scroll down to extensions and then nushell ide support. Mine is set to a debug version. image

kubouch commented 1 year ago

It points to my ~/.config/bin/nu which is the latest Nushell from the main branch that I use every day.

fdncred commented 1 year ago

I was planning on doing a new release anyway. I'll try to get that going soon to see if it fixes it.

fdncred commented 1 year ago

Testing the unreleased version. No red squigglies, yay! image

fdncred commented 1 year ago

@kubouch 1.7.0 is released. It's working good for me so far. When you get a chance, tomorrow or whatever, give it a whirl and let me know if it fixes the problem you were seeing.

kubouch commented 1 year ago

It still happens. If you open code from this PR https://github.com/nushell/nupm/pull/18 and go to nupm/utils/dirs.nu and nupm/mod.nu, you should see the red squiggles related to constants.

fdncred commented 1 year ago

I see what you mean. I can repro. You can see what's going on under the hook in vscode by looking in the bottom pane where the terminal is and choosing the output tab and then choosing 'Nushell Language Server' in the combo box on the right. image

I run the same command in my terminal and this is what I get

❯ /Users/fdncred/.cargo/bin/nu --ide-check 100 -I "/Users/fdncred/src/nupm/nupm/utils/Users/fdncred/Library/Application Support/nushell/scripts/Users/fdncred/src/nu_scripts/Users/fdncred/Library/Application Support/nushell/Users/fdncred/src/nushell/crates/nu-std/lib/" /var/folders/m1/x0gp9jy51s146ttxtz7jlpxh0000gn/T/nushell-81543-csL8QLo30TOb
{"message":"Error: \u001b[31mnu::shell::not_a_constant\u001b[0m\n\n  \u001b[31m×\u001b[0m Not a constant.\n   ╭─[\u001b[36;1;4m/var/folders/m1/x0gp9jy51s146ttxtz7jlpxh0000gn/T/nushell-81543-csL8QLo30TOb\u001b[0m:3:1]\n \u001b[2m3\u001b[0m │ # Default installation path for nupm packages\n \u001b[2m4\u001b[0m │ export const DEFAULT_NUPM_HOME = ($nu.default-config-dir | path join \"nupm\")\n   · \u001b[35;1m                                  ─┬─\u001b[0m\n   ·                                    \u001b[35;1m╰── \u001b[35;1mValue is not a parse-time constant\u001b[0m\u001b[0m\n \u001b[2m5\u001b[0m │ \n   ╰────\n\u001b[36m  help: \u001b[0mOnly a subset of expressions are allowed constants during parsing. Try using the 'const' command or typing the value\n        literally.\n","severity":"Error","span":{"end":128,"start":127},"type":"diagnostic"}
{"message":"Error: \u001b[31mnu::shell::not_a_constant\u001b[0m\n\n  \u001b[31m×\u001b[0m Not a constant.\n   ╭─[\u001b[36;1;4m/var/folders/m1/x0gp9jy51s146ttxtz7jlpxh0000gn/T/nushell-81543-csL8QLo30TOb\u001b[0m:6:1]\n \u001b[2m6\u001b[0m │ # Default temporary path for various nupm purposes\n \u001b[2m7\u001b[0m │ export const DEFAULT_NUPM_TEMP = ($nu.temp-path | path join \"nupm\")\n   · \u001b[35;1m                                  ─┬─\u001b[0m\n   ·                                    \u001b[35;1m╰── \u001b[35;1mValue is not a parse-time constant\u001b[0m\u001b[0m\n \u001b[2m8\u001b[0m │ \n   ╰────\n\u001b[36m  help: \u001b[0mOnly a subset of expressions are allowed constants during parsing. Try using the 'const' command or typing the value\n        literally.\n","severity":"Error","span":{"end":257,"start":256},"type":"diagnostic"}
{"position":{"end":126,"start":109},"type":"hint","typename":"any"}
{"position":{"end":255,"start":238},"type":"hint","typename":"any"}
{"position":{"end":963,"start":957},"type":"hint","typename":"string"}
{"position":{"end":1325,"start":1324},"type":"hint","typename":"any"}
{"position":{"end":1476,"start":1475},"type":"hint","typename":"any"}
{"position":{"end":1640,"start":1639},"type":"hint","typename":"any"}

There are a couple issues here.

  1. This looks like a bug in the nushell code because all the vscode extension does is parse the json output.
  2. There are ansi escape characters here, which there shouldn't be. I'm not sure why but that part could be a bug in the extension or nushell. I can't remember who strips them.

If you want to investigate further, the --ide-check parameter is in ide.rs and unsurprisingly, the check function here https://github.com/fdncred/nushell/blob/main/src/ide.rs#L77-L139. So, something is going wrong in that function. That's my guess.

fdncred commented 1 year ago

I'm looking a little closer to this and it seems that parse() is returning an error. I'm not sure why, but I don't see what else it could be. You may need to look at this yourself to see what you can find. This is a bit over my pay grade.

I reconstructed the error for readability, and I guess it's talking about $nu image

I put some eprintln!()'s around all the places that return a NotAConstant error and none of them print out when calling nu --ide-check which leads me to believe eval_constant() isn't being called. I could be wrong, but that's my guess.

kubouch commented 1 year ago

What might be happening is that the $nu constant is not inserted on Nushell startup when calling the --ide-XXX flags.

kubouch commented 1 year ago

Fixed by https://github.com/nushell/nushell/pull/10470, awesome!