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

Incorrect build issue displayed: "found duplicate lang item panic_impl" #4490

Closed
jackgerrits opened this issue May 16, 2020 · 17 comments
Closed

Comments

@jackgerrits
Copy link

jackgerrits commented May 16, 2020

I am following the writing an OS in Rust tutorials and in the first section it describes creating a no_std binary and you must specify the panic handler. The following code compiles file but rust-analyzer believes there is an error and shows it in VSCode.

#![no_std]
#![no_main]

use core::panic::PanicInfo;

/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
    loop {}
}

rsz_screenshot_from_2020-05-16_17-46-26

found duplicate lang item `panic_impl`
the lang item is first defined in crate `std` (which `test` depends on)rustc(E0152)

I tried setting the target to one that doesn't link with the OS, settings.json:

 "rust-analyzer.cargo.target": "thumbv7em-none-eabihf"

Rust-analyzer extension version: 0.2.166

@edwin0cheng
Copy link
Member

This error is coming from rustc, what's the result if you run cargo check in the terminal ?

@jackgerrits
Copy link
Author

I added a custom target and updated .cargo/config to be:

[build]
target = "x86_64-rustos.json"

and defined the target as follows:

//x86_64-rustos.json
{
    "llvm-target": "x86_64-unknown-none",
    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "none",
    "executables": true,
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "panic-strategy": "abort",
    "disable-redzone": true,
    "features": "-mmx,-sse,+soft-float"
}

This fixed the initial error from the issue. However, when I run cargo check and also in VSCode it displays this error:

    Checking rustos v0.1.0 (/home/jack/w/repos/rust-os-copy)
error[E0463]: can't find crate for `core`
  |
  = note: the `x86_64-rustos-6314987440205109011` target may not be installed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `rustos`.

To learn more, run the command again with --verbose.

When I actually build the project I must use xbuild/xcheck because the custom target I have defined of course does not have the pre-built core crate included with the compiler. So the x-command builds core along with the target itself for the specified default target.

How does rust-analyzer deal with project that must be cross built and cannot be built directly for the host OS?

@edwin0cheng
Copy link
Member

edwin0cheng commented May 18, 2020

You can change the checkOnSave. settings, for example for rustc development:

{
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./x.py",
        "check",
        "--json-output"
    ]
}

In your case, maybe something like this will works:

{
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./cargo",
        "xcheck",
        "--json-output"
    ]
}

(Not sure xcheck support --json-output)

@jackgerrits
Copy link
Author

Yes, that looks like it has resolved it.

Thanks so much for your reply and also for your work on rust-analyzer. It is a fantastic project!

@thorlucas
Copy link

I'm a bit confused as to the solution. Where is this checkOnSave attribute overridden? Can I do this on a per project basis?

@bjorn3
Copy link
Member

bjorn3 commented Jun 15, 2020

ctrl-shift-p > "Preferences: Open Settings (JSON)" for the global settings. ctrl-shift-p > "Preferences: Open Workspace Settings (JSON)" or .vscode/settings.json for the workspace settings.

@thorlucas
Copy link

I'm not on VSCode. I'm using coc-rust-analyzer. Is this an IDE specific thing?

@bjorn3
Copy link
Member

bjorn3 commented Jun 15, 2020

Is this an IDE specific thing?

Yes, every IDE has it's own way to pass settings to a language server.

@naveedpash
Copy link

I'm not on VSCode. I'm using coc-rust-analyzer. Is this an IDE specific thing?

You're using neovim yes?

:CocConfig<CR>

Then copy and paste @edwin0cheng 's response

:)

@lf-
Copy link
Contributor

lf- commented Sep 1, 2020

Hi! I'm also getting this, but my cargo check displays no error output on a x86_64-unknown-linux-gnu. I am developing a very thin rust program indeed, but it is running on Linux rather than bare metal (it's shellcode, for a CTF challenge, injected into a x86_64 Linux process). How can I continue debugging this?

.cargo/config:

[build]
rustflags = ["-C", "link-arg=-nostdlib", "-C", "link-arg=-static"]

src/main.rs:

#![no_std]
#![no_main]

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
fn _start() -> ! {
    loop {}
}

Cargo.toml:

[package]
name = "shellcode"
edition = "2018"
version = "0.0.0"

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

@bjorn3
Copy link
Member

bjorn3 commented Sep 1, 2020

Does cargo check --all-targets result in an error? If so you can set rust-analyzer.checkOnSave.allTargets to false.

@lf-
Copy link
Contributor

lf- commented Sep 1, 2020

@bjorn3 you hit the nail right on the head! I just found this independently with bpftrace's execsnoop before looking at GitHub again 😅.

Thanks!

@briankung
Copy link

Does anyone know how to set rust-analyzer.checkOnSave.allTargets to false for Sublime Text users?

abdes added a commit to setros/bootcom that referenced this issue Nov 3, 2020
@andyduplain
Copy link

Sorry to necro this issue, but I had the same issue as the OP and I think I solved it using #[cfg(not(test))]:

#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

The function is then greyed-out within VSCode.

mstange added a commit to mstange/samply that referenced this issue Dec 16, 2022
It was displaying a "duplicate lang item" error on the panic handler.
This was also displayed when running "cargo check --all-targets".
The workaround to add #[cfg(not(test))] is from rust-lang/rust-analyzer#4490 (comment)
@pacifi5t
Copy link

pacifi5t commented Jan 7, 2023

I recently had the same issue when using rust-analyzer (v0.3.1348) and CLion with a Rust plugin. I was targeting my MCU, so the solution was to set the target in project-directory/.cargo/config

[build]
target = "thumbv7em-none-eabihf"

@ghost
Copy link

ghost commented Jul 28, 2023

Sorry to necro this issue, but I had the same issue as the OP and I think I solved it using #[cfg(not(test))]:

#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

The function is then greyed-out within VSCode.

i did as you mentioned

#![no_std]
#![no_main]
#![allow(unused_imports)]

use core::panic::PanicInfo;

#[no_mangle]
pub extern "C" fn _start() -> ! {
    loop {}
}

#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

but i don't think its the best way to handle the warning

Screenshot_20230728_140418

@KjetilIN
Copy link

KjetilIN commented Apr 6, 2024

You can change the checkOnSave. settings, for example for rustc development:

{
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./x.py",
        "check",
        "--json-output"
    ]
}

In your case, maybe something like this will works:

{
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./cargo",
        "xcheck",
        "--json-output"
    ]
}

(Not sure xcheck support --json-output)

Worked for me! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants