Skip to content

Commit

Permalink
Specify which file not found in error (#11868)
Browse files Browse the repository at this point in the history
# Description
Currently, `ShellError::FileNotFound` shows the span where the error
occurred but doesn't say which file wasn't found. This PR makes it so
the help includes that (like the `DirectoryNotFound` error).

# User-Facing Changes
No breaking changes, it's just that when a file can't be found, the help
will say which file couldn't be found:


![image](https://github.com/nushell/nushell/assets/45539777/e52f1e65-55c1-4cd2-8108-a4ccc334a66f)
  • Loading branch information
ysthakur committed Feb 21, 2024
1 parent 1058707 commit 6ff3a41
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 16 deletions.
34 changes: 22 additions & 12 deletions crates/nu-cli/src/commands/history/history_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,23 @@ impl Command for History {
} else {
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
HistoryFileFormat::Sqlite => {
SqliteBackedHistory::with_file(history_path, None, None)
SqliteBackedHistory::with_file(history_path.clone(), None, None)
.map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed
})
.ok()
}

HistoryFileFormat::PlainText => {
FileBackedHistory::with_file(history.max_size as usize, history_path)
.map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed
})
.ok()
}
HistoryFileFormat::PlainText => FileBackedHistory::with_file(
history.max_size as usize,
history_path.clone(),
)
.map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed
})
.ok(),
};

match history.file_format {
Expand All @@ -107,7 +108,10 @@ impl Command for History {
)
})
})
.ok_or(ShellError::FileNotFound { span: head })?
.ok_or(ShellError::FileNotFound {
file: history_path.display().to_string(),
span: head,
})?
.into_pipeline_data(ctrlc)),
HistoryFileFormat::Sqlite => Ok(history_reader
.and_then(|h| {
Expand All @@ -119,12 +123,18 @@ impl Command for History {
create_history_record(idx, entry, long, head)
})
})
.ok_or(ShellError::FileNotFound { span: head })?
.ok_or(ShellError::FileNotFound {
file: history_path.display().to_string(),
span: head,
})?
.into_pipeline_data(ctrlc)),
}
}
} else {
Err(ShellError::FileNotFound { span: head })
Err(ShellError::FileNotFound {
file: "history file".into(),
span: head,
})
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/nu-command/src/env/source_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Command for SourceEnv {
PathBuf::from(&path)
} else {
return Err(ShellError::FileNotFound {
file: source_filename.item,
span: source_filename.span,
});
};
Expand Down
1 change: 1 addition & 0 deletions crates/nu-command/src/filesystem/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl Command for Mv {

if sources.is_empty() {
return Err(ShellError::FileNotFound {
file: spanned_source.item.to_string(),
span: spanned_source.span,
});
}
Expand Down
5 changes: 4 additions & 1 deletion crates/nu-command/src/filesystem/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ impl Command for Open {

for path in nu_engine::glob_from(&path, &cwd, call_span, None)
.map_err(|err| match err {
ShellError::DirectoryNotFound { span, .. } => ShellError::FileNotFound { span },
ShellError::DirectoryNotFound { span, .. } => ShellError::FileNotFound {
file: path.item.to_string(),
span,
},
_ => err,
})?
.1
Expand Down
5 changes: 4 additions & 1 deletion crates/nu-command/src/filesystem/ucp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ impl Command for UCp {
.map(|f| f.1)?
.collect();
if exp_files.is_empty() {
return Err(ShellError::FileNotFound { span: p.span });
return Err(ShellError::FileNotFound {
file: p.item.to_string(),
span: p.span,
});
};
let mut app_vals: Vec<PathBuf> = Vec::new();
for v in exp_files {
Expand Down
5 changes: 4 additions & 1 deletion crates/nu-command/src/filesystem/umv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ impl Command for UMv {
.map(|f| f.1)?
.collect();
if exp_files.is_empty() {
return Err(ShellError::FileNotFound { span: p.span });
return Err(ShellError::FileNotFound {
file: p.item.to_string(),
span: p.span,
});
};
let mut app_vals: Vec<PathBuf> = Vec::new();
for v in exp_files {
Expand Down
1 change: 1 addition & 0 deletions crates/nu-command/src/system/nu_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl Command for NuCheck {
path
} else {
return Err(ShellError::FileNotFound {
file: path_str.item,
span: path_str.span,
});
}
Expand Down
3 changes: 2 additions & 1 deletion crates/nu-protocol/src/shell_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,9 @@ pub enum ShellError {
///
/// Does the file in the error message exist? Is it readable and accessible? Is the casing right?
#[error("File not found")]
#[diagnostic(code(nu::shell::file_not_found))]
#[diagnostic(code(nu::shell::file_not_found), help("{file} does not exist"))]
FileNotFound {
file: String,
#[label("file not found")]
span: Span,
},
Expand Down

0 comments on commit 6ff3a41

Please sign in to comment.