nushell icon indicating copy to clipboard operation
nushell copied to clipboard

`export const` with a type signature causes `Internal error: failed to find added variable`

Open aionescu opened this issue 1 year ago • 1 comments

Describe the bug

Declaring an export const with a type signature inside a module results in an Internal error being thrown with the message failed to find added variable.

How to reproduce

At the REPL, try to define the following module:

> module foo { export const FOO: int = 2 }
Error: nu::parser::unknown_state

  × Internal error.
   ╭─[entry #1:1:21]
 1 │ module foo { export const FOO: int = 2 }
   ·                     ─────────┬────────
   ·                              ╰── failed to find added variable
   ╰────

Alternatively, define the module in a file named foo.nu and try to use it from the REPL:

# foo.nu
export const FOO: int = 2
# REPL
> use foo.nu
Error: nu::parser::unknown_state

  × Internal error.
   ╭─[/path/to/foo.nu:1:8]
 1 │ export const FOO: int = 2
   ·        ─────────┬────────
   ·                 ╰── failed to find added variable
   ╰────

The error is only produced if the const has both export and a type signature. The specific type in the signature doesn't seem to matter.

The issue occurs even when running Nu with --no-config-file --no-std-lib.

Expected behavior

I expected module foo to be defined without issue.

Configuration

key value
version 0.98.0
major 0
minor 98
patch 0
branch makepkg
commit_hash 6e1e824473e15eba246e1c43704c5d88fa237a17
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.81.0 (eeb90cda1 2024-09-04) (Arch Linux rust 1:1.81.0-1)
cargo_version cargo 1.81.0 (2dbb1af80 2024-08-20)
build_time 2024-09-18 13:20:08 +00:00
build_rust_channel release
allocator mimalloc
features default, sqlite, trash
installed_plugins

aionescu avatar Oct 07 '24 12:10 aionescu

i identified the issue: at crates/nu-parser/src/parse_keywords.rs:1551 the parsed var_name includes the :.

a patch to verify this (and also a dirty fix):

diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs
index 8ca5249..7816274 100644
--- a/crates/nu-parser/src/parse_keywords.rs
+++ b/crates/nu-parser/src/parse_keywords.rs
@@ -1544,6 +1544,11 @@ pub fn parse_export_in_module(

                 if let Some(var_name_span) = spans.get(2) {
                     let var_name = working_set.get_span_contents(*var_name_span);
+                    let var_name = if var_name.ends_with(&[58]) {
+                        var_name[..var_name.len() - 1].into()
+                    } else {
+                        var_name
+                    };
                     let var_name = trim_quotes(var_name);

                     if let Some(var_id) = working_set.find_variable(var_name) {

Jan9103 avatar Oct 18 '24 12:10 Jan9103