Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
aionescu opened this issue Oct 7, 2024 · 1 comment · Fixed by #14118
Closed
Labels
🐛 bug Something isn't working modules needs-triage An issue that hasn't had any proper look parser Issues related to parsing
Milestone

Comments

@aionescu
Copy link
Contributor

aionescu commented Oct 7, 2024

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 6e1e824
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 aionescu added the needs-triage An issue that hasn't had any proper look label Oct 7, 2024
@fdncred fdncred added 🐛 bug Something isn't working modules labels Oct 7, 2024
@kubouch kubouch added the parser Issues related to parsing label Oct 7, 2024
@Jan9103
Copy link
Contributor

Jan9103 commented Oct 18, 2024

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) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working modules needs-triage An issue that hasn't had any proper look parser Issues related to parsing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants