Skip to content

Commit 33535c5

Browse files
authored
Better error message if env var is used as var (#9522)
# Description This PR improves the error message if an environment variable (that's visible before the parser begins) is used in the form of `$PATH` instead of `$env.PATH`. Before: ``` Error: nu::parser::variable_not_found × Variable not found. ╭─[entry #31:1:1] 1 │ echo $PATH · ──┬── · ╰── variable not found. ╰──── ``` After: ``` Error: nu::parser::env_var_not_var × Use $env.PATH instead of $PATH. ╭─[entry #1:1:1] 1 │ echo $PATH · ──┬── · ╰── use $env.PATH instead of $PATH ╰──── ``` # User-Facing Changes Just the improvement to the error message # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
1 parent b74d508 commit 33535c5

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

crates/nu-parser/src/parser.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,13 +1817,22 @@ pub fn parse_variable_expr(working_set: &mut StateWorkingSet, span: Span) -> Exp
18171817
};
18181818
}
18191819

1820+
let name = if contents.starts_with(b"$") {
1821+
String::from_utf8_lossy(&contents[1..]).to_string()
1822+
} else {
1823+
String::from_utf8_lossy(contents).to_string()
1824+
};
1825+
18201826
if let Some(id) = parse_variable(working_set, span) {
18211827
Expression {
18221828
expr: Expr::Var(id),
18231829
span,
18241830
ty: working_set.get_variable(id).ty.clone(),
18251831
custom_completion: None,
18261832
}
1833+
} else if working_set.get_env_var(&name).is_some() {
1834+
working_set.error(ParseError::EnvVarNotVar(name, span));
1835+
garbage(span)
18271836
} else {
18281837
let ws = &*working_set;
18291838
let suggestion = DidYouMean::new(&ws.list_variables(), ws.get_span_contents(span));

crates/nu-protocol/src/parse_error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ pub enum ParseError {
173173
#[diagnostic(code(nu::parser::variable_not_found))]
174174
VariableNotFound(DidYouMean, #[label = "variable not found. {0}"] Span),
175175

176+
#[error("Use $env.{0} instead of ${0}.")]
177+
#[diagnostic(code(nu::parser::env_var_not_var))]
178+
EnvVarNotVar(String, #[label = "use $env.{0} instead of ${0}"] Span),
179+
176180
#[error("Variable name not supported.")]
177181
#[diagnostic(code(nu::parser::variable_not_valid))]
178182
VariableNotValid(#[label = "variable name can't contain spaces or quotes"] Span),
@@ -492,6 +496,7 @@ impl ParseError {
492496
ParseError::IncorrectValue(_, s, _) => *s,
493497
ParseError::MultipleRestParams(s) => *s,
494498
ParseError::VariableNotFound(_, s) => *s,
499+
ParseError::EnvVarNotVar(_, s) => *s,
495500
ParseError::VariableNotValid(s) => *s,
496501
ParseError::AliasNotValid(s) => *s,
497502
ParseError::CommandDefNotValid(s) => *s,

tests/shell/environment/env.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,11 @@ fn hides_env_in_block() {
175175
assert!(actual.err.contains("column_not_found"));
176176
assert!(actual_repl.err.contains("column_not_found"));
177177
}
178+
179+
#[test]
180+
fn env_var_not_var() {
181+
let actual = nu!(cwd: ".", r#"
182+
echo $CARGO
183+
"#);
184+
assert!(actual.err.contains("use $env.CARGO instead of $CARGO"));
185+
}

0 commit comments

Comments
 (0)