Use only $nu.env.PWD for getting the current directory (#587)

* Use only $nu.env.PWD for getting current directory

Because setting and reading to/from std::env changes the global state
shich is problematic if we call `cd` from multiple threads (e.g., in a
`par-each` block).

With this change, when engine-q starts, it will either inherit existing
PWD env var, or create a new one from `std::env::current_dir()`.
Otherwise, everything that needs the current directory will get it from
`$nu.env.PWD`. Each spawned external command will get its current
directory per-process which should be thread-safe.

One thing left to do is to patch nu-path for this as well since it uses
`std::env::current_dir()` in its expansions.

* Rename nu-path functions

*_with is not *_relative which should be more descriptive and frees
"with" for use in a followup commit.

* Clone stack every each iter; Fix some commands

Cloning the stack each iteration of `each` makes sure we're not reusing
PWD between iterations.

Some fixes in commands to make them use the new PWD.

* Post-rebase cleanup, fmt, clippy

* Change back _relative to _with in nu-path funcs

Didn't use the idea I had for the new "_with".

* Remove leftover current_dir from rebase

* Add cwd sync at merge_delta()

This makes sure the parser and completer always have up-to-date cwd.

* Always pass absolute path to glob in ls

* Do not allow PWD a relative path; Allow recovery

Makes it possible to recover PWD by proceeding with the REPL cycle.

* Clone stack in each also for byte/string stream

* (WIP) Start moving env variables to engine state

* (WIP) Move env vars to engine state (ugly)

Quick and dirty code.

* (WIP) Remove unused mut and args; Fmt

* (WIP) Fix dataframe tests

* (WIP) Fix missing args after rebase

* (WIP) Clone only env vars, not the whole stack

* (WIP) Add env var clone to `for` loop as well

* Minor edits

* Refactor merge_delta() to include stack merging.

Less error-prone than doing it manually.

* Clone env for each `update` command iteration

* Mark env var hidden only when found in eng. state

* Fix clippt warnings

* Add TODO about env var reading

* Do not clone empty environment in loops

* Remove extra cwd collection

* Split current_dir() into str and path; Fix autocd

* Make completions respect PWD env var
This commit is contained in:
Jakub Žádník
2022-01-05 00:30:34 +02:00
committed by GitHub
parent 8f6843c600
commit 74dcd91cc3
30 changed files with 424 additions and 177 deletions

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{Config, PipelineData, ShellError, Value};
@ -17,69 +18,62 @@ const ENV_SEP: &str = ":";
/// skip errors. This function is called in the main() so we want to keep running, we cannot just
/// exit.
pub fn convert_env_values(
engine_state: &EngineState,
stack: &mut Stack,
engine_state: &mut EngineState,
stack: &Stack,
config: &Config,
) -> Option<ShellError> {
let mut new_env_vars = vec![];
let mut error = None;
for scope in &stack.env_vars {
let mut new_scope = HashMap::new();
let mut new_scope = HashMap::new();
for (name, val) in scope {
if let Some(env_conv) = config.env_conversions.get(name) {
if let Some((block_id, from_span)) = env_conv.from_string {
let val_span = match val.span() {
Ok(sp) => sp,
Err(e) => {
error = error.or(Some(e));
continue;
for (name, val) in &engine_state.env_vars {
if let Some(env_conv) = config.env_conversions.get(name) {
if let Some((block_id, from_span)) = env_conv.from_string {
let val_span = match val.span() {
Ok(sp) => sp,
Err(e) => {
error = error.or(Some(e));
continue;
}
};
let block = engine_state.get_block(block_id);
if let Some(var) = block.signature.get_positional(0) {
let mut stack = stack.collect_captures(&block.captures);
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, val.clone());
}
let result =
eval_block(engine_state, &mut stack, block, PipelineData::new(val_span));
match result {
Ok(data) => {
let val = data.into_value(val_span);
new_scope.insert(name.to_string(), val);
}
};
let block = engine_state.get_block(block_id);
if let Some(var) = block.signature.get_positional(0) {
let mut stack = stack.collect_captures(&block.captures);
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, val.clone());
}
let result = eval_block(
engine_state,
&mut stack,
block,
PipelineData::new(val_span),
);
match result {
Ok(data) => {
let val = data.into_value(val_span);
new_scope.insert(name.to_string(), val);
}
Err(e) => error = error.or(Some(e)),
}
} else {
error = error.or_else(|| {
Some(ShellError::MissingParameter(
"block input".into(),
from_span,
))
});
Err(e) => error = error.or(Some(e)),
}
} else {
new_scope.insert(name.to_string(), val.clone());
error = error.or_else(|| {
Some(ShellError::MissingParameter(
"block input".into(),
from_span,
))
});
}
} else {
new_scope.insert(name.to_string(), val.clone());
}
} else {
new_scope.insert(name.to_string(), val.clone());
}
new_env_vars.push(new_scope);
}
stack.env_vars = new_env_vars;
for (k, v) in new_scope {
engine_state.env_vars.insert(k, v);
}
error
}
@ -89,7 +83,7 @@ pub fn env_to_string(
env_name: &str,
value: Value,
engine_state: &EngineState,
stack: &mut Stack,
stack: &Stack,
config: &Config,
) -> Result<String, ShellError> {
if let Some(env_conv) = config.env_conversions.get(env_name) {
@ -128,10 +122,10 @@ pub fn env_to_string(
/// Translate all environment variables from Values to Strings
pub fn env_to_strings(
engine_state: &EngineState,
stack: &mut Stack,
stack: &Stack,
config: &Config,
) -> Result<HashMap<String, String>, ShellError> {
let env_vars = stack.get_env_vars();
let env_vars = stack.get_env_vars(engine_state);
let mut env_vars_str = HashMap::new();
for (env_name, val) in env_vars {
let val_str = env_to_string(&env_name, val, engine_state, stack, config)?;
@ -140,3 +134,33 @@ pub fn env_to_strings(
Ok(env_vars_str)
}
/// Shorthand for env_to_string() for PWD with custom error
pub fn current_dir_str(engine_state: &EngineState, stack: &Stack) -> Result<String, ShellError> {
let config = stack.get_config()?;
if let Some(pwd) = stack.get_env_var(engine_state, "PWD") {
match env_to_string("PWD", pwd, engine_state, stack, &config) {
Ok(cwd) => {
if Path::new(&cwd).is_absolute() {
Ok(cwd)
} else {
Err(ShellError::LabeledError(
"Invalid current directory".to_string(),
format!("The 'PWD' environment variable must be set to an absolute path. Found: '{}'", cwd)
))
}
}
Err(e) => Err(e),
}
} else {
Err(ShellError::LabeledError(
"Current directory not found".to_string(),
"The environment variable 'PWD' was not found. It is required to define the current directory.".to_string(),
))
}
}
/// Calls current_dir_str() and returns the current directory as a PathBuf
pub fn current_dir(engine_state: &EngineState, stack: &Stack) -> Result<PathBuf, ShellError> {
current_dir_str(engine_state, stack).map(PathBuf::from)
}

View File

@ -8,7 +8,7 @@ use nu_protocol::{
Spanned, Type, Unit, Value, VarId, ENV_VARIABLE_ID,
};
use crate::get_full_help;
use crate::{current_dir_str, get_full_help};
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
match op {
@ -575,15 +575,9 @@ pub fn eval_variable(
// since the env var PWD doesn't exist on all platforms
// lets just get the current directory
if let Ok(current_dir) = std::env::current_dir() {
if let Some(cwd) = current_dir.to_str() {
output_cols.push("cwd".into());
output_vals.push(Value::String {
val: cwd.into(),
span,
})
}
}
let cwd = current_dir_str(engine_state, stack)?;
output_cols.push("cwd".into());
output_vals.push(Value::String { val: cwd, span });
if let Some(home_path) = nu_path::home_dir() {
if let Some(home_path_str) = home_path.to_str() {
@ -886,7 +880,7 @@ pub fn eval_variable(
span,
})
} else if var_id == ENV_VARIABLE_ID {
let env_vars = stack.get_env_vars();
let env_vars = stack.get_env_vars(engine_state);
let env_columns = env_vars.keys();
let env_values = env_vars.values();

View File

@ -1,7 +1,7 @@
mod call_ext;
pub mod column;
mod documentation;
mod env;
pub mod env;
mod eval;
pub use call_ext::CallExt;