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

ProcMacroSrv executed outside of workspace directory #11518

Closed
mattsse opened this issue Feb 21, 2022 · 4 comments
Closed

ProcMacroSrv executed outside of workspace directory #11518

mattsse opened this issue Feb 21, 2022 · 4 comments
Assignees
Labels
C-support Category: support questions S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@mattsse
Copy link

mattsse commented Feb 21, 2022

rust-analyzer version: (eg. output of "Rust Analyzer: Show RA Version" command)
rust-analyzer version: 02904e9 2022-02-14 stable

rustc version: (eg. output of rustc -V)
rustc 1.58.1 (db9d1b20b 2022-01-20)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME)

It seems like during proc-macro expansion in ProcMacroSrv::expand the task's current dir is not the workspace directory.
I'm not sure how to debug this properly but what I suspect is happening is that the cwd and env vars of the ExpandMacro are not the workspace and workspace env vars. so that, if a proc-macro that expands depending on CARGO_MANIFEST_DIR and expects this path to point to the current workspace, it can generate different token trees for RA inside ProcMacroSrv.

@lnicola
Copy link
Member

lnicola commented Feb 21, 2022

I don't think rustc guarantees any working directory. But IIUC, this should have been implemented in #11353.

@mattsse
Copy link
Author

mattsse commented Feb 21, 2022

Right, this rather concerns the env vars, however the cwd also appears to set to an imported crate.
and since CARGO_MANIFEST_DIR is set to an imported crate and not the current workspace, Rust Analyzer: Expand macro recursively shows the code that was expanded while not being in the current workspace.

#11353 seems to be related, but not sure if that's already included in rust-analyzer version: 02904e9 2022-02-14 stable?

@lnicola
Copy link
Member

lnicola commented Feb 21, 2022

Yeah, #11353 should be included in that version.

@flodiebold flodiebold added C-support Category: support questions S-unactionable Issue requires feedback, design decisions or is blocked on other work labels Jun 28, 2022
@Veykril Veykril self-assigned this Sep 16, 2023
@Veykril
Copy link
Member

Veykril commented Jan 4, 2024

Just looked into this and this seems to work fine?

#[proc_macro]
pub fn foo(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let ct_env = env!("CARGO_MANIFEST_DIR");
    let rt_env = format!("{:?}", std::env::var("CARGO_MANIFEST_DIR"));
    quote::quote!((#ct_env, #rt_env)).into()
}

expands to the same stuff in r-a and rustc. So unless we get more specifics I'll close this for the time being.

@Veykril Veykril closed this as completed Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Category: support questions S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

4 participants