Fix windows shell#1057
Conversation
alexpasmantier
left a comment
There was a problem hiding this comment.
Thanks for taking the time to fix this. The solution looks fine to me, just left a couple of comments I think we should address before merging.
| [[package]] | ||
| name = "television" | ||
| version = "0.15.6" | ||
| version = "0.15.6-hos-fix" |
There was a problem hiding this comment.
I don't think you want to include this in the PR
There was a problem hiding this comment.
you are right , I though i remove it
| Shell::Bash => { | ||
| if cfg!(target_os = "windows") { | ||
| "bash.exe" | ||
| } else { | ||
| "bash" | ||
| } | ||
| } | ||
| Shell::Zsh => { | ||
| if cfg!(target_os = "windows") { | ||
| "zsh.exe" | ||
| } else { | ||
| "zsh" | ||
| } | ||
| } | ||
| Shell::Fish => { | ||
| if cfg!(target_os = "windows") { | ||
| "fish.exe" | ||
| } else { | ||
| "fish" | ||
| } | ||
| } | ||
| Shell::Cmd => { | ||
| if cfg!(target_os = "windows") { | ||
| "cmd.exe" | ||
| } else { | ||
| "cmd" | ||
| } | ||
| } | ||
| Shell::Nu => { | ||
| if cfg!(target_os = "windows") { | ||
| "nu.exe" | ||
| } else { | ||
| "nu" | ||
| } | ||
| } | ||
| Shell::Psh => { | ||
| if cfg!(target_os = "windows") { | ||
| static PSH_EXE: std::sync::OnceLock<&'static str> = | ||
| std::sync::OnceLock::new(); | ||
| PSH_EXE.get_or_init(|| { | ||
| if std::process::Command::new("pwsh.exe") | ||
| .arg("-Version") | ||
| .output() | ||
| .is_ok() | ||
| { | ||
| "pwsh.exe" | ||
| } else { | ||
| "powershell.exe" | ||
| } | ||
| }) | ||
| } else { | ||
| "pwsh" | ||
| } | ||
| } |
There was a problem hiding this comment.
To avoid all that branching, let's just duplicate the executable function and have one for windows and one for unix
There was a problem hiding this comment.
Would a using the which crate and a match be better here?
| Shell::Bash => { | |
| if cfg!(target_os = "windows") { | |
| "bash.exe" | |
| } else { | |
| "bash" | |
| } | |
| } | |
| Shell::Zsh => { | |
| if cfg!(target_os = "windows") { | |
| "zsh.exe" | |
| } else { | |
| "zsh" | |
| } | |
| } | |
| Shell::Fish => { | |
| if cfg!(target_os = "windows") { | |
| "fish.exe" | |
| } else { | |
| "fish" | |
| } | |
| } | |
| Shell::Cmd => { | |
| if cfg!(target_os = "windows") { | |
| "cmd.exe" | |
| } else { | |
| "cmd" | |
| } | |
| } | |
| Shell::Nu => { | |
| if cfg!(target_os = "windows") { | |
| "nu.exe" | |
| } else { | |
| "nu" | |
| } | |
| } | |
| Shell::Psh => { | |
| if cfg!(target_os = "windows") { | |
| static PSH_EXE: std::sync::OnceLock<&'static str> = | |
| std::sync::OnceLock::new(); | |
| PSH_EXE.get_or_init(|| { | |
| if std::process::Command::new("pwsh.exe") | |
| .arg("-Version") | |
| .output() | |
| .is_ok() | |
| { | |
| "pwsh.exe" | |
| } else { | |
| "powershell.exe" | |
| } | |
| }) | |
| } else { | |
| "pwsh" | |
| } | |
| } | |
| Shell::Bash => "bash", | |
| Shell::Zsh => "zsh", | |
| Shell::Fish => "fish", | |
| Shell::Cmd => "cmd", | |
| Shell::Nu => "nu", | |
| Shell::Psh => { | |
| match which("pwsh") { | |
| Ok(path) => "pwsh", | |
| Err(_) => "powershell.exe", | |
| } | |
| } |
There was a problem hiding this comment.
@alexpasmantier it still branching anyway, but if using std::process::Command as @HeyItsGilbert mentioned, this would cut the branch,
@HeyItsGilbert std::process::Command wasn't used anywhere, only used in my update, and you're right it dosen't require the .exe extension,however your update still won't work (I guess you're asuming that crate utils::shell uses std::process::Command, it's not)
|
I'm not sure specifying the extension is needed. https://doc.rust-lang.org/std/process/struct.Command.html#platform-specific-behavior |
📺 PR Description
issue #987 and #1004
panic caused by std::process::Command on Windows throwing program not found
the shell names were missing the .exe extension on Windows.
also implemented a default to
pwsh.exeif available, gracefully falling back topowershell.exe.Checklist