Skip to content

rustfmt fix: allow file not found errors for external mods annotated with #[my_macro]#159737

Open
ytmimi wants to merge 1 commit into
rust-lang:mainfrom
ytmimi:rustfmt_issue_6959
Open

rustfmt fix: allow file not found errors for external mods annotated with #[my_macro]#159737
ytmimi wants to merge 1 commit into
rust-lang:mainfrom
ytmimi:rustfmt_issue_6959

Conversation

@ytmimi

@ytmimi ytmimi commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Tracking issue: #54727

#[my_macro] was stabilized for macro hygiene 2.0 in #157857. There isn't a guarantee that the external module exists on the file system so ignore any file not found errors encountered when trying to resolve the module's file.

Fixes rust-lang/rustfmt#6959

r? @petrochenkov

cc: @TimNN

@rustbot

rustbot commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

rustfmt is developed in its own repository. If possible, consider making this change to rust-lang/rustfmt instead.

cc @rust-lang/rustfmt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Jul 22, 2026
@ytmimi ytmimi changed the title rustfmt fix: allow mod resolution errors for mods annotated with #[my_macro] rustfmt fix: allow file not found errors for external mods annotated with #[my_macro] Jul 22, 2026
@ytmimi
ytmimi force-pushed the rustfmt_issue_6959 branch from e679de0 to 0a54058 Compare July 22, 2026 19:10
Comment thread src/tools/rustfmt/src/utils.rs Outdated
.filter(|attr| matches!(attr.style(), AttrStyle::Outer))
.any(|attr| {
attr.meta().is_some_and(|meta_item| match meta_item.kind {
MetaItemKind::Word => meta_item.has_name(Symbol::intern("my_macro")),

@TimNN TimNN Jul 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"my_macro" was only an example. This could be any custom attribute macro.

View changes since the review

@ytmimi ytmimi Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, in that case if there are any outer attributes then we could ignore the file not found error. I don't think there's a reliable way for rustfmt to know if this is a custom vs built-in attribute macro.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it doesn't even have to be a macro, necessarily:

#[cfg(false)]
mod x;

so this was a problem long before macros on modules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it doesn't even have to be a macro, necessarily:

#[cfg(false)]
mod x;

so this was a problem long before macros on modules.

rustfmt has always assumed that it could find the source file for externally defined modules. rustfmt only operates on the AST of your program so it just sees an external module declaration that has some outer attributes. Just because something isn't going to be compiled doesn't mean that we shouldn't format the content.

As mentioned in rust-lang/rustfmt#6959 (comment), the only time rustfmt won't try to resolve external modules is when it's running on input from stdin or if you're running rustfmt with the --skip-children command line flag.

@ytmimi
ytmimi force-pushed the rustfmt_issue_6959 branch from 0a54058 to 3eb2b02 Compare July 23, 2026 03:07
Comment thread src/tools/rustfmt/src/modules.rs Outdated
ModError::FileNotFound(_, default_path, _secondary_path) => {
if attrs
.iter()
.any(|a| matches!(a.style(), ast::AttrStyle::Outer))

@petrochenkov petrochenkov Jul 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This condition is always true if the module is not loaded.

rustfmt has access to compiler crates, so it can use is_builtin_attr_name or is_builtin_attr to detect non-built-in attributes.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for pointing that out. I think that would help narrow the scope in which we ignore the error.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 23, 2026
…om outer attributes

It's possible that at least one of the attributes is a custom proc macro that
takes the module tokens as an input. It's hard to know for sure since rustfmt
only operates on the AST pre-expansion. In this case we'll be overly permissive
and just ignore the file not found error so rustfmt can still try formatting the
input.

Fixes rustfmt issue 6959
@ytmimi
ytmimi force-pushed the rustfmt_issue_6959 branch from 3eb2b02 to baffab0 Compare July 23, 2026 13:31
@ytmimi

ytmimi commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

@petrochenkov thanks for pointing out that I could use is_builtin_attr_name.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustfmt fails to resolve mod foo; decorated by an attribute macro

5 participants