Fix loading the shell environment in Windows. (#3499)

This commit is contained in:
ifengqi 2024-09-13 18:17:45 +08:00 committed by GitHub
parent 513be4377d
commit eafaa138b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 3 deletions

View File

@ -3942,7 +3942,7 @@ pub fn launch() {
} }
/// Uses a login shell to load the correct shell environment for the current user. /// Uses a login shell to load the correct shell environment for the current user.
fn load_shell_env() { pub fn load_shell_env() {
use std::process::Command; use std::process::Command;
use tracing::warn; use tracing::warn;
@ -3969,7 +3969,10 @@ fn load_shell_env() {
command.args(["--login", "-c", "printenv"]); command.args(["--login", "-c", "printenv"]);
#[cfg(windows)] #[cfg(windows)]
command.args(["{ ls env: | foreach { '{0}={1}' -f $_.Name, $_.Value } }"]); command.args(&[
"-Command",
"Get-ChildItem env: | ForEach-Object { \"{0}={1}\" -f $_.Name, $_.Value }",
]);
let env = match command.output() { let env = match command.output() {
Ok(output) => String::from_utf8(output.stdout).unwrap_or_default(), Ok(output) => String::from_utf8(output.stdout).unwrap_or_default(),
@ -3985,7 +3988,8 @@ fn load_shell_env() {
env.split('\n') env.split('\n')
.filter_map(|line| line.split_once('=')) .filter_map(|line| line.split_once('='))
.for_each(|(key, value)| { .for_each(|(key, value)| unsafe {
let value = value.trim_matches('\r');
if let Ok(v) = std::env::var(key) { if let Ok(v) = std::env::var(key) {
if v != value { if v != value {
warn!("Overwriting '{key}', previous value: '{v}', new value '{value}'"); warn!("Overwriting '{key}', previous value: '{v}', new value '{value}'");