Revert "Allow NU_LIBS_DIR and friends to be const" (#8501)

Reverts nushell/nushell#8310

In anticipation that we may want to revert this PR. I'm starting the
process because of this issue.

This stopped working
```
let-env NU_LIB_DIRS = [
    ($nu.config-path | path dirname | path join 'scripts')
    'C:\Users\username\source\repos\forks\nu_scripts'
    ($nu.config-path | path dirname)
]
```
You have to do this now instead.
```
const NU_LIB_DIRS = [
    'C:\Users\username\AppData\Roaming\nushell\scripts'
    'C:\Users\username\source\repos\forks\nu_scripts'
    'C:\Users\username\AppData\Roaming\nushell'
]
```

In talking with @kubouch, he was saying that the `let-env` version
should keep working. Hopefully it's a small change.
This commit is contained in:
Darren Schroeder
2023-03-17 09:33:24 -05:00
committed by GitHub
parent eb2e2e6370
commit ef7fbf4bf9
13 changed files with 116 additions and 264 deletions

View File

@ -39,7 +39,7 @@ pub trait CallExt {
&self,
engine_state: &EngineState,
stack: &mut Stack,
name: &str,
pos: usize,
) -> Result<T, ShellError>;
}
@ -111,9 +111,9 @@ impl CallExt for Call {
&self,
engine_state: &EngineState,
stack: &mut Stack,
name: &str,
pos: usize,
) -> Result<T, ShellError> {
if let Some(expr) = self.get_parser_info(name) {
if let Some(expr) = self.parser_info_nth(pos) {
let result = eval_expression(engine_state, stack, expr)?;
FromValue::from_value(&result)
} else if self.parser_info.is_empty() {

View File

@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use nu_protocol::ast::{Call, Expr, PathMember};
use nu_protocol::ast::PathMember;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Span, Value, VarId};
use nu_protocol::{PipelineData, ShellError, Span, Value};
use nu_path::canonicalize_with;
@ -18,6 +18,8 @@ const ENV_PATH_NAME: &str = "PATH";
const ENV_CONVERSIONS: &str = "ENV_CONVERSIONS";
static LIB_DIRS_ENV: &str = "NU_LIB_DIRS";
enum ConversionResult {
Ok(Value),
ConversionError(ShellError), // Failure during the conversion itself
@ -222,17 +224,6 @@ pub fn path_str(
env_to_string(pathname, &pathval, engine_state, stack)
}
pub const DIR_VAR_PARSER_INFO: &str = "dirs_var";
pub fn get_dirs_var_from_call(call: &Call) -> Option<VarId> {
call.get_parser_info(DIR_VAR_PARSER_INFO).and_then(|x| {
if let Expr::Var(id) = x.expr {
Some(id)
} else {
None
}
})
}
/// This helper function is used to find files during eval
///
/// First, the actual current working directory is selected as
@ -248,7 +239,6 @@ pub fn find_in_dirs_env(
filename: &str,
engine_state: &EngineState,
stack: &Stack,
dirs_var: Option<VarId>,
) -> Result<Option<PathBuf>, ShellError> {
// Choose whether to use file-relative or PWD-relative path
let cwd = if let Some(pwd) = stack.get_env_var(engine_state, "FILE_PWD") {
@ -272,32 +262,36 @@ pub fn find_in_dirs_env(
current_dir_str(engine_state, stack)?
};
Ok((|| -> Option<PathBuf> {
if let Ok(p) = canonicalize_with(filename, &cwd) {
return Some(p);
}
if let Ok(p) = canonicalize_with(filename, &cwd) {
Ok(Some(p))
} else {
let path = Path::new(filename);
if !path.is_relative() {
return None;
if path.is_relative() {
if let Some(lib_dirs) = stack.get_env_var(engine_state, LIB_DIRS_ENV) {
if let Ok(dirs) = lib_dirs.as_list() {
for lib_dir in dirs {
if let Ok(dir) = lib_dir.as_path() {
// make sure the dir is absolute path
if let Ok(dir_abs) = canonicalize_with(dir, &cwd) {
if let Ok(path) = canonicalize_with(filename, dir_abs) {
return Ok(Some(path));
}
}
}
}
Ok(None)
} else {
Ok(None)
}
} else {
Ok(None)
}
} else {
Ok(None)
}
let lib_dirs = dirs_var.and_then(|dirs_var| engine_state.find_constant(dirs_var, &[]));
// TODO: remove (see #8310)
let lib_dirs_fallback = stack.get_env_var(engine_state, "NU_LIB_DIRS");
let lib_dirs = lib_dirs.or(lib_dirs_fallback.as_ref())?;
lib_dirs
.as_list()
.ok()?
.iter()
.map(|lib_dir| -> Option<PathBuf> {
let dir = lib_dir.as_path().ok()?;
let dir_abs = canonicalize_with(dir, &cwd).ok()?;
canonicalize_with(filename, dir_abs).ok()
})
.find(Option::is_some)
.flatten()
})())
}
}
fn get_converted_value(

View File

@ -828,7 +828,7 @@ pub fn eval_element_with_input(
],
redirect_stdout: false,
redirect_stderr: false,
parser_info: HashMap::new(),
parser_info: vec![],
},
input,
)
@ -899,7 +899,7 @@ pub fn eval_element_with_input(
],
redirect_stdout: false,
redirect_stderr: false,
parser_info: HashMap::new(),
parser_info: vec![],
},
input,
)