Skip to content

fix: Expand proc-macros in workspace root, not package root #17973

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
Aug 27, 2024

Conversation

Veykril
Copy link
Member

@Veykril Veykril commented Aug 27, 2024

Should fix #17748. The approach is generally not perfect though as rust-project.json projects don't benefit from this (still, nothing changes in that regard)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 27, 2024
@Veykril Veykril force-pushed the proc-macro-curr-dir branch 3 times, most recently from cf55624 to 70ff8fa Compare August 27, 2024 11:06
@Veykril Veykril force-pushed the proc-macro-curr-dir branch from 70ff8fa to 9a47e6f Compare August 27, 2024 11:40
@Veykril
Copy link
Member Author

Veykril commented Aug 27, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Aug 27, 2024

📌 Commit 9a47e6f has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 27, 2024

⌛ Testing commit 9a47e6f with merge 06a40a6...

@bors
Copy link
Contributor

bors commented Aug 27, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 06a40a6 to master...

@bors bors merged commit 06a40a6 into rust-lang:master Aug 27, 2024
11 checks passed
@Veykril Veykril deleted the proc-macro-curr-dir branch August 27, 2024 12:15
@lnicola lnicola changed the title Expand proc-macros in workspace root, not package root fix: Expand proc-macros in workspace root, not package root Aug 27, 2024
@max-sixty
Copy link

Thank you!

@max-sixty
Copy link

max-sixty commented Sep 9, 2024

@Veykril thanks for giving this a go.

Unfortunately I don't think it solved the problem. The repro in #17748 still fails. I'm on v0.3.2096 which contains this commit.

If I augment this to the repro, on nightly rustc:

#[proc_macro]
pub fn test_each_file(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    dbg!(env!("CARGO_MANIFEST_DIR"));
    dbg!(std::env::var("CARGO_RUSTC_CURRENT_DIR"));

    let current_dir = dbg!(std::env::current_dir().unwrap());
    assert!(dbg!(Path::new("./ROOT.txt")).is_file());
    assert!(dbg!(Path::new("./ROOT.txt").canonicalize().unwrap()).is_file());
    proc_macro::TokenStream::new()
}

...then cargo build shows that it's using the workspace root as cwd:

[test_macro/src/lib.rs:5:5] env!("CARGO_MANIFEST_DIR") = "/Users/maximilian/workspace/test-each-file/test_macro"
[test_macro/src/lib.rs:6:5] std::env::var("CARGO_RUSTC_CURRENT_DIR") = Ok(
    "/Users/maximilian/workspace/test-each-file",
)
[test_macro/src/lib.rs:8:23] std::env::current_dir().unwrap() = "/Users/maximilian/workspace/test-each-file"
[test_macro/src/lib.rs:9:13] Path::new("./ROOT.txt").canonicalize().unwrap() = "/Users/maximilian/workspace/test-each-file/ROOT.txt"
[test_macro/src/lib.rs:10:13] Path::new("./ROOT.txt") = "./ROOT.txt"

But Rust Analyzer Language Server log doesn't find the newly added CARGO_RUSTC_CURRENT_DIR var and is still using the crate root:

[test_macro/src/lib.rs:5:5] env!("CARGO_MANIFEST_DIR") = "/Users/maximilian/workspace/test-each-file/test_macro"
[test_macro/src/lib.rs:6:5] std::env::var("CARGO_RUSTC_CURRENT_DIR") = Err(
    NotPresent,
)
[test_macro/src/lib.rs:8:23] std::env::current_dir().unwrap() = "/Users/maximilian/workspace/test-each-file/test_src"

(if you don't see this, I'll open a new issue, hope that's reasonable, thank you!)

@Veykril
Copy link
Member Author

Veykril commented Sep 9, 2024

Oh I see the issue, the env var is set for the proc-macro crate, but we need to it to be set for the calling crate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect cwd during macro expansion
4 participants