From a71732ba1262220ce7ff866976c5c60d301ba23f Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Thu, 27 Jun 2024 23:49:06 -0700 Subject: [PATCH] Add context to the I/O error messages in `nu_cmd_plugin::util::modify_plugin_file()` (#13259) # Description This might help @hustcer debug problems with `setup-nu`. The error messages with the file I/O in `modify_plugin_file()` are not currently not specific about what file path was involved in the I/O operation. The spans on those errors have also changed to the span of the custom path if provided. # User-Facing Changes - Slightly better error --- crates/nu-cmd-plugin/src/util.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/nu-cmd-plugin/src/util.rs b/crates/nu-cmd-plugin/src/util.rs index 312ab9e81a..7d5282235f 100644 --- a/crates/nu-cmd-plugin/src/util.rs +++ b/crates/nu-cmd-plugin/src/util.rs @@ -31,11 +31,20 @@ pub(crate) fn modify_plugin_file( })? }; + let file_span = custom_path.as_ref().map(|p| p.span).unwrap_or(span); + // Try to read the plugin file if it exists let mut contents = if fs::metadata(&plugin_registry_file_path).is_ok_and(|m| m.len() > 0) { PluginRegistryFile::read_from( - File::open(&plugin_registry_file_path).err_span(span)?, - Some(span), + File::open(&plugin_registry_file_path).map_err(|err| ShellError::IOErrorSpanned { + msg: format!( + "failed to read `{}`: {}", + plugin_registry_file_path.display(), + err + ), + span: file_span, + })?, + Some(file_span), )? } else { PluginRegistryFile::default() @@ -46,7 +55,14 @@ pub(crate) fn modify_plugin_file( // Save the modified file on success contents.write_to( - File::create(&plugin_registry_file_path).err_span(span)?, + File::create(&plugin_registry_file_path).map_err(|err| ShellError::IOErrorSpanned { + msg: format!( + "failed to create `{}`: {}", + plugin_registry_file_path.display(), + err + ), + span: file_span, + })?, Some(span), )?;