-
Notifications
You must be signed in to change notification settings - Fork 933
add --offline mode fallback to cargo fmt #3813
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,10 @@ pub struct Opts { | |
/// Format all packages (only usable in workspaces) | ||
#[structopt(long = "all")] | ||
format_all: bool, | ||
|
||
/// Run without accessing the network | ||
#[structopt(long = "offline")] | ||
offline: bool, | ||
} | ||
|
||
fn main() { | ||
|
@@ -112,6 +116,12 @@ fn execute() -> i32 { | |
} | ||
} | ||
|
||
let mut cargo_metadata_additional_opts: Vec<String> = Vec::new(); | ||
|
||
if opts.offline { | ||
cargo_metadata_additional_opts.push(String::from("--offline")); | ||
} | ||
|
||
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"); | ||
|
@@ -123,9 +133,16 @@ fn execute() -> i32 { | |
&strategy, | ||
rustfmt_args, | ||
Some(&manifest_path), | ||
&cargo_metadata_additional_opts, | ||
)) | ||
} else { | ||
handle_command_status(format_crate(verbosity, &strategy, rustfmt_args, None)) | ||
handle_command_status(format_crate( | ||
verbosity, | ||
&strategy, | ||
rustfmt_args, | ||
None, | ||
&cargo_metadata_additional_opts, | ||
)) | ||
} | ||
} | ||
|
||
|
@@ -229,8 +246,9 @@ fn format_crate( | |
strategy: &CargoFmtStrategy, | ||
rustfmt_args: Vec<String>, | ||
manifest_path: Option<&Path>, | ||
cargo_metadata_additional_opts: &[String], | ||
) -> Result<i32, io::Error> { | ||
let targets = get_targets(strategy, manifest_path)?; | ||
let targets = get_targets(strategy, manifest_path, cargo_metadata_additional_opts)?; | ||
|
||
// Currently only bin and lib files get formatted. | ||
run_rustfmt(&targets, &rustfmt_args, verbosity) | ||
|
@@ -310,17 +328,26 @@ impl CargoFmtStrategy { | |
fn get_targets( | ||
strategy: &CargoFmtStrategy, | ||
manifest_path: Option<&Path>, | ||
cargo_metadata_additional_opts: &[String], | ||
) -> Result<BTreeSet<Target>, io::Error> { | ||
let mut targets = BTreeSet::new(); | ||
|
||
match *strategy { | ||
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)? | ||
CargoFmtStrategy::Root => { | ||
get_targets_root_only(manifest_path, &mut targets, cargo_metadata_additional_opts)? | ||
} | ||
CargoFmtStrategy::All => get_targets_recursive( | ||
manifest_path, | ||
&mut targets, | ||
&mut BTreeSet::new(), | ||
cargo_metadata_additional_opts, | ||
)?, | ||
CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist( | ||
manifest_path, | ||
hitlist, | ||
&mut targets, | ||
cargo_metadata_additional_opts, | ||
)?, | ||
} | ||
|
||
if targets.is_empty() { | ||
|
@@ -336,8 +363,9 @@ fn get_targets( | |
fn get_targets_root_only( | ||
manifest_path: Option<&Path>, | ||
targets: &mut BTreeSet<Target>, | ||
cargo_metadata_additional_opts: &[String], | ||
) -> Result<(), io::Error> { | ||
let metadata = get_cargo_metadata(manifest_path, false)?; | ||
let metadata = get_cargo_metadata(manifest_path, false, cargo_metadata_additional_opts)?; | ||
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?; | ||
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path { | ||
( | ||
|
@@ -380,9 +408,11 @@ fn get_targets_recursive( | |
manifest_path: Option<&Path>, | ||
mut targets: &mut BTreeSet<Target>, | ||
visited: &mut BTreeSet<String>, | ||
cargo_metadata_additional_opts: &[String], | ||
) -> Result<(), io::Error> { | ||
let metadata = get_cargo_metadata(manifest_path, false)?; | ||
let metadata_with_deps = get_cargo_metadata(manifest_path, true)?; | ||
let metadata = get_cargo_metadata(manifest_path, false, cargo_metadata_additional_opts)?; | ||
let metadata_with_deps = | ||
get_cargo_metadata(manifest_path, true, cargo_metadata_additional_opts)?; | ||
|
||
for package in metadata.packages { | ||
add_targets(&package.targets, &mut targets); | ||
|
@@ -409,7 +439,12 @@ fn get_targets_recursive( | |
|
||
if manifest_path.exists() { | ||
visited.insert(dependency.name); | ||
get_targets_recursive(Some(&manifest_path), &mut targets, visited)?; | ||
get_targets_recursive( | ||
Some(&manifest_path), | ||
&mut targets, | ||
visited, | ||
cargo_metadata_additional_opts, | ||
)?; | ||
} | ||
} | ||
} | ||
|
@@ -421,8 +456,9 @@ fn get_targets_with_hitlist( | |
manifest_path: Option<&Path>, | ||
hitlist: &[String], | ||
targets: &mut BTreeSet<Target>, | ||
cargo_metadata_additional_opts: &[String], | ||
) -> Result<(), io::Error> { | ||
let metadata = get_cargo_metadata(manifest_path, false)?; | ||
let metadata = get_cargo_metadata(manifest_path, false, cargo_metadata_additional_opts)?; | ||
|
||
let mut workspace_hitlist: BTreeSet<&String> = BTreeSet::from_iter(hitlist); | ||
|
||
|
@@ -511,6 +547,7 @@ fn run_rustfmt( | |
fn get_cargo_metadata( | ||
manifest_path: Option<&Path>, | ||
include_deps: bool, | ||
other_opts: &[String], | ||
) -> Result<cargo_metadata::Metadata, io::Error> { | ||
let mut cmd = cargo_metadata::MetadataCommand::new(); | ||
if !include_deps { | ||
|
@@ -519,6 +556,7 @@ fn get_cargo_metadata( | |
if let Some(manifest_path) = manifest_path { | ||
cmd.manifest_path(manifest_path); | ||
} | ||
cmd.other_options(other_opts); | ||
match cmd.exec() { | ||
Ok(metadata) => Ok(metadata), | ||
Err(error) => Err(io::Error::new(io::ErrorKind::Other, error.to_string())), | ||
|
@@ -541,6 +579,7 @@ mod cargo_fmt_tests { | |
assert_eq!(false, o.format_all); | ||
assert_eq!(None, o.manifest_path); | ||
assert_eq!(None, o.message_format); | ||
assert_eq!(false, o.offline); | ||
} | ||
|
||
#[test] | ||
|
@@ -552,13 +591,15 @@ mod cargo_fmt_tests { | |
"p1", | ||
"-p", | ||
"p2", | ||
"--offline", | ||
"--message-format", | ||
"short", | ||
"--", | ||
"--edition", | ||
"2018", | ||
]); | ||
assert_eq!(true, o.quiet); | ||
assert_eq!(true, o.offline); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that unit test is good enough... @topecongiro ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be cool if one day we could have tests that validate that the correct options were passed to the |
||
assert_eq!(false, o.verbose); | ||
assert_eq!(false, o.version); | ||
assert_eq!(vec!["p1", "p2"], o.packages); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I elected to go this route to make it easier to add any additional options in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm!