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

Linker error on proc macros that depend on defmt #14190

Closed
p-avital opened this issue Feb 22, 2023 · 21 comments
Closed

Linker error on proc macros that depend on defmt #14190

p-avital opened this issue Feb 22, 2023 · 21 comments
Labels
A-proc-macro proc macro C-bug Category: bug E-unknown It's unclear if the issue is E-hard or E-easy without digging in

Comments

@p-avital
Copy link

p-avital commented Feb 22, 2023

First of all, thank you for the immense work you've been doing.

Issue description

Apologies, I haven't been able to make a minimal reproduction of the issue I'm running into while working on proc-macros in zenoh's keformatter branch. I'll try to provide as much information as possible.

Behaviour

Since working on new proc-macros last week, rust-analyzer keeps on showing a failed to run build scripts error, stating in server logs that it "could not compile zenoh-macros" (belongs to the workspace, commons/zenoh-macros).

In VSCode, this causes every usage of zenoh-macros's members to be marked with "proc macro unstable not expanded: no proc macro dylib present" (unstable for instance expands correctly on master).

In Lapce, the error notification appears, but macros appear to be expanded correctly regardless, since type inference and documentation in the examples/examples/z_formats.rs file works.

Note that when building the project, zenoh-macros builds fine, so this issue seems to be RA-specific.

Suspected cause

It appears that rust-analyzer drives the build slightly differently, leading to invalid linkage description files, since linkers on both Windows and Linux complain about syntax errors in lib.def and list (passed through --version-script) respectively.

Server logs on Windows
Server logs on Linux

The directories that contain the incriminated files are deleted by the time I get their path in the logs, so I haven't been able to inspect the files to try and find out more.

Impacted platforms

Linux (Manjaro)

rust-analyzer version: 0.3.1412-standalone
rustc version: rustc 1.67.1 (d5a82bbd2 2023-02-07)
relevant settings: None to my knowledge

Windows

rust-analyzer version: 0.3.1410-standalone (a6603fc 2023-02-16)
rustc version: rustc 1.67.1 (d5a82bbd2 2023-02-07)
relevant settings: None to my knowledge

@p-avital p-avital added the C-bug Category: bug label Feb 22, 2023
@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

Does it work if you disable rust-analyzer.cargo.buildScripts.useRustcWrapper?

@p-avital
Copy link
Author

I just gave it a go (restarted all of VSCode for good measure), it doesn't :(

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

Maybe try a cargo clean -p zenoh-macros with Code closed.

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

If disabling the wrapper helps, you can try setting this as a workaround: https://github.com/lnicola/rust-analyzer/blob/563bd9c24a0620906e4640f57262ea33e48687f0/crates/rust-analyzer/src/bin/rustc_wrapper.rs#L24.

EDIT: no, that won't work.

@p-avital
Copy link
Author

Failed again after cargo cleaning. Deleting target also doesn't help either :(

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

Strange, for me it works without disabling that setting. Expanding the second macro call in that example, I get formatter.user_id(42).and_then(|x| x.file(file)).

My version.rs says:

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6

@p-avital
Copy link
Author

That's the expected expansion (and the one you get through cargo expand), I really don't get why the crate doesn't link exclusively in RA...

It started happening when I first added the zenoh-protocol dependency in zenoh-macros, all the other macros always worked fine before that...

@p-avital
Copy link
Author

p-avital commented Feb 22, 2023

Ahah, removing ALL of my rust related settings did work, now to find the true culprit. Thanks for letting me know that it could work, I have Settings Sync enabled so whatever setting is messing it up, it was synced between my machines.

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

Did you check the contents of version.rs when it failed? I don't know of any settings we have that might cause this except for the wrapper one (in addition to the toolchain version).

Of course, version.rs might be fine. The error is about a linker script, not something we would touch.

@p-avital
Copy link
Author

rustc --version --verbose gives me the exact same thing :)

Still looping through settings to find the issue

@p-avital
Copy link
Author

p-avital commented Feb 22, 2023

AHAH: "rust-analyzer.cargo.allFeatures": true seems to be the thing that breaks zenoh-macros (kinda sad, we use features a lot in zenoh, and all-features is a very good sanity check for that)

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

I can confirm, enabling defmt causes this.

@p-avital
Copy link
Author

Aight, we were debating whether defmt support was worth it, no I know I'm murdering it, thank you :D

@p-avital
Copy link
Author

Again, thank you very much, you've been very helpful and very quick. Feel free to close this issue, unless you want to keep track of that defmt thing.

I am satisfied with my care :)

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

It's a bit weird. We seem to load defmt fine, even with --all-features, and same for uhlc. Going out on a limb, maybe @jonas-schievink has an idea about how defmt might interfere with the linker.

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

And thanks, by the way!

@lnicola
Copy link
Member

lnicola commented Feb 22, 2023

Oh, it actually has a linker script: https://github.com/knurling-rs/defmt/blob/main/defmt/defmt.x.in.

@lnicola lnicola changed the title Linkage of proc_macro crates fails on rust-analyzer exclusively. Linker error on proc macros that depend on defmt Feb 22, 2023
@lnicola lnicola added E-unknown It's unclear if the issue is E-hard or E-easy without digging in A-proc-macro proc macro labels Feb 22, 2023
@p-avital
Copy link
Author

I might have done it wrong, but I tried adding the following

if env::var_os("DEP_DEFMT_LINKS").is_some() {
    println!("cargo:rustc-link-arg-examples=-Tdefmt.x");
}

to zenoh-macros/build.rs, to no avail. We were dropping defmt anyway, but if you see something I'm obviously doing wrong, I'm always happy to learn :)

@p-avital
Copy link
Author

We've dropped defmt on zenoh, and I'm going to update the problematic branch.

For future reference, the I'll update the link which pointed to the branch to point to the last problematic commit

@Veykril
Copy link
Member

Veykril commented Jan 3, 2025

Is this still an issue?

@lnicola
Copy link
Member

lnicola commented Jan 3, 2025

I can't spot any problems on bd645773e65912428d09fd408009de99352fcd73 with all features enabled, let's close this.

@lnicola lnicola closed this as completed Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macro proc macro C-bug Category: bug E-unknown It's unclear if the issue is E-hard or E-easy without digging in
Projects
None yet
Development

No branches or pull requests

3 participants