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

std::file claims that it returns the name of a file, but it returns a relative path #117605

Closed
bonsairobo opened this issue Nov 5, 2023 · 8 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@bonsairobo
Copy link

Location

https://doc.rust-lang.org/std/macro.file.html

Summary

The docs say:

Expands to the file name in which it was invoked.

But in fact, a relative path is returned. For example, if we have src/main.rs:

fn main() {
    println!("{}", std::file!())
}

This prints "src/main.rs" (on GNU/Linux).

I believe this will be a path relative to the workspace root, though an expert should confirm this.

@bonsairobo bonsairobo added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Nov 5, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 5, 2023
@bjorn3
Copy link
Member

bjorn3 commented Nov 5, 2023

I believe this will be a path relative to the workspace root, though an expert should confirm this.

Cargo will invoke rustc for local crates with the workspace root as working directory and for dependencies from crates.io with their source directory as working directory. For local crates it then passes a relative path as filename and for crates.io dependencies the absolute path as filename to rustc. file!() is relative based on the original filename that was passed to rustc.

@bonsairobo
Copy link
Author

file!() is relative based on the original filename that was passed to rustc.

I'm not sure I understand this part. Could you give an example?

@bjorn3
Copy link
Member

bjorn3 commented Nov 5, 2023

If you pass src/lib.rs to rustc as filename and you have a module foo at src/foo.rs, then file!() will return src/foo.rs when invoked inside that module. If you pass /path/to/lib.rs then it will return /path/to/foo.rs when invoked inside the foo module.

@bonsairobo
Copy link
Author

bonsairobo commented Nov 5, 2023

Is there any way to test the crates.io behavior without first publishing the crate?

EDIT: To clarify, it would be nice to test behavior that relies on file!() in the case where your crate is being used as a crates.io dependency. Ideally the test would not involve manually invoking rustc.

(Sorry if this is a little off topic)

@bonsairobo
Copy link
Author

And can I assume that the path would depend on the operating system? E.g. on Windows you would get an absolute path as C:\\path\to\crate\src\lib.rs?

@bjorn3
Copy link
Member

bjorn3 commented Nov 5, 2023

Is there any way to test the crates.io behavior without first publishing the crate?

Yes, same behavior applies to git dependencies AFAIK, so you could create a git repo somewhere and depend on it.

And can I assume that the path would depend on the operating system? E.g. on Windows you would get an absolute path as C:\path\to\crate\src\lib.rs?

Yes

@saethlin saethlin added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 7, 2023
@Dylan-DPC
Copy link
Member

Closing this as it's not an issue and it works as intended

@bonsairobo
Copy link
Author

@Dylan-DPC Can you tell me why it's "not an issue and working as intended"? I find the documentation to be both factually incorrect and lacking a thorough explanation of how the macro actually behaves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants