Skip to content

normalize special characters in module names to allow variable access - #14353

Merged
WindSoilder merged 1 commit into
nushell:mainfrom
sgvictorino:stricter-module-name-parsing
Dec 5, 2024
Merged

WindSoilder merged 1 commit into
nushell:mainfrom
sgvictorino:stricter-module-name-parsing

Conversation

@sgvictorino

@sgvictorino sgvictorino commented Nov 15, 2024 •

Copy link
Copy Markdown
Contributor

Fixes #14252

User-Facing Changes

  • Special characters in module names are replaced with underscores when
    importing constants, preventing "expected valid variable name":
> module foo-bar { export const baz = 1 }
> use foo-bar
> $foo_bar.baz
  • "expected valid variable name" errors now include a suggestion list:
> module foo-bar { export const baz = 1 }
> use foo-bar
> $foo-bar
Error: nu::parser::parse_mismatch_with_did_you_mean

  × Parse mismatch during operation.
   ╭─[entry #1:1:1]
 1 │ $foo-bar;
   · ────┬───
   ·     ╰── expected valid variable name. Did you mean '$foo_bar'?
   ╰────

@sholderbach sholderbach added notes:breaking-changes Noted in Breaking Changes section deprecated:pr-language language changes labels Nov 15, 2024
@fdncred

fdncred commented Nov 15, 2024

Copy link
Copy Markdown
Contributor

@sgvictorino is on fire 🔥!!! We appreciate all your PRs. Thanks.

@WindSoilder

Copy link
Copy Markdown
Contributor

Thanks! But there is one thing I'm not sure:

module foo-bar { export const baz = 1 }
use foo-bar
$foo-bar.baz

Personally I think it's better to export use baz variable like $foo_bar.baz, to user, after using foo-bar and using it with $ sign, it's using more like a variable rather than a module name.

@sholderbach

Copy link
Copy Markdown
Member

How will this play out with use <filename>.nu modules?

@sgvictorino

sgvictorino commented Nov 17, 2024 •

Copy link
Copy Markdown
Contributor Author

How will this play out with use <filename>.nu modules?

The filename stem must be a valid module name. Out of the invalid identifier characters, someone might try to name files like foo.bar.nu, but fully qualified access would already be ambiguous with a cell path on $foo.

I'm also fine with normalizing certain symbols to a valid variable name like @WindSoilder prefers. The "did you mean" fuzzy search helps with that UX.

@132ikl

132ikl commented Nov 19, 2024

Copy link
Copy Markdown
Member

I think normalizing would be the best option, since then it can align with typical variable name rules and can possibly catch more cases than just hyphens and the dot issue you bring up.

@WindSoilder

Copy link
Copy Markdown
Contributor

Yeah, @sgvictorino can you update the code to use $foo_bar.baz?

@sgvictorino sgvictorino changed the title make module name parsing more strict normalize special characters in module names to allow variable access Nov 22, 2024

@WindSoilder WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for a delay. Finally I take time to review it.

And I think the solution can be easier.

Comment thread crates/nu-protocol/src/module.rs Resolved

@WindSoilder WindSoilder left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just one thing left.

Comment thread crates/nu-protocol/src/module.rs Resolved
# User-Facing Changes

- Special characters in module names are replaced with underscores when
  importing constants, preventing "expected valid variable name":

```nushell
> module foo-bar { export const baz = 1 }
> use foo-bar
> $foo_bar.baz
```

- "expected valid variable name" errors now include a suggestion list:

```nushell
> module foo-bar { export const baz = 1 }
> use foo-bar
> $foo-bar
Error: nu::parser::parse_mismatch_with_did_you_mean

  × Parse mismatch during operation.
   ╭─[entry #1:1:1]
 1 │ $foo-bar;
   · ────┬───
   ·     ╰── expected valid variable name. Did you mean '$foo_bar'?
   ╰────
```

@WindSoilder WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! LGTM

@WindSoilder
WindSoilder merged commit 234484b into nushell:main Dec 5, 2024
@github-actions github-actions Bot added this to the v0.101.0 milestone Dec 5, 2024
@sgvictorino
sgvictorino deleted the stricter-module-name-parsing branch December 13, 2024 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecated:pr-language language changes notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exported constants in modules with a dash in the name aren't accessible

5 participants