-
Notifications
You must be signed in to change notification settings - Fork 13k
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
path attribute on modules is useless in nested modules #35016
Comments
@jethrogb I guess the issue is that both behaviours are reasonable, it depends on your mental model of how |
Given that one option results in unresolvable paths, I'd say the other option is better. I think it could be non-breaking by testing whether the current way works, and if it doesn't work try the other way. |
Another option is to let rustc, instead of the OS, resolve |
The following works for this use case: #[path = "../tests"]
mod tests {
#[path = "support/mod.rs"]
mod support; // expects "../tests/support/mod.rs" (relative to source file directory)
// --- or just ---
mod support; // expects "../tests/support/mod.rs" or "../tests/support.rs"
} If we were designing from scratch, I would probably prefer to have all paths be relative to the source file's directory. This example is reasonable enough; the paths are relative to the current module's directory, i.e. the directory in which However, when That being said, I think the status quo here is acceptable enough that falling back to file-relative paths isn't worth the extra language complexity. |
This issue is common enough that I think it would be worth checking the file-relative path before emitting the "no such file" error. If the path is file-relative, we would give an informative error message, ideally with an error code that can explains |
I'm not sure that adding a logic in such a case is a good idea. The error seems pretty explicit for me. However, we could still try to resolve the path instead of having this ".." in the middle. |
I'm wary of allowing more paths than we do today, but perhaps we could check if the user is trying to |
I'm feeling uneasy with having "hidden" logic in here. It's possible that the user wants the |
So after explanations, it's not about adding a new mechanism but clarifying the error. Therefore, I completely agree! 👍 |
Any progress here? I am getting this error, and have been trying to work around it for 10 minutes after reading all the comments without any luck. I don't understand how any workaround for this could even work. AFAICT the problem is that the relative path specified in Given two files:
and writing the following in lib.rs: mod foo {
#[path = "bar.rs"]
mod bar;
} will try to find mod foo {
#[path = "../bar.rs"]
mod bar;
} instead, rustc will try to find Note how both paths contain mod foo {
#[path = "X/bar.rs"]
mod bar;
} will find If the answer is yes, then this is an error message bug, and the error message should suggest the appropriate path to use. If the answer is no, this is either a bug in |
I've run into the problem @gnzlbg described in #35016 (comment) both in |
Found this issue from a tail end of E-mentor issues, seems like something I could help solve. As far as I understand the situation is illustrated by the original issue description, a file ends up being opened at a non-canonicalized path showing an error like:
I looked into introducing Regarding the "more original" better error fix the originally suggested hint of using two Another point raised in the error discussion about generally not being able to escape inline modules with Would like to work on getting this fixed, one way or the another but there's a decision to be made. |
Continued exploration on this. It would seem that
This wouldn't work without removing the current behaviour of giving inline modules a directory path. As in the modified original example of: // in src/lib.rs
mod this_is_inline {
#[path = "../support.rs"]
mod support;
} Here a direct application of
|
Another wrinkle is that the current behavior (as of rust 1.65 today) is not consistent between platforms. Given this code: mod foo {
#[path = "../bar.rs"]
mod bar;
} This will work on Windows, but not Linux |
If you're looking for a solution, this may suffice: // In, say, mod.rs
#[path="."]
mod foo {
#[path="."]
mod bar {
mod baz;
}
} This will load |
My crate has some test support data in
tests/support/mod.rs
. When trying to load this module for unit tests like this:I get an error like this:
In fact, specifying pretty much any path is going to fail, because the
src/tests/
directory does not exist, because I'm using a nested module and not a separate file. When using nested modules, I think the path at which relative path resolution should start should be the directory that the actual source file is in.The text was updated successfully, but these errors were encountered: