Skip to content
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

Improve error message on bad working directory #2317

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub(crate) enum Error<'src> {
Io {
recipe: &'src str,
io_error: io::Error,
shell: String,
working_directory_io_error: Option<io::Error>,
working_directory: Option<PathBuf>,
},
Load {
path: PathBuf,
Expand Down Expand Up @@ -396,12 +399,15 @@ impl<'src> ColorDisplay for Error<'src> {
write!(f, "Internal runtime error, this may indicate a bug in just: {message} \
consider filing an issue: https://github.com/casey/just/issues/new")?;
}
Io { recipe, io_error } => {
match io_error.kind() {
io::ErrorKind::NotFound => write!(f, "Recipe `{recipe}` could not be run because just could not find the shell: {io_error}"),
io::ErrorKind::PermissionDenied => write!(f, "Recipe `{recipe}` could not be run because just could not run the shell: {io_error}"),
_ => write!(f, "Recipe `{recipe}` could not be run because of an IO error while launching the shell: {io_error}"),
}?;
Io { recipe, io_error, shell, working_directory_io_error, working_directory } => {
write!(f, "Failed to run recipe `{recipe}`:\n Failed to run shell `{shell}`:\n {io_error}", )?;
if let Some(working_directory_io_error) = working_directory_io_error {
let working_directory = working_directory.as_ref().expect("is Some when error is Some").display();
write!(f, "\n Failed to set working directory to `{working_directory}`")?;
if working_directory_io_error.to_string() != io_error.to_string() {
write!(f, ":\n {working_directory_io_error}")?;
}
}
}
Load { io_error, path } => {
write!(f, "Failed to read justfile at `{}`: {io_error}", path.display())?;
Expand Down
14 changes: 14 additions & 0 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,23 @@ impl<'src, D> Recipe<'src, D> {
}
}
Err(io_error) => {
let working_directory = self.working_directory(context);
let working_directory_io_error = match &working_directory {
Some(working_directory) => match fs::read_dir(working_directory) {
Ok(_) => None,
Err(io_error) => Some(io_error),
},
// no working directory = no attempt to change current working directory = no error
None => None,
};
let (shell, _) = context.module.settings.shell(config);

return Err(Error::Io {
recipe: self.name(),
io_error,
shell: shell.into(),
working_directory_io_error,
working_directory,
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn recipe_shell_not_found_error_message() {
.shell(false)
.args(["--shell", "NOT_A_REAL_SHELL"])
.stderr_regex(
"error: Recipe `foo` could not be run because just could not find the shell: .*\n",
".*Failed to run recipe `foo`:\n Failed to run shell `NOT_A_REAL_SHELL`:\n .*",
)
.status(1)
.run();
Expand Down
8 changes: 7 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {super::*, pretty_assertions::assert_eq};
use {super::*, fs::Permissions, pretty_assertions::assert_eq};

macro_rules! test {
{
Expand Down Expand Up @@ -99,6 +99,12 @@ impl Test {
self
}

#[cfg(unix)]
pub(crate) fn chmod(self, path: impl AsRef<Path>, perm: Permissions) -> Self {
fs::set_permissions(self.tempdir.path().join(path.as_ref()), perm).unwrap();
self
}

pub(crate) fn current_dir(mut self, path: impl AsRef<Path>) -> Self {
path.as_ref().clone_into(&mut self.current_dir);
self
Expand Down
114 changes: 114 additions & 0 deletions tests/working_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,117 @@ file := shell('cat file.txt')
.stdout("FILE\n")
.run();
}

#[test]
fn missing_working_directory_produces_clear_message() {
Test::new()
.justfile(
"
set working-directory := 'missing'
default:
pwd
",
)
.status(1)
.stderr_regex(".*Failed to run recipe `default`:\n Failed to run shell `bash`:\n .*\n Failed to set working directory to `.*/missing`.*")
.run();
}

#[test]
#[cfg(unix)]
fn unusable_working_directory_produces_clear_message() {
use {fs::Permissions, std::os::unix::fs::PermissionsExt};
Test::new()
.justfile(
"
set working-directory := 'unusable'
default:
pwd
",
)
.tree(tree! {
unusable: {}
})
.chmod("unusable", Permissions::from_mode(0o000))
.status(1)
.stderr_regex(".*Failed to run recipe `default`:\n Failed to run shell `bash`:\n .*\n Failed to set working directory to `.*/unusable`.*")
.run();
}

#[test]
fn working_directory_is_not_a_directory_produces_clear_message() {
Test::new()
.justfile(
"
set working-directory := 'unusable'
default:
pwd
",
)
.tree(tree! {
unusable: "is not a directory"
})
.status(1)
.stderr_regex(".*Failed to run recipe `default`:\n Failed to run shell `bash`:\n .*\n Failed to set working directory to `.*/unusable`.*")
.run();
}

#[test]
fn missing_working_directory_and_missing_shell_produces_clear_message() {
Test::new()
.justfile(
"
set working-directory := 'missing'
default:
pwd
",
)
.shell(false)
.args(["--shell", "NOT_A_REAL_SHELL"])
.status(1)
.stderr_regex(".*Failed to run recipe `default`:\n Failed to run shell `NOT_A_REAL_SHELL`:\n .*\n Failed to set working directory to `.*/missing`.*")
.run();
}

#[test]
#[cfg(unix)]
fn unusable_working_directory_and_missing_shell_produces_clear_message() {
use {fs::Permissions, std::os::unix::fs::PermissionsExt};
Test::new()
.justfile(
"
set working-directory := 'unusable'
default:
pwd
",
)
.tree(tree! {
unusable: {}
})
.shell(false)
.args(["--shell", "NOT_A_REAL_SHELL"])
.chmod("unusable", Permissions::from_mode(0o000))
.status(1)
.stderr_regex(".*Failed to run recipe `default`:\n Failed to run shell `NOT_A_REAL_SHELL`:\n .*\n Failed to set working directory to `.*/unusable`.*")
.run();
}

#[test]
fn working_directory_is_not_a_directory_and_missing_shell_produces_clear_message() {
Test::new()
.justfile(
"
set working-directory := 'unusable'
default:
pwd
",
)
.tree(tree! {
unusable: "is not a directory"
})
.shell(false)
.args(["--shell", "NOT_A_REAL_SHELL"])
.status(1)
.stderr_regex(".*Failed to run recipe `default`:\n Failed to run shell `NOT_A_REAL_SHELL`:\n .*\n Failed to set working directory to `.*/unusable`.*")
.run();
}