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

fails to traverse from mod foo to foo.rs in some cases #115

Closed
sourcefrog opened this issue Apr 26, 2023 · 0 comments · Fixed by #183
Closed

fails to traverse from mod foo to foo.rs in some cases #115

sourcefrog opened this issue Apr 26, 2023 · 0 comments · Fixed by #183
Assignees
Labels
bug Something isn't working trial Reports from trying cargo-mutants on some tree

Comments

@sourcefrog
Copy link
Owner

sourcefrog commented Apr 26, 2023

In 9617adc5ddad3603d730dc3306870364624625bf of https://github.com/openmls/openmls, cargo-mutants 765fca9 says

   0.561492717s  WARN traits/src/traits.rs:6: referent of mod "crypto" not found: tried ["/home/mbp/src/openmls/traits/src/traits/crypto.rs", "/home/mbp/src/openmls/traits/src/traits/crypto/mod.rs"]
   0.561519127s  WARN traits/src/traits.rs:7: referent of mod "key_store" not found: tried ["/home/mbp/src/openmls/traits/src/traits/key_store.rs", "/home/mbp/src/openmls/traits/src/traits/key_store/mod.rs"]
   0.561535347s  WARN traits/src/traits.rs:8: referent of mod "random" not found: tried ["/home/mbp/src/openmls/traits/src/traits/random.rs", "/home/mbp/src/openmls/traits/src/traits/random/mod.rs"]
   0.561550568s  WARN traits/src/traits.rs:9: referent of mod "signatures" not found: tried ["/home/mbp/src/openmls/traits/src/traits/signatures.rs", "/home/mbp/src/openmls/traits/src/traits/signatures/mod.rs"]
   0.561570058s  WARN traits/src/traits.rs:10: referent of mod "types" not found: tried ["/home/mbp/src/openmls/traits/src/traits/types.rs", "/home/mbp/src/openmls/traits/src/traits/types/mod.rs"]
Found 1287 mutants to test
Unmutated baseline ... ok in 40.8s build + 46.8s test

It still carries on with testing but is perhaps failing to generate some mutants.

This crate has

.
├── Cargo.toml
├── CHANGELOG.md
├── README.md
└── src
    ├── crypto.rs
    ├── key_store.rs
    ├── random.rs
    ├── signatures.rs
    ├── traits.rs
    └── types.rs

So it should find mod traits in src/traits.rs. I think it's confused because the top-level file is traits.rs, not lib.rs, due to configuration in Cargo.toml.

Specifically, openmls/traits/Cargo.toml has

[lib]
path = "src/traits.rs"

This code

cargo-mutants/src/visit.rs

Lines 250 to 298 in 765fca9

// If there's no content in braces, then this is a `mod foo;`
// statement referring to an external file. We find the file name
// then remember to visit it later.
//
// Both the current module and the included sub-module can be in
// either style: `.../foo.rs` or `.../foo/mod.rs`.
//
// If the current file ends with `/mod.rs`, then sub-modules
// will be in the same directory as this file. Otherwise, this is
// `/foo.rs` and sub-modules will be in `foo/`.
//
// Having determined the directory then we can look for either
// `foo.rs` or `foo/mod.rs`.
if node.content.is_none() {
let my_path: &Utf8Path = self.source_file.tree_relative_path().as_ref();
// Maybe matching on the name here is no the right approach and
// we should instead remember how this file was found?
let dir = if my_path.ends_with("mod.rs")
|| my_path.ends_with("lib.rs")
|| my_path.ends_with("main.rs")
{
my_path.parent().expect("mod path has no parent").to_owned()
} else {
my_path.with_extension("")
};
let mut found = false;
let mut tried_paths = Vec::new();
for &ext in &[".rs", "/mod.rs"] {
let relative_path = TreeRelativePathBuf::new(dir.join(format!("{mod_name}{ext}")));
let full_path = relative_path.within(&self.root);
if full_path.is_file() {
trace!("found submodule in {full_path}");
self.more_files.push(relative_path);
found = true;
break;
} else {
tried_paths.push(full_path);
}
}
if !found {
warn!(
"{path}:{line}: referent of mod {mod_name:#?} not found: tried {tried_paths:?}",
path = self.source_file.tree_relative_path,
line = node.mod_token.span.start().line,
);
}
}
self.in_namespace(mod_name, |v| syn::visit::visit_item_mod(v, node));
}
assumes the top level file will always be lib.rs or main.rs but that's not reliable.

Maybe we ought to remember if the file is a top source file for the target and use that instead: see https://doc.rust-lang.org/cargo/reference/cargo-targets.html. We probably already have this in the cargo metadata.

@sourcefrog sourcefrog added bug Something isn't working trial Reports from trying cargo-mutants on some tree labels Apr 26, 2023
@sourcefrog sourcefrog self-assigned this Dec 11, 2023
sourcefrog added a commit that referenced this issue Dec 11, 2023
Remember whether the file is a target top, rather than
relying on the file name.

Fixes #115
but needs tests
@sourcefrog sourcefrog linked a pull request Dec 11, 2023 that will close this issue
1 task
sourcefrog added a commit that referenced this issue Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working trial Reports from trying cargo-mutants on some tree
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant