Skip to content

Add --manifest-path support to cargo fmt #3683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub struct Opts {
#[structopt(short = "p", long = "package", value_name = "package")]
packages: Vec<String>,

/// Specify path to Cargo.toml
#[structopt(long = "manifest-path", value_name = "manifest-path")]
manifest_path: Option<String>,

/// Options passed to rustfmt
// 'raw = true' to make `--` explicit.
#[structopt(name = "rustfmt_options", raw(raw = "true"))]
Expand Down Expand Up @@ -97,7 +101,26 @@ fn execute() -> i32 {

let strategy = CargoFmtStrategy::from_opts(&opts);

handle_command_status(format_crate(verbosity, &strategy, opts.rustfmt_options))
if let Some(specified_manifest_path) = opts.manifest_path {
if !specified_manifest_path.ends_with("Cargo.toml") {
print_usage_to_stderr("the manifest-path must be a path to a Cargo.toml file");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same error message cargo test displays when the value is not a path to a Cargo.toml file, so I thought it would be good to do the same here.

return FAILURE;
}
let manifest_path = PathBuf::from(specified_manifest_path);
handle_command_status(format_crate(
verbosity,
&strategy,
opts.rustfmt_options,
Some(&manifest_path),
))
} else {
handle_command_status(format_crate(
verbosity,
&strategy,
opts.rustfmt_options,
None,
))
}
}

fn print_usage_to_stderr(reason: &str) {
Expand Down Expand Up @@ -149,8 +172,9 @@ fn format_crate(
verbosity: Verbosity,
strategy: &CargoFmtStrategy,
rustfmt_args: Vec<String>,
manifest_path: Option<&Path>,
) -> Result<i32, io::Error> {
let targets = get_targets(strategy)?;
let targets = get_targets(strategy, manifest_path)?;

// Currently only bin and lib files get formatted.
run_rustfmt(&targets, &rustfmt_args, verbosity)
Expand Down Expand Up @@ -227,13 +251,20 @@ impl CargoFmtStrategy {
}

/// Based on the specified `CargoFmtStrategy`, returns a set of main source files.
fn get_targets(strategy: &CargoFmtStrategy) -> Result<BTreeSet<Target>, io::Error> {
fn get_targets(
strategy: &CargoFmtStrategy,
manifest_path: Option<&Path>,
) -> Result<BTreeSet<Target>, io::Error> {
let mut targets = BTreeSet::new();

match *strategy {
CargoFmtStrategy::Root => get_targets_root_only(&mut targets)?,
CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut BTreeSet::new())?,
CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?,
CargoFmtStrategy::Root => get_targets_root_only(manifest_path, &mut targets)?,
CargoFmtStrategy::All => {
get_targets_recursive(manifest_path, &mut targets, &mut BTreeSet::new())?
}
CargoFmtStrategy::Some(ref hitlist) => {
get_targets_with_hitlist(manifest_path, hitlist, &mut targets)?
}
}

if targets.is_empty() {
Expand All @@ -246,12 +277,24 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<BTreeSet<Target>, io::Erro
}
}

fn get_targets_root_only(targets: &mut BTreeSet<Target>) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(None, false)?;
let current_dir = env::current_dir()?.canonicalize()?;
let current_dir_manifest = current_dir.join("Cargo.toml");
fn get_targets_root_only(
manifest_path: Option<&Path>,
targets: &mut BTreeSet<Target>,
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path, false)?;
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
let in_workspace_root = workspace_root_path == current_dir;
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
(
workspace_root_path == target_manifest,
target_manifest.canonicalize()?,
)
} else {
let current_dir = env::current_dir()?.canonicalize()?;
(
workspace_root_path == current_dir,
current_dir.join("Cargo.toml"),
)
};

let package_targets = match metadata.packages.len() {
1 => metadata.packages.into_iter().next().unwrap().targets,
Expand Down Expand Up @@ -319,10 +362,11 @@ fn get_targets_recursive(
}

fn get_targets_with_hitlist(
manifest_path: Option<&Path>,
hitlist: &[String],
targets: &mut BTreeSet<Target>,
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(None, false)?;
let metadata = get_cargo_metadata(manifest_path, false)?;

let mut workspace_hitlist: BTreeSet<&String> = BTreeSet::from_iter(hitlist);

Expand Down