diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index f182384e08..7f0111452c 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -611,6 +611,10 @@ impl CommandHelper { &self.app } + /// Canonical form of the current working directory path. + /// + /// A loaded `Workspace::workspace_root()` also returns a canonical path, so + /// relative paths can be easily computed from these paths. pub fn cwd(&self) -> &Path { &self.cwd } @@ -2954,12 +2958,16 @@ impl CliRunner { ui: &mut Ui, mut layered_configs: LayeredConfigs, ) -> Result<(), CommandError> { - let cwd = env::current_dir().map_err(|_| { - user_error_with_hint( - "Could not determine current directory", - "Did you check-out a commit where the directory doesn't exist?", - ) - })?; + // `cwd` is canonicalized for consistency with `Workspace::workspace_root()` and + // to easily compute relative paths between them. + let cwd = env::current_dir() + .and_then(|cwd| cwd.canonicalize()) + .map_err(|_| { + user_error_with_hint( + "Could not determine current directory", + "Did you check-out a commit where the directory doesn't exist?", + ) + })?; // Use cwd-relative workspace configs to resolve default command and // aliases. WorkspaceLoader::init() won't do any heavy lifting other // than the path resolution. @@ -3010,9 +3018,6 @@ impl CliRunner { } } - // `cwd` is canonicalized for consistency with `Workspace::workspace_root()` and - // to easily compute relative paths between them. - let cwd = cwd.canonicalize().unwrap_or(cwd); let settings = UserSettings::from_config(config); let working_copy_factories = self .working_copy_factories diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index 0c309c1373..e0cffbcf08 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -492,10 +492,7 @@ fn cmd_git_init( command: &CommandHelper, args: &GitInitArgs, ) -> Result<(), CommandError> { - let cwd = command - .cwd() - .canonicalize() - .map_err(|e| user_error_with_message("Could not determine current directory", e))?; + let cwd = command.cwd(); let wc_path = cwd.join(&args.destination); let wc_path = file_util::create_or_reuse_dir(&wc_path) .and_then(|_| wc_path.canonicalize()) @@ -509,7 +506,7 @@ fn cmd_git_init( args.git_repo.as_deref(), )?; - let relative_wc_path = file_util::relative_path(&cwd, &wc_path); + let relative_wc_path = file_util::relative_path(cwd, &wc_path); writeln!( ui.stderr(), r#"Initialized repo in "{}""#, diff --git a/cli/src/commands/init.rs b/cli/src/commands/init.rs index 3efdeac08b..dcd53dffd6 100644 --- a/cli/src/commands/init.rs +++ b/cli/src/commands/init.rs @@ -49,10 +49,7 @@ pub(crate) fn cmd_init( command: &CommandHelper, args: &InitArgs, ) -> Result<(), CommandError> { - let cwd = command - .cwd() - .canonicalize() - .map_err(|e| user_error_with_message("Could not determine current directory", e))?; + let cwd = command.cwd(); let wc_path = cwd.join(&args.destination); let wc_path = file_util::create_or_reuse_dir(&wc_path) .and_then(|_| wc_path.canonicalize()) @@ -79,7 +76,7 @@ Set `ui.allow-init-native` to allow initializing a repo with the native backend. Workspace::init_local(command.settings(), &wc_path)?; } - let relative_wc_path = file_util::relative_path(&cwd, &wc_path); + let relative_wc_path = file_util::relative_path(cwd, &wc_path); writeln!( ui.stderr(), "Initialized repo in \"{}\"",