-
Notifications
You must be signed in to change notification settings - Fork 605
fix: use bash -c
to get interpretation of tilde and env vars in path
#5756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
0be7457
to
ff0c606
Compare
9615c55
to
1f7afca
Compare
1f7afca
to
006d19c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something you could try is the to use gix-command
for this. It does the no-window thing automatically, and has a lot of tricks to execute correctly even on platforms that aren't really made for executing non-gui programs.
This could get you started…
let gpg_in_sh: std::process::Command = gix::command::prepare(gpg_cmd).with_shell().into()
…to then finalize the command configuration the traditional way.
This is how Git executes things as well.
Okay trying this now. I'm basiclaly passing the finished Command into the gix::command prepare fn so we end up with this: // cmd: "ssh-keygen" "-Y" "sign" "-n" "git" "-f" "$HOME/.ssh/id_ndo4.pub /tmp/.tmpQDYPg7"
let mut gpg_cmd: std::process::Command =
gix::command::prepare(format!("{:?}", cmd))
.with_shell()
.into(); However, this seems to both escape the pre-existing gpg_cmd: "/bin/sh" "-c" "\"ssh-keygen\" \"-Y\" \"sign\" \"-n\" \"git\" \"-f\" \"$HOME/.ssh/id_ndo4.pub /tmp/.tmpQDYPg7\"" "--" Any way to avoid the additional escaping and appending |
006d19c
to
e4bda57
Compare
Is this a problem for execution? I tried to execute it and it appeared to work fine, except that it can't find the files as expected. The Probably I am missing something though, and hope you could clarify. |
e4bda57
to
53bf7fd
Compare
Yeah no so the There doesn't seem to be a way to customize the shell that with_shell() is using, is that correct? |
53bf7fd
to
7d10fd4
Compare
That is correct. It's easy to add though and I am working on it already, but in the middle of that I wondered why Maybe you tried it on Windows? There it performs argument splitting itself if it can do so safely, completely bypassing the shell by default. However, it would refrain from doing so if it finds any of |
Hmm yeah good point, just tried using the A quick triple check on this PR though does confirm:
|
tl;dr: The problem seems to be the extra quotes - can these be removed? I also noticed that there seem to be two files within one quote: I cannot reproduce this on MacOS. In the testsuite, there was this test that I modified to see what happens: let mut cmd = std::process::Command::from(gix_command::prepare("~/bin/exe --foo \"a b\"").with_shell());
assert_eq!(
format!("{cmd:?}"),
format!(r#""{SH}" "-c" "~/bin/exe --foo \"a b\"" "--""#),
"this always needs a shell as we need tilde expansion"
);
dbg!(cmd.output().unwrap().stdout.as_bstr()); And this is the output: let mut cmd = std::process::Command::from(gix_command::prepare("echo \"~/bin/hello\"").with_shell());
dbg!(cmd.output().unwrap().stdout.as_bstr()); … doesn't resolve: The same is true when using let mut cmd = std::process::Command::from(
gix_command::prepare("echo \"~/bin/hello\"")
.with_shell()
.with_shell_program("bash"),
);
dbg!(&cmd);
dbg!(cmd.output().unwrap().stdout.as_bstr()); Which resolves to:
However, it seems to be about the extra quotes on let mut cmd = std::process::Command::from(
gix_command::prepare("echo ~/bin/hello")
.with_shell()
.with_shell_program("bash"),
); …works: And fortunately the same is true with
The problem seems to be the extra quotes - can these be removed? I also noticed that there seem to be two files within one quote: |
Thanks for the deep-dive! So the extra quotes seem to be by default from the rust Regarding the final two paths being wrapped in 1 set of quotes - that's been fixed in the last 1 or 2 commits in this branch already. So the currently, the
And the output of the gix prepare
For context, those come from here: println!("1. CMD: {:?}", cmd);
let mut gpg_cmd: std::process::Command =
gix::command::prepare(format!("{:?}", cmd))
.with_shell()
.into();
println!("2. GPG_CMD: {:?}", gpg_cmd);
gpg_cmd.stderr(Stdio::piped());
gpg_cmd.stdout(Stdio::piped());
gpg_cmd.stdin(Stdio::null());
let child = gpg_cmd.spawn()?;
output = child.wait_with_output()?; |
I've got an alternative here which seems to work well with both let cmd_str = cmd
.get_args()
.map(|arg| arg.to_str().unwrap_or(""))
.collect::<Vec<&str>>()
.join(" ");
let full_cmd =
format!("{} {}", cmd.get_program().to_str().unwrap_or(""), cmd_str);
let mut gpg_cmd: std::process::Command =
gix::command::prepare(full_cmd).with_shell().into();
println!("GPG_CMD3: {:?}", gpg_cmd); Which results in:
|
7d10fd4
to
19c7b60
Compare
Good to hear it works now! Somehow I can't shake the feeling that there is no need at all to go through |
Thanks for the extra push haha, that was a bit convoluted. Works now with both
|
59ed5f1
to
593aa47
Compare
593aa47
to
93aef8f
Compare
c857c4f
to
814c099
Compare
☕️ Reasoning
🧢 Changes
bash -c ...
to execute our&gpg_program
command$SHELL
first, then falls back tosh
if it cant be found