matklad / xshell

Apache License 2.0
695 stars 28 forks source link

Nonexistent current directory results in confusing error message #72

Closed smoelius closed 1 year ago

smoelius commented 1 year ago

I tried this code:

#[test]
fn huh() -> Result<()> {
    let sh = Shell::new()?;
    sh.change_dir("nonexistent");
    cmd!(sh, "which echo").run()?;
    Ok(())
}

And I saw this:

running 1 test
$ which echo
Error: command not found: `which`
test huh ... FAILED

IMHO, the error message should indicate that the current directory does not exist, rather than say "command not found".

Really nice tool, BTW.

matklad commented 1 year ago

Heh, that's a fun one! The core issue here is that running commands requires touching two file-system paths:

So, when we spawn a command and get a NotFound, it can mean either:

and, as far as I can tell, there's no way for us to tell which one was that precisely. I think it makes sense to heuristically detect this case: if spawning a command failed, check if the directory exist and, if it does not, emit a message like the following:

Error: working directory `/bla/bla/bla` does not exist.

This of course is TOCTOU, but better than the status quo.