-
Notifications
You must be signed in to change notification settings - Fork 933
feat: add ignore_missing_submod option #5611
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
b9e2897
34fd630
b6d27d3
2faae32
aa4cd26
50e7197
4bb0453
f678369
fcc06f9
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 |
---|---|---|
|
@@ -1306,6 +1306,15 @@ If you want to ignore every file under the directory where you put your rustfmt. | |
ignore = ["/"] | ||
``` | ||
|
||
## `ignore_missing_submod` | ||
|
||
Ignore missing submodule error. | ||
By default, missing modules will lead to module not found errors and rustfmt won't format anything. | ||
|
||
- **Default value**: `false` | ||
- **Possible values**: `true`, `false` | ||
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. On thing the team has discussed is moving away from a binary error model. I think if we land this, I'd like to change from 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. Update Configuration.md on this commit. 4bb0453 |
||
- **Stable**: No | ||
|
||
## `imports_indent` | ||
|
||
Indent style of imports | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ pub(crate) struct ModResolver<'ast, 'sess> { | |
directory: Directory, | ||
file_map: FileModMap<'ast>, | ||
recursive: bool, | ||
ignore_missing_submod: bool, | ||
} | ||
|
||
/// Represents errors while trying to resolve modules. | ||
|
@@ -105,6 +106,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { | |
parse_sess: &'sess ParseSess, | ||
directory_ownership: DirectoryOwnership, | ||
recursive: bool, | ||
ignore_missing_submod: bool, | ||
) -> Self { | ||
ModResolver { | ||
directory: Directory { | ||
|
@@ -114,6 +116,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { | |
file_map: BTreeMap::new(), | ||
parse_sess, | ||
recursive, | ||
ignore_missing_submod, | ||
} | ||
} | ||
|
||
|
@@ -249,6 +252,14 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { | |
// mod foo; | ||
// Look for an extern file. | ||
self.find_external_module(item.ident, &item.attrs, sub_mod) | ||
.or_else(|err| match err.kind { | ||
ModuleResolutionErrorKind::NotFound { file: _ } | ||
if self.ignore_missing_submod => | ||
{ | ||
Ok(None) | ||
} | ||
_ => Err(err), | ||
}) | ||
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'd like to rework this with 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. Thank you for the review! When emitting a message for a 'warn', is it sufficient to simply use 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. Fixed. f678369 For now, I implemented direct output with eprintln. |
||
} else { | ||
// An internal module (`mod foo { /* ... */ }`); | ||
Ok(Some(SubModKind::Internal(item))) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
//! Integration tests for rustfmt. | ||
|
||
use std::env; | ||
use std::fs::remove_file; | ||
use std::path::Path; | ||
use std::fs::{read_to_string, remove_file}; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
use rustfmt_config_proc_macro::rustfmt_only_ci_test; | ||
|
@@ -200,3 +200,30 @@ fn rustfmt_emits_error_when_control_brace_style_is_always_next_line() { | |
let (_stdout, stderr) = rustfmt(&args); | ||
assert!(!stderr.contains("error[internal]: left behind trailing whitespace")) | ||
} | ||
|
||
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. will likely need to rework the tests slightly to accommodate 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. Fixed test on this commit. fcc06f9 |
||
#[test] | ||
fn ignore_missing_sub_mod_false() { | ||
// Ensure that missing submodules cause module not found errors when trying to | ||
// resolve submodules with `skip_children=false` and `ignore_missing_submod=false` | ||
let args = [ | ||
"--config=skip_children=false,ignore_missing_submod=false", | ||
"tests/source/issue-5609.rs", | ||
]; | ||
let (_stdout, stderr) = rustfmt(&args); | ||
// Module resolution fails because we're unable to find `missing_submod.rs` | ||
assert!(stderr.contains("missing_submod.rs does not exist")) | ||
} | ||
|
||
#[test] | ||
fn ignore_missing_sub_mod_true() { | ||
// Ensure that missing submodules don't cause module not found errors when trying to | ||
// resolve submodules with `skip_children=false` and `ignore_missing_submod=true`. | ||
let args = [ | ||
"--emit=stdout", | ||
"--config=skip_children=false,ignore_missing_submod=true", | ||
"tests/source/issue-5609.rs", | ||
]; | ||
let (stdout, _stderr) = rustfmt(&args); | ||
let target = read_to_string(PathBuf::from("tests/target/issue-5609.rs")).unwrap(); | ||
assert!(stdout.ends_with(&target)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// rustfmt-ignore_missing_submod: true | ||
// rustfmt-skip_children: false | ||
|
||
mod missing_submod; | ||
|
||
fn test() { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// rustfmt-ignore_missing_submod: true | ||
// rustfmt-skip_children: false | ||
|
||
mod missing_submod; | ||
|
||
fn test() {} |
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 think we need to elaborate on this description. I think we should explain that normally missing modules will lead to module not found errors and rustfmt won't format anything.
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.
Fixed. Most of the messages you wrote are used, but I added "By default" at the beginning of the sentence to clarify the behavior by default.