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

feat: add ignore_missing_submod option #5611

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

tanzaku
Copy link

@tanzaku tanzaku commented Nov 21, 2022

Resolve #5609

Made the changes suggested by ytmimi in issue #5609.

This is my first contribution, so I may have made some mistakes. I would be happy to fix them if you point them out.

@ytmimi ytmimi self-assigned this Nov 21, 2022
@ytmimi
Copy link
Contributor

ytmimi commented Nov 21, 2022

Thanks for your first contribution to rustfmt 🎉. Always great to see new contributors.

I won't have a chance to review this today, but I'll be sure to give feedback once I take a look!

Comment on lines 1 to 5
// rustfmt-config: issue-5609.toml
// rustfmt-ignore_missing_submod: true

mod missing_submod;

Copy link
Contributor

Choose a reason for hiding this comment

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

The source file actually isn't necessary either. Since we're not doing any formatting to the file the target file is enough for this test.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, on second thought, I think keeping the source test is good to demonstrate that formatting is still happening. Can we explicitly set skip_children=false. skip_children=false is the current default, and when this is set we'll try to resolve sub modules. Again, this is the current default, but formatting would work just as well if skip_children=true, since we then wouldn't try to recursively resolve children, and therefore the ignore_missing_submod option wouldn't have any affect.

In case we change the default behavior of skip_children to be true in the future explicitly setting false will ensure this test keeps working as intended.

@@ -173,6 +173,7 @@ create_config! {
or they are left with trailing whitespaces";
ignore: IgnoreList, IgnoreList::default(), false,
"Skip formatting the specified files and directories";
ignore_missing_submod: bool, false, true, "Ignore missing submodule error";
Copy link
Contributor

Choose a reason for hiding this comment

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

All new options are unstable by default. Can you please change this to:

Suggested change
ignore_missing_submod: bool, false, true, "Ignore missing submodule error";
ignore_missing_submod: bool, false, false, "Ignore missing submodule error";

@ytmimi
Copy link
Contributor

ytmimi commented Nov 26, 2022

Can we also add the following tests to tests/rustfmt/main.rs

#[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));
}

The more important of the two is ignore_missing_sub_mod_false to explicitly test the behavior when the new ignore_missing_sub option is set to false, but having the ignore_missing_sub_mod_true case right next to it serves as an inline contrast that mimics the system test that you added in tests/target/issue-5609.rs.

@@ -1288,6 +1288,14 @@ If you want to ignore every file under the directory where you put your rustfmt.
ignore = ["/"]
```

## `ignore_missing_submod`

Ignore missing submodule error.
Copy link
Contributor

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.

Copy link
Author

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.


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

Choose a reason for hiding this comment

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

All new options are unstable by default, so we'll need to update this. We don't need to create a tracking issue for the option just yet, but we'll need to mark this as unstable.

@tanzaku
Copy link
Author

tanzaku commented Nov 27, 2022

Thank you for your review. All fixed.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 28, 2024

@tanzaku I know this one has been left on the backlog for a little while. I wanted to check back in and see if you were still interested in working on it.

@tanzaku
Copy link
Author

tanzaku commented Jan 29, 2024

Yes, I am still interested in working on it.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 29, 2024

Awesome! I wanted to confirm that before jumping back in. When you have a chance, can you rebase on the latest master and fix any merge conflicts.

@tanzaku tanzaku force-pushed the issue-5609-add-ignore_missing_submod-option branch from 03a5767 to 50e7197 Compare January 29, 2024 16:04
@tanzaku
Copy link
Author

tanzaku commented Jan 30, 2024

@ytmimi Sorry, I forgot to post earlier. I've rebased and resolved the conflicts.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 30, 2024

Thank you, I'll give this one another review later today!

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

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

First, I wanted to say thank you for your patience. I know this was left on the backlog for quite a while, and I'm glad that you're still interested in helping out with this one.

Overall, I think the implementation that you've put together would work if we went with a boolean option, however the team has had some discussions about shifting our error reporting towards an Error, Warn, and Ignore model. Given that we're likely heading in this direction for our other error reporting I'd really like to apply those variants to this new configuration option.

Comment on lines 1314 to 1315
- **Default value**: `false`
- **Possible values**: `true`, `false`
Copy link
Contributor

Choose a reason for hiding this comment

The 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 true and false to something like Warn, Error, Ignore. With that in mind I think it a more appropriate name would be something like report_missing_submod. Let me know if you can come up with anything better!

Copy link
Author

Choose a reason for hiding this comment

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

Update Configuration.md on this commit. 4bb0453

src/modules.rs Outdated
Comment on lines 255 to 262
.or_else(|err| match err.kind {
ModuleResolutionErrorKind::NotFound { file: _ }
if self.ignore_missing_submod =>
{
Ok(None)
}
_ => Err(err),
})
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to rework this with Error, Warn, and Ignore variants in mind. I think the only difference is that if Warn is set we'll emit an error message, but still continue with Ok(None).

Copy link
Author

Choose a reason for hiding this comment

The 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 eprintln! to output to stderr? Or is there a more appropriate method you could suggest?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed. f678369

For now, I implemented direct output with eprintln.
(However, I think I might be better off using FormatReport.)

@@ -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"))
}

Copy link
Contributor

Choose a reason for hiding this comment

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

will likely need to rework the tests slightly to accommodate Error, Warn, and Ignore variants.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed test on this commit. fcc06f9

@tanzaku tanzaku requested a review from ytmimi February 2, 2024 16:45
@ytmimi
Copy link
Contributor

ytmimi commented Feb 7, 2024

Thanks for pushing those changes. Just wanted to follow up and say that I'm planning to look at this one again later this week!

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

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

Just had a chance to look this over again and test out these changes locally. Things are working great! Thank you again for your help here. Marking this one as ready-to-merge, but will hold off on merging this one right away to give @calebcartwright a chance to look it over.

@ytmimi ytmimi added pr-ready-to-merge release-notes Needs an associated changelog entry and removed pr-waiting-on-author labels Feb 15, 2024
@ytmimi ytmimi mentioned this pull request Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-ready-to-merge release-notes Needs an associated changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to ignore modules that cannot be resolved
2 participants