From 1eef1a4f559a6d3f060f315d06402bdf44601de0 Mon Sep 17 00:00:00 2001 From: Jonathan Tan Date: Mon, 1 Jul 2024 15:19:32 -0700 Subject: [PATCH] workspace add: add filename context to FS error At work, a user encountered a panic upon attempting to create a dir at the line in the diff below, but it turned out to be difficult to debug because I didn't know what the path was. There already is a mechanism to add path context in the lib crate; make it available in the cli crate as well, and use the mechanism to add path context to "workspace add". --- cli/src/command_error.rs | 6 ++++++ cli/src/commands/workspace.rs | 3 ++- lib/src/file_util.rs | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cli/src/command_error.rs b/cli/src/command_error.rs index 2ee53bed67..4471f9b6f1 100644 --- a/cli/src/command_error.rs +++ b/cli/src/command_error.rs @@ -206,6 +206,12 @@ impl From for CommandError { } } +impl From for CommandError { + fn from(err: jj_lib::file_util::PathError) -> Self { + user_error(err) + } +} + impl From for CommandError { fn from(err: config::ConfigError) -> Self { config_error(err) diff --git a/cli/src/commands/workspace.rs b/cli/src/commands/workspace.rs index 67ffa52f9f..b77ab08c3a 100644 --- a/cli/src/commands/workspace.rs +++ b/cli/src/commands/workspace.rs @@ -21,6 +21,7 @@ use clap::Subcommand; use itertools::Itertools; use jj_lib::commit::CommitIteratorExt; use jj_lib::file_util; +use jj_lib::file_util::IoResultExt; use jj_lib::object_id::ObjectId; use jj_lib::op_store::{OpStoreError, WorkspaceId}; use jj_lib::operation::Operation; @@ -137,7 +138,7 @@ fn cmd_workspace_add( if destination_path.exists() { return Err(user_error("Workspace already exists")); } else { - fs::create_dir(&destination_path).unwrap(); + fs::create_dir(&destination_path).context(&destination_path)?; } let name = if let Some(name) = &args.name { name.to_string() diff --git a/lib/src/file_util.rs b/lib/src/file_util.rs index 157c882542..55b24e9aab 100644 --- a/lib/src/file_util.rs +++ b/lib/src/file_util.rs @@ -31,7 +31,7 @@ pub struct PathError { pub error: io::Error, } -pub(crate) trait IoResultExt { +pub trait IoResultExt { fn context(self, path: impl AsRef) -> Result; }