rust-lang / rust-lang/rust-analyzer

"failed to load file" when including build script output from cargo workspace root

Open
#20,129 5 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-vfs C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

I am trying to use prost_build to generate rust structs from a .proto file. I've got it working to the point that cargo build succeeds, but rust-analyzer is giving me a "failed to load file .../out/cobalt.event_type.rs" error. I've checked the path in the error and the file does exist and is valid rust code.

In my case, the crate I'm working in is part of a cargo workspace, so the target directory is a sibling of my crate root. It appears from followup comments on #17828 that this might be a bug in rust-analyzer, where it fails to load included source files which are outside of the crate root. That issue was originally regarding non-rust included files, but in this case the included file ends with .rs, so a new issue seemed appropriate.

rust-analyzer version: 1.85.1

rustc version: 1.85.1

editor or extension: emacs-lsp

relevant settings:

code snippet to reproduce:

build.rs:

    println!("cargo:rerun-if-changed=../../libraries/cobalt_protobuf/src");
    prost_build::compile_protos(
        &["../../libraries/cobalt_protobuf/src/event_type.proto"],
        &["../../libraries/cobalt_protobuf/src"],
    )?;

production code:

pub mod cobalt {
    pub mod event_type {
        include!(concat!(env!("OUT_DIR"), "/cobalt.event_type.rs"));
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the build.rs and production include! snippet, then reproduce the rust-analyzer error in the described Cargo workspace layout. Trace how rust-analyzer handles the generated cobalt.event_type.rs file under OUT_DIR when it is outside the crate root. Done means the existing generated Rust file loads without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.