util: Fix shell environment fetching with cmd (#42093)
Release Notes: - Fixed shell environment fetching failing when having `cmd` configured as terminal shell
This commit is contained in:
@@ -333,60 +333,51 @@ async fn load_directory_shell_environment(
|
|||||||
.into()
|
.into()
|
||||||
};
|
};
|
||||||
|
|
||||||
if cfg!(target_os = "windows") {
|
let (shell, args) = shell.program_and_args();
|
||||||
|
let mut envs = util::shell_env::capture(shell.clone(), args, abs_path)
|
||||||
|
.await
|
||||||
|
.with_context(|| {
|
||||||
|
tx.unbounded_send("Failed to load environment variables".into())
|
||||||
|
.ok();
|
||||||
|
format!("capturing shell environment with {shell:?}")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if cfg!(target_os = "windows")
|
||||||
|
&& let Some(path) = envs.remove("Path")
|
||||||
|
{
|
||||||
|
// windows env vars are case-insensitive, so normalize the path var
|
||||||
|
// so we can just assume `PATH` in other places
|
||||||
|
envs.insert("PATH".into(), path);
|
||||||
|
}
|
||||||
|
// If the user selects `Direct` for direnv, it would set an environment
|
||||||
|
// variable that later uses to know that it should not run the hook.
|
||||||
|
// We would include in `.envs` call so it is okay to run the hook
|
||||||
|
// even if direnv direct mode is enabled.
|
||||||
|
let direnv_environment = match load_direnv {
|
||||||
|
DirenvSettings::ShellHook => None,
|
||||||
// Note: direnv is not available on Windows, so we skip direnv processing
|
// Note: direnv is not available on Windows, so we skip direnv processing
|
||||||
// and just return the shell environment
|
// and just return the shell environment
|
||||||
let (shell, args) = shell.program_and_args();
|
DirenvSettings::Direct if cfg!(target_os = "windows") => None,
|
||||||
let mut envs = util::shell_env::capture(shell.clone(), args, abs_path)
|
DirenvSettings::Direct => load_direnv_environment(&envs, &dir)
|
||||||
.await
|
.await
|
||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
tx.unbounded_send("Failed to load environment variables".into())
|
tx.unbounded_send("Failed to load direnv environment".into())
|
||||||
.ok();
|
.ok();
|
||||||
format!("capturing shell environment with {shell:?}")
|
"load direnv environment"
|
||||||
})?;
|
})
|
||||||
if let Some(path) = envs.remove("Path") {
|
.log_err(),
|
||||||
// windows env vars are case-insensitive, so normalize the path var
|
};
|
||||||
// so we can just assume `PATH` in other places
|
if let Some(direnv_environment) = direnv_environment {
|
||||||
envs.insert("PATH".into(), path);
|
for (key, value) in direnv_environment {
|
||||||
}
|
if let Some(value) = value {
|
||||||
Ok(envs)
|
envs.insert(key, value);
|
||||||
} else {
|
} else {
|
||||||
let (shell, args) = shell.program_and_args();
|
envs.remove(&key);
|
||||||
let mut envs = util::shell_env::capture(shell.clone(), args, abs_path)
|
|
||||||
.await
|
|
||||||
.with_context(|| {
|
|
||||||
tx.unbounded_send("Failed to load environment variables".into())
|
|
||||||
.ok();
|
|
||||||
format!("capturing shell environment with {shell:?}")
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// If the user selects `Direct` for direnv, it would set an environment
|
|
||||||
// variable that later uses to know that it should not run the hook.
|
|
||||||
// We would include in `.envs` call so it is okay to run the hook
|
|
||||||
// even if direnv direct mode is enabled.
|
|
||||||
let direnv_environment = match load_direnv {
|
|
||||||
DirenvSettings::ShellHook => None,
|
|
||||||
DirenvSettings::Direct => load_direnv_environment(&envs, &dir)
|
|
||||||
.await
|
|
||||||
.with_context(|| {
|
|
||||||
tx.unbounded_send("Failed to load direnv environment".into())
|
|
||||||
.ok();
|
|
||||||
"load direnv environment"
|
|
||||||
})
|
|
||||||
.log_err(),
|
|
||||||
};
|
|
||||||
if let Some(direnv_environment) = direnv_environment {
|
|
||||||
for (key, value) in direnv_environment {
|
|
||||||
if let Some(value) = value {
|
|
||||||
envs.insert(key, value);
|
|
||||||
} else {
|
|
||||||
envs.remove(&key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(envs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(envs)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_direnv_environment(
|
async fn load_direnv_environment(
|
||||||
|
|||||||
+67
-111
@@ -136,124 +136,80 @@ async fn capture_windows(
|
|||||||
std::env::current_exe().context("Failed to determine current zed executable path.")?;
|
std::env::current_exe().context("Failed to determine current zed executable path.")?;
|
||||||
|
|
||||||
let shell_kind = ShellKind::new(shell_path, true);
|
let shell_kind = ShellKind::new(shell_path, true);
|
||||||
let env_output = match shell_kind {
|
if let ShellKind::Posix
|
||||||
|
| ShellKind::Csh
|
||||||
|
| ShellKind::Tcsh
|
||||||
|
| ShellKind::Rc
|
||||||
|
| ShellKind::Fish
|
||||||
|
| ShellKind::Xonsh = shell_kind
|
||||||
|
{
|
||||||
|
return Err(anyhow::anyhow!("unsupported shell kind"));
|
||||||
|
}
|
||||||
|
let mut cmd = crate::command::new_smol_command(shell_path);
|
||||||
|
let cmd = match shell_kind {
|
||||||
ShellKind::Posix
|
ShellKind::Posix
|
||||||
| ShellKind::Csh
|
| ShellKind::Csh
|
||||||
| ShellKind::Tcsh
|
| ShellKind::Tcsh
|
||||||
| ShellKind::Rc
|
| ShellKind::Rc
|
||||||
| ShellKind::Fish
|
| ShellKind::Fish
|
||||||
| ShellKind::Xonsh => {
|
| ShellKind::Xonsh => {
|
||||||
return Err(anyhow::anyhow!("unsupported shell kind"));
|
unreachable!()
|
||||||
}
|
}
|
||||||
ShellKind::PowerShell => {
|
ShellKind::PowerShell => cmd.args([
|
||||||
let output = crate::command::new_smol_command(shell_path)
|
"-NonInteractive",
|
||||||
.args([
|
"-NoProfile",
|
||||||
"-NonInteractive",
|
"-Command",
|
||||||
"-NoProfile",
|
&format!(
|
||||||
"-Command",
|
"Set-Location '{}'; & '{}' --printenv",
|
||||||
&format!(
|
directory.display(),
|
||||||
"Set-Location '{}'; & '{}' --printenv",
|
zed_path.display()
|
||||||
directory.display(),
|
),
|
||||||
zed_path.display()
|
]),
|
||||||
),
|
ShellKind::Elvish => cmd.args([
|
||||||
])
|
"-c",
|
||||||
.stdin(Stdio::null())
|
&format!(
|
||||||
.stdout(Stdio::piped())
|
"cd '{}'; {} --printenv",
|
||||||
.stderr(Stdio::piped())
|
directory.display(),
|
||||||
.output()
|
zed_path.display()
|
||||||
.await?;
|
),
|
||||||
|
]),
|
||||||
anyhow::ensure!(
|
ShellKind::Nushell => cmd.args([
|
||||||
output.status.success(),
|
"-c",
|
||||||
"PowerShell command failed with {}. stdout: {:?}, stderr: {:?}",
|
&format!(
|
||||||
output.status,
|
"cd '{}'; {}'{}' --printenv",
|
||||||
String::from_utf8_lossy(&output.stdout),
|
directory.display(),
|
||||||
String::from_utf8_lossy(&output.stderr),
|
shell_kind
|
||||||
);
|
.command_prefix()
|
||||||
output
|
.map(|prefix| prefix.to_string())
|
||||||
}
|
.unwrap_or_default(),
|
||||||
ShellKind::Elvish => {
|
zed_path.display()
|
||||||
let output = crate::command::new_smol_command(shell_path)
|
),
|
||||||
.args([
|
]),
|
||||||
"-c",
|
ShellKind::Cmd => cmd.args([
|
||||||
&format!(
|
"/c",
|
||||||
"cd '{}'; {} --printenv",
|
"cd",
|
||||||
directory.display(),
|
&directory.display().to_string(),
|
||||||
zed_path.display()
|
"&&",
|
||||||
),
|
&zed_path.display().to_string(),
|
||||||
])
|
"--printenv",
|
||||||
.stdin(Stdio::null())
|
]),
|
||||||
.stdout(Stdio::piped())
|
}
|
||||||
.stderr(Stdio::piped())
|
.stdin(Stdio::null())
|
||||||
.output()
|
.stdout(Stdio::piped())
|
||||||
.await?;
|
.stderr(Stdio::piped());
|
||||||
|
let output = cmd
|
||||||
anyhow::ensure!(
|
.output()
|
||||||
output.status.success(),
|
.await
|
||||||
"Elvish command failed with {}. stdout: {:?}, stderr: {:?}",
|
.with_context(|| format!("command {cmd:?}"))?;
|
||||||
output.status,
|
anyhow::ensure!(
|
||||||
String::from_utf8_lossy(&output.stdout),
|
output.status.success(),
|
||||||
String::from_utf8_lossy(&output.stderr),
|
"Command {cmd:?} failed with {}. stdout: {:?}, stderr: {:?}",
|
||||||
);
|
output.status,
|
||||||
output
|
String::from_utf8_lossy(&output.stdout),
|
||||||
}
|
String::from_utf8_lossy(&output.stderr),
|
||||||
ShellKind::Nushell => {
|
);
|
||||||
let output = crate::command::new_smol_command(shell_path)
|
// "cmd" "/c" "cd \'C:\\Workspace\\salsa\\\'; \'C:\\Workspace\\zed\\zed\\target\\debug\\zed.exe\' --printenv"
|
||||||
.args([
|
let env_output = String::from_utf8_lossy(&output.stdout);
|
||||||
"-c",
|
|
||||||
&format!(
|
|
||||||
"cd '{}'; {}'{}' --printenv",
|
|
||||||
directory.display(),
|
|
||||||
shell_kind
|
|
||||||
.command_prefix()
|
|
||||||
.map(|prefix| prefix.to_string())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
zed_path.display()
|
|
||||||
),
|
|
||||||
])
|
|
||||||
.stdin(Stdio::null())
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.stderr(Stdio::piped())
|
|
||||||
.output()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
anyhow::ensure!(
|
|
||||||
output.status.success(),
|
|
||||||
"Nushell command failed with {}. stdout: {:?}, stderr: {:?}",
|
|
||||||
output.status,
|
|
||||||
String::from_utf8_lossy(&output.stdout),
|
|
||||||
String::from_utf8_lossy(&output.stderr),
|
|
||||||
);
|
|
||||||
output
|
|
||||||
}
|
|
||||||
ShellKind::Cmd => {
|
|
||||||
let output = crate::command::new_smol_command(shell_path)
|
|
||||||
.args([
|
|
||||||
"/c",
|
|
||||||
&format!(
|
|
||||||
"cd '{}'; '{}' --printenv",
|
|
||||||
directory.display(),
|
|
||||||
zed_path.display()
|
|
||||||
),
|
|
||||||
])
|
|
||||||
.stdin(Stdio::null())
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.stderr(Stdio::piped())
|
|
||||||
.output()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
anyhow::ensure!(
|
|
||||||
output.status.success(),
|
|
||||||
"Cmd command failed with {}. stdout: {:?}, stderr: {:?}",
|
|
||||||
output.status,
|
|
||||||
String::from_utf8_lossy(&output.stdout),
|
|
||||||
String::from_utf8_lossy(&output.stderr),
|
|
||||||
);
|
|
||||||
output
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let env_output = String::from_utf8_lossy(&env_output.stdout);
|
|
||||||
|
|
||||||
// Parse the JSON output from zed --printenv
|
// Parse the JSON output from zed --printenv
|
||||||
serde_json::from_str(&env_output)
|
serde_json::from_str(&env_output)
|
||||||
|
|||||||
Reference in New Issue
Block a user