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:
YizhePKU
2024-09-26 02:04:26 +08:00
committed by GitHub
parent 54e9aa92bc
commit 13df0af514
16 changed files with 91 additions and 127 deletions

View File

@ -17,7 +17,7 @@ use nu_path::AbsolutePathBuf;
use std::{
collections::HashMap,
num::NonZeroUsize,
path::{Path, PathBuf},
path::PathBuf,
sync::{
atomic::{AtomicBool, AtomicU32, Ordering},
Arc, Mutex, MutexGuard, PoisonError,
@ -307,11 +307,7 @@ impl EngineState {
}
/// Merge the environment from the runtime Stack into the engine state
pub fn merge_env(
&mut self,
stack: &mut Stack,
cwd: impl AsRef<Path>,
) -> Result<(), ShellError> {
pub fn merge_env(&mut self, stack: &mut Stack) -> Result<(), ShellError> {
for mut scope in stack.env_vars.drain(..) {
for (overlay_name, mut env) in Arc::make_mut(&mut scope).drain() {
if let Some(env_vars) = Arc::make_mut(&mut self.env_vars).get_mut(&overlay_name) {
@ -324,9 +320,6 @@ impl EngineState {
}
}
// TODO: better error
std::env::set_current_dir(cwd)?;
if let Some(config) = stack.config.take() {
// If config was updated in the stack, replace it.
self.config = config;