Skip to content

Commit 1a83abe

Browse files
authored
Handle errors when checking for files (#3051)
1 parent e6b9f31 commit 1a83abe

6 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/compiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl Compiler {
7979
.join(Self::expand_tilde(&relative.cooked)?)
8080
.lexiclean();
8181

82-
if import.is_file() {
82+
if filesystem::is_file(&import)? {
8383
if current.file_path.contains(&import) {
8484
return Err(Error::CircularImport {
8585
current: current.path,
@@ -121,7 +121,7 @@ impl Compiler {
121121
if let Some(path) = path {
122122
let full = parent.join(path);
123123

124-
if full.is_file() {
124+
if filesystem::is_file(&full)? {
125125
return Ok(Some(full));
126126
}
127127

src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ pub(crate) enum Error<'src> {
104104
ExpectedSubmoduleButFoundRecipe {
105105
path: String,
106106
},
107+
FilesystemIo {
108+
io_error: io::Error,
109+
path: PathBuf,
110+
},
107111
FlagWithValue {
108112
recipe: &'src str,
109113
option: Switch,
@@ -551,6 +555,9 @@ impl ColorDisplay for Error<'_> {
551555
ExpectedSubmoduleButFoundRecipe { path } => {
552556
write!(f, "Expected submodule at `{path}` but found recipe.")?;
553557
}
558+
FilesystemIo { io_error, path } => {
559+
write!(f, "I/O error at `{}`: {io_error}", path.display())?;
560+
}
554561
FlagWithValue { recipe, option } => {
555562
write!(f, "Recipe `{recipe}` flag `{option}` does not take value",)?;
556563
}

src/filesystem.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use super::*;
2+
3+
pub(crate) fn is_file(path: &Path) -> RunResult<'static, bool> {
4+
match path.metadata() {
5+
Ok(metadata) => Ok(metadata.is_file()),
6+
Err(io_error) => {
7+
if io_error.kind() == io::ErrorKind::NotFound {
8+
Ok(false)
9+
} else {
10+
Err(Error::FilesystemIo {
11+
path: path.into(),
12+
io_error,
13+
})
14+
}
15+
}
16+
}
17+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ mod evaluator;
224224
mod execution_context;
225225
mod executor;
226226
mod expression;
227+
mod filesystem;
227228
mod format_string_part;
228229
mod fragment;
229230
mod function;

src/load_dotenv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) fn load_dotenv(
2626

2727
if let Some(path) = dotenv_path {
2828
let path = working_directory.join(path);
29-
if path.is_file() {
29+
if filesystem::is_file(&path)? {
3030
return load_from_file(&path, settings);
3131
}
3232
}
@@ -35,7 +35,7 @@ pub(crate) fn load_dotenv(
3535

3636
for directory in working_directory.ancestors() {
3737
let path = directory.join(filename);
38-
if path.is_file() {
38+
if filesystem::is_file(&path)? {
3939
return load_from_file(&path, settings);
4040
}
4141
}

src/subcommand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl Subcommand {
380380
config.ceiling.as_deref(),
381381
)?;
382382

383-
if search.justfile.is_file() {
383+
if filesystem::is_file(&search.justfile)? {
384384
return Err(Error::InitExists {
385385
justfile: search.justfile,
386386
});

0 commit comments

Comments
 (0)