0

I have a package that contains both a binary crate and a library crate. The structure of the package looks like this

.
├── Cargo.lock
├── Cargo.toml
├── src
│   ├── bin
│   │   └── bin.rs
│   ├── lib.rs
│   ├── foo
│   │   ├── concrete.rs
│   │   └── traits.rs
│   └── foo.rs

Cargo.toml:

[package]
name = "mycrate"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

src/lib.rs:

pub mod foo;

src/foo.rs:

pub mod concrete;
pub mod traits;

src/foo/traits.rs:

pub trait A {
    ...
}

src/foo/concrete.rs

use crate::foo::traits::A;
pub struct B {
    ...
}
impl B {
    ...
}
impl A for B {
    ...
}

And finally src/bin/bin.rs:

use mycrate::foo::concrete::B;
use mycrate::foo::traits::A;

fn main() {
    let b: B = ...
    // use methods defined in trait A
}

The problem is in the last file both my use statements are having problems according to rust-analyzer

use mycrate::foo::traits::A;

For the above I am getting an unused import warning (but if I don't include it the code won't compile because I use methods defined in A so it needs to be in scope)

use mycrate::foo::concrete::B;

And for above I am getting an unresolved import mycrate::foo::concrete, could not find concrete in foo error.

Strangely if I run cargo run or cargo build it works without any warnings or errors.

rustup --version (https://www.rust-lang.org/tools/install):

rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.71.1 (eb26296b5 2023-08-03)`

rust-analyzer --version (https://formulae.brew.sh/formula/rust-analyzer):

rust-analyzer 0.0.0 (99718d0c8 2023-07-22)

hx --version (my text editor installed from https://formulae.brew.sh/formula/helix)

helix 23.05 (7f5940be)

hx --health | grep rust

rust                      ✓ rust-analyzer           ✘ lldb-vscode             ✓                         ✓                         ✓

I am on a MacBook Pro with Apple M1 Max chip running macOS 13.0.1 (probably not relevant, but including just in case)

HashBr0wn
  • 387
  • 1
  • 11
  • 1
    What editor are you using? – drewtato Aug 16 '23 at 04:18
  • Does the error come from rustc or rust-analyzer? (It should appear next to the error). – Chayim Friedman Aug 16 '23 at 11:52
  • I am using helix. It does not say where the error is coming from, there is a code next to it though in parens. The unresolved import is (E0432) and for ununsed import it says (unused imports). I assume it is rust-analyzer because the code compiles without any warning or error. Also if I uninstall rust-analyzer it goes away. I am still able to use go to definition on the unresolved import while rust-analyzer is installed which is also weird, because if it was unresolved why is it able to jump to the code for me – HashBr0wn Aug 16 '23 at 12:39
  • Editted post to include editor information – HashBr0wn Aug 16 '23 at 12:43

0 Answers0