Fix file lookup in parser keywords; Refactor nu_repl (#6185)

* Fix file lookup in parser keywords

* Make nu_repl a testbin; Fix wrong cwd test error
This commit is contained in:
Jakub Žádník
2022-07-29 23:42:00 +03:00
committed by GitHub
parent 7a820b1304
commit d6f4189c7b
10 changed files with 353 additions and 219 deletions

View File

@ -2834,25 +2834,29 @@ pub fn parse_register(
/// This helper function is used to find files during parsing
///
/// Checks whether the file is:
/// 1. relative to the directory of the file currently being parsed
/// 2. relative to the current working directory
/// 3. within one of the NU_LIB_DIRS entries
/// First, the actual current working directory is selected as
/// a) the directory of a file currently being parsed
/// b) current working directory (PWD)
///
/// Always returns absolute path
/// Then, if the file is not found in the actual cwd, NU_LIB_DIRS is checked.
/// If there is a relative path in NU_LIB_DIRS, it is assumed to be relative to the actual cwd
/// determined in the first step.
///
/// Always returns an absolute path
fn find_in_dirs(
filename: &str,
working_set: &StateWorkingSet,
cwd: &str,
dirs_env: &str,
) -> Option<PathBuf> {
if let Some(currently_parsed_cwd) = &working_set.currently_parsed_cwd {
if let Ok(p) = canonicalize_with(filename, currently_parsed_cwd) {
Some(p)
} else {
None
}
} else if let Ok(p) = canonicalize_with(filename, cwd) {
// Choose whether to use file-relative or PWD-relative path
let actual_cwd = if let Some(currently_parsed_cwd) = &working_set.currently_parsed_cwd {
currently_parsed_cwd.as_path()
} else {
Path::new(cwd)
};
if let Ok(p) = canonicalize_with(filename, actual_cwd) {
Some(p)
} else {
let path = Path::new(filename);
@ -2862,8 +2866,8 @@ fn find_in_dirs(
if let Ok(dirs) = lib_dirs.as_list() {
for lib_dir in dirs {
if let Ok(dir) = lib_dir.as_path() {
if let Ok(dir_abs) = canonicalize_with(&dir, cwd) {
// make sure the dir is absolute path
// make sure the dir is absolute path
if let Ok(dir_abs) = canonicalize_with(&dir, actual_cwd) {
if let Ok(path) = canonicalize_with(filename, dir_abs) {
return Some(path);
}

View File

@ -199,7 +199,7 @@ impl Value {
if let Ok(map) = create_map(value, &config) {
config.color_config = map;
} else {
eprintln!("$config.color_config is not a record")
eprintln!("$env.config.color_config is not a record")
}
}
"use_grid_icons" => {
@ -403,7 +403,7 @@ impl Value {
}
}
} else {
eprintln!("$config is not a record");
eprintln!("$env.config is not a record");
}
Ok(config)
@ -412,7 +412,7 @@ impl Value {
fn try_parse_trim_strategy(value: &Value, config: &Config) -> Result<TrimStrategy, ShellError> {
let map = create_map(value, config).map_err(|e| {
eprintln!("$config.table_trim is not a record");
eprintln!("$env.config.table_trim is not a record");
e
})?;

View File

@ -35,6 +35,25 @@ pub fn pipeline(commands: &str) -> String {
.to_string()
}
pub fn nu_repl_code(source_lines: &[&str]) -> String {
let mut out = String::from("nu --testbin=nu_repl [ ");
for line in source_lines.iter() {
// convert each "line" to really be a single line to prevent nu! macro joining the newlines
// with ';'
let line = pipeline(line);
out.push('`');
out.push_str(&line);
out.push('`');
out.push(' ');
}
out.push(']');
out
}
pub fn shell_os_paths() -> Vec<std::path::PathBuf> {
let mut original_paths = vec![];