Skip to content

Improve out of line module resolution #5142

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

Merged
merged 1 commit into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
self.directory.path.push(path.as_str());
self.directory.ownership = DirectoryOwnership::Owned { relative: None };
} else {
let id = id.as_str();
// We have to push on the current module name in the case of relative
// paths in order to ensure that any additional module paths from inline
// `mod x { ... }` come after the relative extension.
Expand All @@ -468,9 +469,15 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
if let Some(ident) = relative.take() {
// remove the relative offset
self.directory.path.push(ident.as_str());

// In the case where there is an x.rs and an ./x directory we want
// to prevent adding x twice. For example, ./x/x
if self.directory.path.exists() && !self.directory.path.join(id).exists() {
return;
}
}
}
self.directory.path.push(id.as_str());
self.directory.path.push(id);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/mod_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ fn skip_out_of_line_nested_inline_within_out_of_line() {
&["tests/mod-resolver/skip-files-issue-5065/one.rs"],
);
}

#[test]
fn fmt_out_of_line_test_modules() {
// See also https://github.com/rust-lang/rustfmt/issues/5119
verify_mod_resolution(
"tests/mod-resolver/test-submodule-issue-5119/tests/test1.rs",
&[
"tests/mod-resolver/test-submodule-issue-5119/tests/test1.rs",
"tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub1.rs",
"tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub2.rs",
"tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub3/sub4.rs",
],
)
}
24 changes: 24 additions & 0 deletions tests/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::env;
use std::process::Command;
use std::path::Path;

/// Run the cargo-fmt executable and return its output.
fn cargo_fmt(args: &[&str]) -> (String, String) {
Expand Down Expand Up @@ -71,3 +72,26 @@ fn rustfmt_help() {
assert_that!(&["--", "-h"], contains("Format Rust code"));
assert_that!(&["--", "--help=config"], contains("Configuration Options:"));
}

#[test]
fn cargo_fmt_out_of_line_test_modules() {
// See also https://github.com/rust-lang/rustfmt/issues/5119
let expected_modified_files = [
"tests/mod-resolver/test-submodule-issue-5119/src/lib.rs",
"tests/mod-resolver/test-submodule-issue-5119/tests/test1.rs",
"tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub1.rs",
"tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub2.rs",
"tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub3/sub4.rs",
];
let args = [
"-v",
"--check",
"--manifest-path",
"tests/mod-resolver/test-submodule-issue-5119/Cargo.toml",
];
let (stdout, _) = cargo_fmt(&args);
for file in expected_modified_files {
let path = Path::new(file).canonicalize().unwrap();
assert!(stdout.contains(&format!("Diff in {}", path.display())))
}
}
8 changes: 8 additions & 0 deletions tests/mod-resolver/test-submodule-issue-5119/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "rustfmt-test-submodule-issue"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
7 changes: 7 additions & 0 deletions tests/mod-resolver/test-submodule-issue-5119/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub fn foo() -> i32 {
3
}

pub fn bar() -> i32 {
4
}
8 changes: 8 additions & 0 deletions tests/mod-resolver/test-submodule-issue-5119/tests/test1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod test1 {
#[cfg(unix)]
mod sub1;
#[cfg(not(unix))]
mod sub2;

mod sub3;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use rustfmt_test_submodule_issue::foo;

#[test]
fn test_foo() {
assert_eq!(3, foo());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use rustfmt_test_submodule_issue::bar;

#[test]
fn test_bar() {
assert_eq!(4, bar());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod sub4;
Empty file.