Skip to content

Commit 0e6896b

Browse files
authored
Merge pull request #3683 from calebcartwright/accept-manifest-path
Add --manifest-path support to cargo fmt
2 parents 71289e1 + 6f67f07 commit 0e6896b

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

src/cargo-fmt/main.rs

+56-12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub struct Opts {
4141
#[structopt(short = "p", long = "package", value_name = "package")]
4242
packages: Vec<String>,
4343

44+
/// Specify path to Cargo.toml
45+
#[structopt(long = "manifest-path", value_name = "manifest-path")]
46+
manifest_path: Option<String>,
47+
4448
/// Options passed to rustfmt
4549
// 'raw = true' to make `--` explicit.
4650
#[structopt(name = "rustfmt_options", raw(raw = "true"))]
@@ -97,7 +101,26 @@ fn execute() -> i32 {
97101

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

100-
handle_command_status(format_crate(verbosity, &strategy, opts.rustfmt_options))
104+
if let Some(specified_manifest_path) = opts.manifest_path {
105+
if !specified_manifest_path.ends_with("Cargo.toml") {
106+
print_usage_to_stderr("the manifest-path must be a path to a Cargo.toml file");
107+
return FAILURE;
108+
}
109+
let manifest_path = PathBuf::from(specified_manifest_path);
110+
handle_command_status(format_crate(
111+
verbosity,
112+
&strategy,
113+
opts.rustfmt_options,
114+
Some(&manifest_path),
115+
))
116+
} else {
117+
handle_command_status(format_crate(
118+
verbosity,
119+
&strategy,
120+
opts.rustfmt_options,
121+
None,
122+
))
123+
}
101124
}
102125

103126
fn print_usage_to_stderr(reason: &str) {
@@ -149,8 +172,9 @@ fn format_crate(
149172
verbosity: Verbosity,
150173
strategy: &CargoFmtStrategy,
151174
rustfmt_args: Vec<String>,
175+
manifest_path: Option<&Path>,
152176
) -> Result<i32, io::Error> {
153-
let targets = get_targets(strategy)?;
177+
let targets = get_targets(strategy, manifest_path)?;
154178

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

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

233260
match *strategy {
234-
CargoFmtStrategy::Root => get_targets_root_only(&mut targets)?,
235-
CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut BTreeSet::new())?,
236-
CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?,
261+
CargoFmtStrategy::Root => get_targets_root_only(manifest_path, &mut targets)?,
262+
CargoFmtStrategy::All => {
263+
get_targets_recursive(manifest_path, &mut targets, &mut BTreeSet::new())?
264+
}
265+
CargoFmtStrategy::Some(ref hitlist) => {
266+
get_targets_with_hitlist(manifest_path, hitlist, &mut targets)?
267+
}
237268
}
238269

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

249-
fn get_targets_root_only(targets: &mut BTreeSet<Target>) -> Result<(), io::Error> {
250-
let metadata = get_cargo_metadata(None, false)?;
251-
let current_dir = env::current_dir()?.canonicalize()?;
252-
let current_dir_manifest = current_dir.join("Cargo.toml");
280+
fn get_targets_root_only(
281+
manifest_path: Option<&Path>,
282+
targets: &mut BTreeSet<Target>,
283+
) -> Result<(), io::Error> {
284+
let metadata = get_cargo_metadata(manifest_path, false)?;
253285
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
254-
let in_workspace_root = workspace_root_path == current_dir;
286+
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
287+
(
288+
workspace_root_path == target_manifest,
289+
target_manifest.canonicalize()?,
290+
)
291+
} else {
292+
let current_dir = env::current_dir()?.canonicalize()?;
293+
(
294+
workspace_root_path == current_dir,
295+
current_dir.join("Cargo.toml"),
296+
)
297+
};
255298

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

321364
fn get_targets_with_hitlist(
365+
manifest_path: Option<&Path>,
322366
hitlist: &[String],
323367
targets: &mut BTreeSet<Target>,
324368
) -> Result<(), io::Error> {
325-
let metadata = get_cargo_metadata(None, false)?;
369+
let metadata = get_cargo_metadata(manifest_path, false)?;
326370

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

0 commit comments

Comments
 (0)