Search code examples
rustrust-cargorust-analyzer

Rust-Analyzer crates not being recognized


I am working with a CLI application suite built in Rust. I currently configured Rust-Analyzer to help manage the project but I am getting errors across my suit on the crate imports of my sub applications.The rand, rusqlite, and regex crates are not being found by the analyzer depsite being declared at the workspace root in the main Cargo.toml and in the Sub applications local Cargo.toml

Currently my app structure looks like:

rust-cli-buddy/
  ├── Cargo.toml      (Main Cargo.toml for the entire application suite)
  │── buddy_manager/  (Main application manager)
  │      └── src/
  │           └── main.rs
  │
  │── app1/         (Subfolder for application 1)
  │      └── src/...
  │
  │── app2/         (Subfolder for application 2)
  │
  │       └── src/...
  │
  │── ...           (Other subfolders for additional applications)
  └── ...

The manager is run by default using the root Cargo.toml:

  [package]
name = "rust-cli-buddy"
version = "0.1.0"
edition = "2021"
authors = ["Jesse jesse.greenough84@gmail.com"]
license = "MIT"
default-run = "buddy-manager"

[workspace]
members = [
    "buddy-manager",
    "calculator",
    "password-manager",
    "password-generator",
    "file-organizer",
    "task-manager",
    "development-timer",
    "word-analyzer",
]

[dependencies]
buddy-utils = { path = "./buddy-utils" }
... other crates used in the app


[[bin]]
name = "buddy-manager"
path = "buddy-manager/src/main.rs"

... other sub app libraries and binaries

Each sub app has their own Cargo.toml declaring the package of the format:

[package]
name = "<app_name>"
version = "0.1.0"
edition = "2021"

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

[dependencies]
buddy-utils = { path = "../buddy-utils" }
... crates used in current sub-app

[[bin]]
name = "<app_name>"
path = "src/main.rs"

I am new to Rust package management and wonder if I have anything wrong as of now? Everything is able to run but is there better practices for this?

I have looked at all the different ways to reference the dependency, looked down the entirety of Rust-Analyzers settings, and have tried to personally debug with the analyzers output.

I can't seem to find why the Analyzer is not finding the crates despite them being declared and working as expecting in the actual building and running of the application?


Solution

  • Since you already have a main package (buddy-manager) and it's in a subdirectory already to fix your problems you could remove the [package] table from rust-cli-buddy/Cargo.toml completely. If the dependencies are used across the sub packages you can rename [dependencies] to [workspace.dependencies] to ease version management, otherwise remove it, a [dependencies] table is not allowed in vitual manifests.

    Then to get back the eas of calling cargo run you should add default-members = ["buddy-manager"] to the [workspace] table.

    In the sub packages you have to declare all crates they depend on. If you have [workspace.dependencies] in rust-cli-buddy/Cargo.toml then you may want to add them like external = { workspace = true } instead of specifying the version and features in every package.

    So for example: rust-cli-buddy/Cargo.toml

    [workspace]
    members = [
        "buddy-manager",
        "calculator",
        "password-manager",
        "password-generator",
        "file-organizer",
        "task-manager",
        "development-timer",
        "word-analyzer",
    ]
    default-members = [
        "buddy-manager"
    ]
    
    [workspace.dependencies]
    rand = "0.8.5"
    

    and one example for an package:

    [package]
    name = "buddy-manager"
    version = "0.1.0"
    
    [dependencies]
    rand = { workspace = true }
    buddy-utils = { path = "../buddy-utils" }
    # and more
    

    The bin section you've included is the one provided by cargo so you don't actually have to spell it out in the manifest.

    You can refer to chapter 14.3 of the book for further info on how virtual manifests for workspaces work.