Reset current working directory at startup
This commit is contained in:
parent
143385ff92
commit
10d4a0d2b6
|
@ -20,7 +20,6 @@ pub use config_files::eval_config_contents;
|
|||
pub use eval_cmds::evaluate_commands;
|
||||
pub use eval_file::evaluate_file;
|
||||
pub use menus::NuHelpCompleter;
|
||||
pub use nu_cmd_base::util::get_init_cwd;
|
||||
pub use nu_highlight::NuHighlight;
|
||||
pub use print::Print;
|
||||
pub use prompt::NushellPrompt;
|
||||
|
|
|
@ -2,21 +2,7 @@ use nu_protocol::{
|
|||
engine::{EngineState, Stack},
|
||||
Range, ShellError, Span, Value,
|
||||
};
|
||||
use std::{ops::Bound, path::PathBuf};
|
||||
|
||||
pub fn get_init_cwd() -> PathBuf {
|
||||
std::env::current_dir().unwrap_or_else(|_| {
|
||||
std::env::var("PWD")
|
||||
.map(Into::into)
|
||||
.unwrap_or_else(|_| nu_path::home_dir().unwrap_or_default())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf {
|
||||
engine_state
|
||||
.cwd(Some(stack))
|
||||
.unwrap_or(crate::util::get_init_cwd())
|
||||
}
|
||||
use std::ops::Bound;
|
||||
|
||||
type MakeRangeError = fn(&str, Span) -> ShellError;
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use nu_cmd_base::util::get_init_cwd;
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_utils::filesystem::{have_permission, PermissionResult};
|
||||
|
||||
|
@ -41,9 +40,11 @@ impl Command for Cd {
|
|||
let physical = call.has_flag(engine_state, stack, "physical")?;
|
||||
let path_val: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
|
||||
|
||||
// If getting PWD failed, default to the initial directory. This way, the
|
||||
// user can use `cd` to recover PWD to a good state.
|
||||
let cwd = engine_state.cwd(Some(stack)).unwrap_or(get_init_cwd());
|
||||
// If getting PWD failed, default to the home directory. The user can
|
||||
// use `cd` to reset PWD to a good state.
|
||||
let cwd = engine_state
|
||||
.cwd(Some(stack))
|
||||
.unwrap_or(nu_path::home_dir().unwrap_or_default());
|
||||
|
||||
let path_val = {
|
||||
if let Some(path) = path_val {
|
||||
|
|
|
@ -58,9 +58,8 @@ impl Command for Start {
|
|||
open_path(url.as_str(), engine_state, stack, path.span)?;
|
||||
} else {
|
||||
// try to distinguish between file not found and opening url without prefix
|
||||
if let Ok(canon_path) =
|
||||
canonicalize_with(path_no_whitespace, std::env::current_dir()?.as_path())
|
||||
{
|
||||
let cwd = engine_state.cwd(Some(stack))?;
|
||||
if let Ok(canon_path) = canonicalize_with(path_no_whitespace, cwd) {
|
||||
open_path(canon_path, engine_state, stack, path.span)?;
|
||||
} else {
|
||||
// open crate does not allow opening URL without prefix
|
||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -21,7 +21,6 @@ use command::gather_commandline_args;
|
|||
use log::{trace, Level};
|
||||
use miette::Result;
|
||||
use nu_cli::gather_parent_env_vars;
|
||||
use nu_cmd_base::util::get_init_cwd;
|
||||
use nu_lsp::LanguageServer;
|
||||
use nu_path::canonicalize_with;
|
||||
use nu_protocol::{
|
||||
|
@ -47,6 +46,27 @@ fn get_engine_state() -> EngineState {
|
|||
nu_explore::add_explore_context(engine_state)
|
||||
}
|
||||
|
||||
/// Get the directory where the Nushell executable is located.
|
||||
fn current_exe_directory() -> PathBuf {
|
||||
let mut path = std::env::current_exe().expect("current_exe() should succeed");
|
||||
path.pop();
|
||||
path
|
||||
}
|
||||
|
||||
/// Get the current working directory from the environment.
|
||||
fn current_dir_from_environment() -> PathBuf {
|
||||
if let Ok(cwd) = std::env::current_dir() {
|
||||
return cwd;
|
||||
}
|
||||
if let Ok(cwd) = std::env::var("PWD") {
|
||||
return cwd.into();
|
||||
}
|
||||
if let Some(home) = nu_path::home_dir() {
|
||||
return home;
|
||||
}
|
||||
current_exe_directory()
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let entire_start_time = std::time::Instant::now();
|
||||
let mut start_time = std::time::Instant::now();
|
||||
|
@ -57,10 +77,11 @@ fn main() -> Result<()> {
|
|||
miette_hook(x);
|
||||
}));
|
||||
|
||||
// Get initial current working directory.
|
||||
let init_cwd = get_init_cwd();
|
||||
let mut engine_state = get_engine_state();
|
||||
|
||||
// Get the current working directory from the environment.
|
||||
let init_cwd = current_dir_from_environment();
|
||||
|
||||
// Custom additions
|
||||
let delta = {
|
||||
let mut working_set = nu_protocol::engine::StateWorkingSet::new(&engine_state);
|
||||
|
@ -328,6 +349,12 @@ fn main() -> Result<()> {
|
|||
_ => std::process::exit(1),
|
||||
}
|
||||
std::process::exit(0)
|
||||
} else {
|
||||
// If we're not running a testbin, set the current working directory to
|
||||
// the location of the Nushell executable. This prevents the OS from
|
||||
// locking the directory where the user launched Nushell.
|
||||
std::env::set_current_dir(current_exe_directory())
|
||||
.expect("set_current_dir() should succeed");
|
||||
}
|
||||
perf(
|
||||
"run test_bins",
|
||||
|
|
Loading…
Reference in New Issue
Block a user