Skip to content
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

switch to non-recursive mode by default #3938

Merged
merged 4 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,14 @@ fn example() {
}
```

## `recursive`
Copy link
Contributor

Choose a reason for hiding this comment

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

We should consider recursive as one of the internal options. We do not want users to add this to the configuration by themselves.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do 👍, though out of curiosity, why should this be a CLI only flag for users?

One downside I see with exposing recursive as a config option is that recursive = false in the config file would functionally be ignored when using cargo fmt since cargo fmt will always pass --recursive flag to rustfmt which will always take precedence over the config file value (which could be confusing for users).

Copy link
Contributor

Choose a reason for hiding this comment

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

The options in the configuration file are meant to be persistent, whereas recursive is more of a one-shot configuration, which best suits as a CLI flag.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense to me! It wouldn't surprise me if there is a future request to support recursive as a config option since skip_children was available, but there's clearly several good reasons why recursive should be internal/cli-only.

Side note, while making recursive internal I noticed that internal options added to the config file seem to just be silently ignored. Do I have that correct?

If they are silently ignored:

Is that the desired behavior or would it beneficial to emit a warning ("Warning: Unknown configuration option ..)? Would that potential change be tackled (perhaps it already is) as part of #3873 or should I open a new issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

Side note, while making recursive internal I noticed that internal options added to the config file seem to just be silently ignored. Do I have that correct?

Yes.

Is that the desired behavior or would it beneficial to emit a warning ("Warning: Unknown configuration option ..)?

We should make it to a hard error rather than silently ignoring it.

Would that potential change be tackled (perhaps it already is) as part of #3873 or should I open a new issue?

This should be fixed by #3873.


Format all encountered modules recursively, including those defined in external files.
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: Yes

## `remove_nested_parens`

Remove nested parens.
Expand Down
23 changes: 22 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ fn make_opts() -> Options {
"Don't reformat child modules (unstable).",
);
}

opts.optflag(
"r",
"recursive",
"Format all encountered modules recursively, including those defined in external files.",
);
opts.optflag("v", "verbose", "Print verbose output");
opts.optflag("q", "quiet", "Print less output");
opts.optflag("V", "version", "Show version information");
Expand Down Expand Up @@ -500,6 +504,7 @@ const STABLE_EMIT_MODES: [EmitMode; 3] = [EmitMode::Files, EmitMode::Stdout, Emi
#[derive(Clone, Debug, Default)]
struct GetOptsOptions {
skip_children: Option<bool>,
recursive: Option<bool>,
quiet: bool,
verbose: bool,
config_path: Option<PathBuf>,
Expand Down Expand Up @@ -568,6 +573,19 @@ impl GetOptsOptions {
}
}

if matches.opt_present("recursive") {
if let Some(skip) = options.skip_children {
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
if skip {
return Err(format_err!(
"Conflicting config options `skip_children` and `recursive` are \
both enabled. `skip_children` has been deprecated and should be \
removed from your config.",
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
));
}
}
options.recursive = Some(true);
}

options.config_path = matches.opt_str("config-path").map(PathBuf::from);

options.inline_config = matches
Expand Down Expand Up @@ -658,6 +676,9 @@ impl CliOptions for GetOptsOptions {
if let Some(skip_children) = self.skip_children {
config.set().skip_children(skip_children);
}
if let Some(recursive) = self.recursive {
config.set().recursive(recursive);
}
if let Some(error_on_unformatted) = self.error_on_unformatted {
config.set().error_on_unformatted(error_on_unformatted);
}
Expand Down
223 changes: 164 additions & 59 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,13 @@ fn execute() -> i32 {

let strategy = CargoFmtStrategy::from_opts(&opts);
let mut rustfmt_args = opts.rustfmt_options;
if opts.check {
let check_flag = String::from("--check");
if !rustfmt_args.contains(&check_flag) {
rustfmt_args.push(check_flag);
}
let cargo_fmt_check = opts.check;
if let Err(ref msg) =
build_rustfmt_args(opts.message_format, &mut rustfmt_args, cargo_fmt_check)
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
{
print_usage_to_stderr(msg);
return FAILURE;
}
if let Some(message_format) = opts.message_format {
if let Err(msg) = convert_message_format_to_rustfmt_args(&message_format, &mut rustfmt_args)
{
print_usage_to_stderr(&msg);
return FAILURE;
}
}

let include_nested_test_files = opts.include_nested_test_files;

if let Some(specified_manifest_path) = opts.manifest_path {
Expand All @@ -152,13 +145,16 @@ fn execute() -> i32 {
}
}

fn convert_message_format_to_rustfmt_args(
message_format: &str,
fn build_rustfmt_args(
message_format: Option<String>,
rustfmt_args: &mut Vec<String>,
check: bool,
) -> Result<(), String> {
let mut contains_emit_mode = false;
let mut contains_check = false;
let mut contains_emit_mode = false;
let mut contains_list_files = false;
let mut contains_recursive = false;

for arg in rustfmt_args.iter() {
if arg.starts_with("--emit") {
contains_emit_mode = true;
Expand All @@ -169,37 +165,53 @@ fn convert_message_format_to_rustfmt_args(
if arg == "-l" || arg == "--files-with-diff" {
contains_list_files = true;
}
if arg == "-r" || arg == "--recursive" {
contains_recursive = true;
}
}

if check && !contains_check {
rustfmt_args.push(String::from("--check"));
}
match message_format {
"short" => {
if !contains_list_files {
rustfmt_args.push(String::from("-l"));

if !contains_recursive {
rustfmt_args.push(String::from("--recursive"));
}

if let Some(format) = message_format {
return match format.as_ref() {
"short" => {
if !contains_list_files {
rustfmt_args.push(String::from("-l"));
}
Ok(())
}
Ok(())
}
"json" => {
if contains_emit_mode {
return Err(String::from(
"cannot include --emit arg when --message-format is set to json",
));
"json" => {
if contains_emit_mode {
return Err(String::from(
"cannot include --emit arg when --message-format is set to json",
));
}
if contains_check {
return Err(String::from(
"cannot include --check arg when --message-format is set to json",
));
}
rustfmt_args.push(String::from("--emit"));
rustfmt_args.push(String::from("json"));
Ok(())
}
if contains_check {
return Err(String::from(
"cannot include --check arg when --message-format is set to json",
"human" => Ok(()),
_ => {
return Err(format!(
"invalid --message-format value: {}. Allowed values are: short|json|human",
format
));
}
rustfmt_args.push(String::from("--emit"));
rustfmt_args.push(String::from("json"));
Ok(())
}
"human" => Ok(()),
_ => {
return Err(format!(
"invalid --message-format value: {}. Allowed values are: short|json|human",
message_format
));
}
};
}

Ok(())
}

fn print_usage_to_stderr(reason: &str) {
Expand Down Expand Up @@ -801,13 +813,13 @@ mod cargo_fmt_tests {
);
}

mod convert_message_format_to_rustfmt_args_tests {
mod build_rustfmt_args_tests {
use super::*;

#[test]
fn invalid_message_format() {
assert_eq!(
convert_message_format_to_rustfmt_args("awesome", &mut vec![]),
build_rustfmt_args(Some(String::from("awesome")), &mut vec![], false),
Err(String::from(
"invalid --message-format value: awesome. Allowed values are: short|json|human"
)),
Expand All @@ -818,7 +830,7 @@ mod cargo_fmt_tests {
fn json_message_format_and_check_arg() {
let mut args = vec![String::from("--check")];
assert_eq!(
convert_message_format_to_rustfmt_args("json", &mut args),
build_rustfmt_args(Some(String::from("json")), &mut args, false),
Err(String::from(
"cannot include --check arg when --message-format is set to json"
)),
Expand All @@ -829,7 +841,7 @@ mod cargo_fmt_tests {
fn json_message_format_and_emit_arg() {
let mut args = vec![String::from("--emit"), String::from("checkstyle")];
assert_eq!(
convert_message_format_to_rustfmt_args("json", &mut args),
build_rustfmt_args(Some(String::from("json")), &mut args, false),
Err(String::from(
"cannot include --emit arg when --message-format is set to json"
)),
Expand All @@ -838,50 +850,143 @@ mod cargo_fmt_tests {

#[test]
fn json_message_format() {
let mut args = vec![String::from("--edition"), String::from("2018")];
assert!(convert_message_format_to_rustfmt_args("json", &mut args).is_ok());
let mut args = vec![
String::from("--edition"),
String::from("2018"),
String::from("--recursive"),
];
assert!(build_rustfmt_args(Some(String::from("json")), &mut args, false).is_ok());
assert_eq!(
args,
vec![
String::from("--edition"),
String::from("2018"),
String::from("--recursive"),
String::from("--emit"),
String::from("json")
String::from("json"),
]
);
}

#[test]
fn human_message_format() {
let exp_args = vec![String::from("--emit"), String::from("json")];
let exp_args = vec![
String::from("--emit"),
String::from("json"),
String::from("--recursive"),
];
let mut act_args = exp_args.clone();
assert!(convert_message_format_to_rustfmt_args("human", &mut act_args).is_ok());
assert!(build_rustfmt_args(Some(String::from("human")), &mut act_args, false).is_ok());
assert_eq!(act_args, exp_args);
}

#[test]
fn short_message_format() {
let mut args = vec![String::from("--check")];
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
let mut args = vec![String::from("--check"), String::from("--recursive")];
assert!(build_rustfmt_args(Some(String::from("short")), &mut args, false).is_ok());
assert_eq!(
args,
vec![
String::from("--check"),
String::from("--recursive"),
String::from("-l"),
],
);
}

#[test]
fn short_message_format_included_short_list_files_flag() {
let mut args = vec![String::from("--check"), String::from("-l")];
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
let mut args = vec![
String::from("--check"),
String::from("-l"),
String::from("--recursive"),
];
assert!(build_rustfmt_args(Some(String::from("short")), &mut args, false).is_ok());
assert_eq!(
args,
vec![
String::from("--check"),
String::from("-l"),
String::from("--recursive"),
],
);
}

#[test]
fn short_message_format_included_long_list_files_flag() {
let mut args = vec![String::from("--check"), String::from("--files-with-diff")];
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
let mut args = vec![
String::from("--check"),
String::from("--files-with-diff"),
String::from("--recursive"),
];
assert!(build_rustfmt_args(Some(String::from("short")), &mut args, false).is_ok());
assert_eq!(
args,
vec![
String::from("--check"),
String::from("--files-with-diff"),
String::from("--recursive"),
]
);
}

#[test]
fn recursive_shorthand_not_duplicated() {
let mut args = vec![String::from("-r")];
assert!(build_rustfmt_args(None, &mut args, false).is_ok());
assert_eq!(args, vec![String::from("-r")]);
}

#[test]
fn recursive_long_not_duplicated() {
let mut args = vec![String::from("--recursive")];
assert!(build_rustfmt_args(None, &mut args, false).is_ok());
assert_eq!(args, vec![String::from("--recursive")]);
}

#[test]
fn recursive_added() {
let mut args = vec![];
assert!(build_rustfmt_args(None, &mut args, false).is_ok());
assert_eq!(args, vec![String::from("--recursive")]);
}

#[test]
fn check_not_duplicated_when_included_in_cargo_fmt() {
let mut args = vec![String::from("--check"), String::from("--recursive")];
assert!(build_rustfmt_args(None, &mut args, true).is_ok());
assert_eq!(
args,
vec![String::from("--check"), String::from("--files-with-diff")]
vec![String::from("--check"), String::from("--recursive")],
);
}

#[test]
fn check_still_passed_through_when_not_included_in_cargo_fmt() {
let mut args = vec![String::from("--check"), String::from("--recursive")];
assert!(build_rustfmt_args(None, &mut args, false).is_ok());
assert_eq!(
args,
vec![String::from("--check"), String::from("--recursive")],
);
}

#[test]
fn check_added() {
let mut args = vec![String::from("--recursive")];
assert!(build_rustfmt_args(None, &mut args, true).is_ok());
assert_eq!(
args,
vec![String::from("--recursive"), String::from("--check")],
);
}

#[test]
fn check_not_added() {
let mut args = vec![String::from("--recursive")];
assert!(build_rustfmt_args(None, &mut args, false).is_ok());
assert_eq!(args, vec![String::from("--recursive")]);
}
}

mod get_nested_integration_test_files_tests {
Expand Down
Loading