mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 18:15:04 +02:00
Set current working directory at startup (#12953)
This PR sets the current working directory to the location of the Nushell executable at startup, using `std::env::set_current_dir()`. This is desirable because after PR https://github.com/nushell/nushell/pull/12922, we no longer change our current working directory even after `cd` is executed, and some OS might lock the directory where Nushell started. The location of the Nushell executable is chosen because it cannot be removed while Nushell is running anyways, so we don't have to worry about OS locking it. This PR has the side effect that it breaks buggy command even harder. I'll keep this PR as a draft until these commands are fixed, but it might be helpful to pull this PR if you're working on fixing one of those bugs. --------- Co-authored-by: Devyn Cairns <devyn.cairns@gmail.com> Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
@ -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,12 +40,14 @@ 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.
|
||||
// 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))
|
||||
.ok()
|
||||
.unwrap_or_else(get_init_cwd);
|
||||
.or_else(nu_path::home_dir)
|
||||
.map(|path| path.into_std_path_buf())
|
||||
.unwrap_or_default();
|
||||
|
||||
let path_val = {
|
||||
if let Some(path) = path_val {
|
||||
@ -65,7 +66,7 @@ impl Command for Cd {
|
||||
if let Some(oldpwd) = stack.get_env_var(engine_state, "OLDPWD") {
|
||||
oldpwd.to_path()?
|
||||
} else {
|
||||
cwd.into()
|
||||
cwd
|
||||
}
|
||||
} else {
|
||||
// Trim whitespace from the end of path.
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user