Skip to content

Commit

Permalink
x: introduce 'cargo xfmt', a wrapper around 'cargo fmt'
Browse files Browse the repository at this point in the history
Introduce 'xfmt', a wrapper around 'cargo fmt' which works around the
issue that the 'merge_imports' config of rustfmt isn't stable.

If you place an unstable config in a rustfmt.toml config file and run
using a stable version of rustfmt the config is ignored and you get the
following:

    $ cargo fmt
    Warning: can't set `merge_imports = true`, unstable features are only available in nightly channel.

But if you instead pass the config using the '--config' option rustfmt
happily applies and uses the unstable config:

    $ cargo fmt -- --config merge_imports=true

Since there are enough people on the project that would like to see
imports merged the new 'xfmt' wrapper uses the abbove workaround to
ensure that imports are merged when running 'cargo xfmt'.

If you also want to have the following behavior with your favorite
editor (assuming your editor lets you pass additional flags to rustfmt)
you can simply add '--config merge_imports=true' to the list of options
your editor passes rustfmt when formatting rust code. For example with
vim and the rust.vim plugin you would just need to add the following to
your vimrc to have imports merged when rust.vim is used to format your
code:

    let g:rustfmt_options = '--config merge_imports=true'

Closes: diem#2926
Approved by: bothra90
  • Loading branch information
bmwill authored and bors-libra committed Mar 13, 2020
1 parent e2ff6d3 commit a32b413
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
x = "run --package x --bin x --"
xcheck = "run --package x --bin x -- check"
xclippy = "run --package x --bin x -- clippy"
xfmt = "run --package x --bin x -- fmt"
xtest = "run --package x --bin x -- test"
5 changes: 5 additions & 0 deletions x/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ impl Cargo {
}
}

pub fn all(&mut self) -> &mut Self {
self.inner.arg("--all");
self
}

pub fn workspace(&mut self) -> &mut Self {
self.inner.arg("--workspace");
self
Expand Down
44 changes: 44 additions & 0 deletions x/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{cargo::Cargo, config::Config, Result};
use std::ffi::OsString;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct Args {
#[structopt(long)]
/// Run in 'check' mode. Exits with 0 if input is
/// formatted correctly. Exits with 1 and prints a diff if
/// formatting is required.
check: bool,

#[structopt(long)]
/// Run check on all packages in the workspace
workspace: bool,

#[structopt(name = "ARGS", parse(from_os_str), last = true)]
/// Pass through args to rustfmt
args: Vec<OsString>,
}

pub fn run(args: Args, _config: Config) -> Result<()> {
// Hardcode that we want imports merged
let mut pass_through_args = vec!["--config".into(), "merge_imports=true".into()];

if args.check {
pass_through_args.push("--check".into());
}

pass_through_args.extend(args.args);

let mut cmd = Cargo::new("fmt");

if args.workspace {
// cargo fmt doesn't have a --workspace flag, instead it uses the
// old --all flag
cmd.all();
}

cmd.pass_through(&pass_through_args).run()
}
5 changes: 5 additions & 0 deletions x/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod cargo;
mod check;
mod clippy;
mod config;
mod fmt;
mod lint;
mod test;
mod utils;
Expand All @@ -33,6 +34,9 @@ enum Command {
#[structopt(name = "clippy")]
/// Run `cargo clippy`
Clippy(clippy::Args),
#[structopt(name = "fmt")]
/// Run `cargo fmt`
Fmt(fmt::Args),
#[structopt(name = "test")]
/// Run tests
Test(test::Args),
Expand All @@ -49,6 +53,7 @@ fn main() -> Result<()> {
Command::Test(args) => test::run(args, config),
Command::Check(args) => check::run(args, config),
Command::Clippy(args) => clippy::run(args, config),
Command::Fmt(args) => fmt::run(args, config),
Command::Bench(args) => bench::run(args, config),
Command::Lint(args) => lint::run(args, config),
}
Expand Down

0 comments on commit a32b413

Please sign in to comment.