From 5c451c00883ad6e1cb38ab73569aeffb442b8be5 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 29 Mar 2022 19:56:50 +0200 Subject: [PATCH] Fix `--manifest-path` to point to `Cargo.toml` instead of a directory (#20) Currently `cargo-subcommand` expects this argument to be a path to the directory containing project(s), whereas it should actually point to a file specifically named `Cargo.toml`. Note that this path doesn't appear to be very strict in `cargo`: even if pointing to a manifest file within a workspace, packages in the surrounding workspace are still built, and are reachable through `-p`. However, there's a strict requirement on the specified manifest to be part of the workspace, which is not enforced in `cargo-subcommand` yet. --- src/error.rs | 2 ++ src/subcommand.rs | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index d698fd1..08a48ed 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,6 +9,7 @@ pub enum Error { InvalidArgs, ManifestNotFound, RustcNotFound, + ManifestPathNotFound, GlobPatternError(&'static str), Glob(GlobError), Io(PathBuf, IoError), @@ -20,6 +21,7 @@ impl Display for Error { f.write_str(match self { Self::InvalidArgs => "Invalid args.", Self::ManifestNotFound => "Didn't find Cargo.toml.", + Self::ManifestPathNotFound => "The manifest-path must be a path to a Cargo.toml file", Self::RustcNotFound => "Didn't find rustc.", Self::GlobPatternError(error) => error, Self::Glob(error) => return error.fmt(f), diff --git a/src/subcommand.rs b/src/subcommand.rs index 8eb7a81..ca8872d 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -3,6 +3,7 @@ use crate::artifact::{Artifact, CrateType}; use crate::error::Error; use crate::profile::Profile; use crate::{utils, LocalizedConfig}; +use std::ffi::OsStr; use std::io::BufRead; use std::path::{Path, PathBuf}; use std::process::Command; @@ -34,11 +35,21 @@ impl Subcommand { args.exclude.is_empty(), "`--exclude` is not supported yet by `cargo-subcommand`" ); + + let manifest_path = args + .manifest_path + .clone() + .map(|path| { + if path.file_name() != Some(OsStr::new("Cargo.toml")) || !path.is_file() { + Err(Error::ManifestPathNotFound) + } else { + Ok(path) + } + }) + .transpose()?; + let (manifest_path, package) = utils::find_package( - &args - .manifest_path - .clone() - .unwrap_or_else(|| std::env::current_dir().unwrap()), + &manifest_path.unwrap_or_else(|| std::env::current_dir().unwrap()), args.package.get(0).map(|s| s.as_str()), )?; let root_dir = manifest_path.parent().unwrap();