Skip to content

Commit 7b5fe7b

Browse files
committed
fix(cli): Unify manifest path error handling
1 parent 4e49af4 commit 7b5fe7b

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/bin/cargo/commands/run.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,7 @@ pub fn exec_manifest_command(config: &Config, cmd: &str, args: &[OsString]) -> C
9797
}
9898

9999
let manifest_path = Path::new(cmd);
100-
let manifest_path = config.cwd().join(manifest_path);
101-
let manifest_path = cargo_util::paths::normalize_path(&manifest_path);
102-
if !manifest_path.exists() {
103-
return Err(anyhow::format_err!(
104-
"manifest path `{}` does not exist",
105-
manifest_path.display()
106-
)
107-
.into());
108-
}
100+
let manifest_path = root_manifest(Some(manifest_path), config)?;
109101
let mut ws = Workspace::new(&manifest_path, config)?;
110102
if config.cli_unstable().avoid_dev_deps {
111103
ws.set_require_optional_deps(false);

src/cargo/util/command_prelude.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::CargoResult;
1515
use anyhow::bail;
1616
use cargo_util::paths;
1717
use std::ffi::{OsStr, OsString};
18+
use std::path::Path;
1819
use std::path::PathBuf;
1920

2021
pub use crate::core::compiler::CompileMode;
@@ -339,22 +340,7 @@ pub trait ArgMatchesExt {
339340
}
340341

341342
fn root_manifest(&self, config: &Config) -> CargoResult<PathBuf> {
342-
if let Some(path) = self.value_of_path("manifest-path", config) {
343-
// In general, we try to avoid normalizing paths in Cargo,
344-
// but in this particular case we need it to fix #3586.
345-
let path = paths::normalize_path(&path);
346-
if !path.ends_with("Cargo.toml") && !crate::util::toml::is_embedded(&path) {
347-
anyhow::bail!("the manifest-path must be a path to a Cargo.toml file")
348-
}
349-
if !path.exists() {
350-
anyhow::bail!(
351-
"manifest path `{}` does not exist",
352-
self._value_of("manifest-path").unwrap()
353-
)
354-
}
355-
return Ok(path);
356-
}
357-
find_root_manifest_for_wd(config.cwd())
343+
root_manifest(self._value_of("manifest-path").map(Path::new), config)
358344
}
359345

360346
fn workspace<'a>(&self, config: &'a Config) -> CargoResult<Workspace<'a>> {
@@ -792,6 +778,24 @@ pub fn values_os(args: &ArgMatches, name: &str) -> Vec<OsString> {
792778
args._values_of_os(name)
793779
}
794780

781+
pub fn root_manifest(manifest_path: Option<&Path>, config: &Config) -> CargoResult<PathBuf> {
782+
if let Some(manifest_path) = manifest_path {
783+
let path = config.cwd().join(manifest_path);
784+
// In general, we try to avoid normalizing paths in Cargo,
785+
// but in this particular case we need it to fix #3586.
786+
let path = paths::normalize_path(&path);
787+
if !path.ends_with("Cargo.toml") && !crate::util::toml::is_embedded(&path) {
788+
anyhow::bail!("the manifest-path must be a path to a Cargo.toml file")
789+
}
790+
if !path.exists() {
791+
anyhow::bail!("manifest path `{}` does not exist", manifest_path.display())
792+
}
793+
Ok(path)
794+
} else {
795+
find_root_manifest_for_wd(config.cwd())
796+
}
797+
}
798+
795799
#[track_caller]
796800
pub fn ignore_unknown<T: Default>(r: Result<T, clap::parser::MatchesError>) -> T {
797801
match r {

0 commit comments

Comments
 (0)