Unable to add UTs to Rust OS -> duplicate lang item

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
carbonBased
Member
Member
Posts: 389
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Unable to add UTs to Rust OS -> duplicate lang item

Post by carbonBased »

For those writing OSs in Rust, how are you handling adding/running tests?

I have a simple kernel: https://github.com/dark-canvas/synapse
Which I can boot via my UEFI bootloader: https://github.com/dark-canvas/satus

But when I try to add UTs, they fail to build:

Code: Select all

jweeks@pop-os:~/code/synapse$ cargo test --target x86_64-unknown-linux-gnu
   Compiling volatile v0.4.6
   Compiling bit_field v0.10.3
   Compiling bitflags v2.11.0
   Compiling satus-struct v0.1.0 (https://github.com/dark-canvas/satus-struct#09dad6de)
error[E0152]: duplicate lang item in crate `core`: `sized`
  |
  = note: the lang item is first defined in crate `core` (which `satus_struct` depends on)
  = note: first definition in `core` loaded from /home/jweeks/code/synapse/target/x86_64-unknown-linux-gnu/debug/deps/libcore-b6f5b675cce2ded8.rmeta
  = note: second definition in `core` loaded from /home/jweeks/code/synapse/target/x86_64-unknown-linux-gnu/debug/deps/libcore-9f84d55d1ea268b0.rmeta
etc...
I assume this is because of this:
https://github.com/dark-canvas/synapse/ ... ig.toml#L4

And, indeed, if I remove that, I can build, and run... but they crash:

Code: Select all

    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running unittests src/main.rs (target/x86_64-unknown-linux-gnu/debug/deps/kernel-584809ad931f72bd)
error: test failed, to rerun pass `--bin kernel`

Caused by:
  process didn't exit successfully: `/home/jweeks/code/synapse/target/x86_64-unknown-linux-gnu/debug/deps/kernel-584809ad931f72bd` (signal: 11, SIGSEGV: invalid memory reference)
Presumably this is due to a mismatch of libraries and target architectures...

I'm curious if anyone has this working, or if there's a list of best practices for building a kernel (with a custom target) but running the tests on the local platform.

Thanks,
Jeff
User avatar
carbonBased
Member
Member
Posts: 389
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: Unable to add UTs to Rust OS -> duplicate lang item

Post by carbonBased »

User avatar
savoita
Posts: 11
Joined: Fri Aug 29, 2025 11:12 am

Re: Unable to add UTs to Rust OS -> duplicate lang item

Post by savoita »

FWIW, I also had to address this in my OS, and it was made much easier by utilizing the xtask pattern to compile and run everything, vs. the traditional makefile approach (which I started with). It effectively allows you to use Rust to write the build process, rather than trying to manipulate the shell and various quirks of Make.

If you'd like to take a look, you can view the project here: https://codeberg.org/linuiz-project/linuiz

Specifically, (in .cargo/config.toml) you'll see a TOML item that reads:

Code: Select all

[alias]
xtask = "run --release --manifest-path xtask/Cargo.toml --"
... which then invokes the Rust binary crate within xtask/. A couple crates like `xshell` and `clap` really help with composability, and make it easy to modify the CLI functions of the builder/runner.

Additionally, for tests, it is much easier to use the following global attributes in your `main.rs`:

Code: Select all

#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
Specifically, the `no_std` attribute allows you to use macros like `println!` in your tests, which I've found incredibly useful. For testing components that rely on architecture-specific data (like reading a value from a control register), you may be able to use the `cfg_select! {}` macro to provide a dummy value when tests are enabled.

Hope this helps.
Be kind.
Post Reply