matklad / xshell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nonexistent current directory results in confusing error message

smoelius opened this issue · comments

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.

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

  • the working directory of the future command
  • the commnd itself

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

  • command itself was not found
  • the working directory was not found

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.