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

Rustfmt cannot resolve submodules of integration tests #5119

Closed
walkie opened this issue Dec 2, 2021 · 2 comments · Fixed by #5142
Closed

Rustfmt cannot resolve submodules of integration tests #5119

walkie opened this issue Dec 2, 2021 · 2 comments · Fixed by #5142
Labels
a-mods Module resolution. a-parser

Comments

@walkie
Copy link

walkie commented Dec 2, 2021

Overview

Rustfmt cannot find a submodule of an integration test located in a separate file.

Running cargo fmt on a project that contains a submodule test1/sub1 implemented by the file tests/test1/sub1.rs produces the following error message:

Error writing files: failed to resolve mod `sub1`: $PACKAGE/tests/test1/test1 does not exist

Submodules in integration tests can be created as follows:

In tests/test1.rs:

mod test1 {
    mod sub1;
    mod sub2;
}

The submodules sub1 and sub2 can be implemented in the files tests/test1/sub1.rs and tests/test1/sub2.rs.

This structure works with cargo build, cargo test, and cargo clippy, but fails for cargo fmt with the error message given above.

Steps to reproduce

Clone this repo containing a minimal example illustrating the problem: https://github.com/walkie/rustfmt-test-submodule-issue

From within the repo:

  • Run cargo test and observe that the package successfully compiles and the tests in both submodules run and pass.
  • Run cargo fmt and observe the following error message:
Error writing files: failed to resolve mod `sub1`: [...]/rustfmt-test-submodule-issue/tests/test1/test1 does not exist

The same error can also be produced by rustfmt directly with the command: rustfmt tests/test1.rs

Expected behavior

  • No Rustfmt I/O errors. :-)

Other information

  • Installed Rustfmt by rustup component add rustfmt
  • Tested with versions 1.51, 1.57, and 1.59.0-nightly (48a5999fc 2021-12-01)
  • Tested on Fedora Workstation 34 and MacOS 11.6.1

The error occurs in all combinations of the above.

@walkie
Copy link
Author

walkie commented Dec 2, 2021

As a workaround, you can tell Rustfmt to skip the submodules when formatting the entire package:

mod test1 {
    #[rustfmt::skip]
    mod sub1;
    #[rustfmt::skip]
    mod sub2;
}

This will enable cargo fmt to complete successfully. You can then apply Rustfmt to each submodule directly:

  • rustfmt tests/test1/sub1.rs
  • rustfmt tests/test1/sub2.rs

@ytmimi
Copy link
Contributor

ytmimi commented Dec 20, 2021

@walkie I wanted to thank you for putting together a great minimal example. It definitely made it a whole lot easier for me as I was working to address this issue. 🤞 the PR review goes smoothly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-mods Module resolution. a-parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants