esp-idf-svc icon indicating copy to clipboard operation
esp-idf-svc copied to clipboard

Rust analyzer not fully functional in esp-rs/esp-idf projects?

Open weiying-chen opened this issue 4 months ago • 6 comments

I generated an esp-rs/esp-idf project using the official template.

Then I modified it like this:

fn main() {
    use esp_idf_svc::{
        hal::prelude::Peripherals,
        sys::link_patches,
    };

    esp_idf_svc::log::EspLogger::initialize_default();

    let peripherals = Peripherals::take()?;
}

There were no warnings or errors in my text editor (Helix), which uses rust-analyzer. Is there something in a esp-rs/esp-if project that makes rust-analyzer not catch there common issues? The Rust compiler did catch the errors, though:

warning: unused import: `sys::link_patches`

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)

Note: if I go to a non-esp-rs/esp-if project, I do see these warnings and errors in my text editor.

Update: I think this line in Cargo.toml is deactivating those warnings and errors. But I still don't know why.

[dependencies]
esp-idf-svc = { version = "0.48", default-features = false }

weiying-chen avatar Feb 16 '24 13:02 weiying-chen

rust-analyzer works perfectly fine. Though the problem itself is more often that RA doesn't pick up the correct cargo configs. So it's more a matter of telling RA directly the same configs, that are included inside the .cargo/config.toml file, like target, feature flags etc. How you can configure RA is dependent on the integration in your editor/ide. Check how you can configure it on your env and look here for the possible settings. You can try out the extaArgs + extraEnv and the target ones. Note that there are settings for .cargo and for .check as they are for different RA invocations you may need to set both configs.

Vollbrecht avatar Feb 16 '24 15:02 Vollbrecht

This is my Helix log when I open a Rust project that has esp-idf-svc as a dependency:

2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "2024-02-17T11:37:28.907967Z ERROR rust_analyzer::main_loop: FetchBuildDataError:\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "error[E0531]: cannot find unit struct, unit variant or constant `SIGABRT` in crate `libc`\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "   --> /home/weiying-chen/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/test_result.rs:102:24\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "    |\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "102 |             Some(libc::SIGABRT) => TestResult::TrFailed,\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "    |                        ^^^^^^^ not found in `libc`\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "error: aborting due to 1 previous error\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "For more information about this error, try `rustc --explain E0531`.\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "error: could not compile `test` (lib) due to 2 previous errors\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "\n"
2024-02-17T19:37:28.908 helix_lsp::transport [ERROR] rust-analyzer err <- "\n"
2024-02-17T19:37:28.914 helix_term::application [WARN] unhandled window/showMessage: ShowMessageParams { typ: Warning, message: "Failed to run build scripts of some packages." }
2024-02-17T19:37:29.579 helix_term::application [WARN] unhandled window/showMessage: ShowMessageParams { typ: Warning, message: "Failed to run build scripts of some packages." }

I think the issue is Helix, not esp-idf-svc.

Note: I did rustup update (which also updated Rust nightly), but I'm getting the same error.

Update: Many people are having exactly the same issue. There's an issue with rust-analyzer and esp-idf projects.

weiying-chen avatar Feb 17 '24 11:02 weiying-chen

Thanks to this comment, I found out rust-analyzer works perfectly in esp-idf projects if you use rustc 1.77.0-nightly (2a3e63551 2023-12-30). The issue is rustc 1.78.0-nightly (ee9c7c940 2024-02-14).

weiying-chen avatar Feb 18 '24 05:02 weiying-chen

@weiying-chen I can't really confirm this. I've been having Problems with RA showing errors on exported crates which require ESP components, for example eventloop:

image

My toolchains are as following:

I am on rustc.177.0-nighty, my project however is using the esp toolchain: image

juliankrieger avatar Apr 12 '24 16:04 juliankrieger

@juliankrieger So your point is that RA also has issues with rustc.177.0-nighty when analyzing ESP projects?

weiying-chen avatar Apr 12 '24 16:04 weiying-chen

I have been running into the same issue. I upgraded my nightly to 1.79-0-nightly and now rust-analyzer is broken in all my esp projects.

julienvincent avatar Apr 19 '24 10:04 julienvincent