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

Clarifying behavior of rust-project.json source.include_dirs #16792

Open
darichey opened this issue Mar 8, 2024 · 2 comments
Open

Clarifying behavior of rust-project.json source.include_dirs #16792

darichey opened this issue Mar 8, 2024 · 2 comments
Labels
C-support Category: support questions

Comments

@darichey
Copy link
Contributor

darichey commented Mar 8, 2024

I have a project structured like...

.
├── generated
│   └── foo.rs
├── rust-project.json
└── src
    └── lib.rs

I want to refer to the module foo from src/lib.rs. I thought I could do that with a rust-project.json that looks like...

{
  "crates": [
    {
      "display_name": "rust_test",
      "root_module": "./src/lib.rs",
      "edition": "2021",
      "is_workspace_member": true,
      "source": {
        "exclude_dirs": [],
        "include_dirs": ["./src", "./generated"]
      },
      "deps": []
    }
  ]
}

But mod foo; in src/lib.rs results in unresolved module, can't find module file: foo.rs, or foo/mod.rs.

I think this happens because the attempt to resolve the path to the module is relative to the original file:

let orig_file_id = file_id.original_file_respecting_includes(db.upcast());
for candidate in candidate_files.iter() {
let path = AnchoredPath { anchor: orig_file_id, path: candidate.as_str() };
if let Some(file_id) = db.resolve_path(path) {

so only paths in src are considered:
pub fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
let mut base = self.paths[&path.anchor].clone();
base.pop();
let path = base.join(path.path)?;
self.files.get(&path).copied()
}

Should this work, or am I misunderstanding include_dirs?

@darichey darichey added the C-support Category: support questions label Mar 8, 2024
@hauserx
Copy link

hauserx commented Jan 27, 2025

Resolving files across all include dirs would also help in case of bazel rules_rust, where a single crate may have mix of generated files and sources: bazelbuild/rules_rust#3126

Is extension of rust analyzer something that could be accepted?

@davidbarsky
Copy link
Contributor

We should support that, but that’ll probably come with #18948. Our VFS/source root situation is suboptimal and I think we’d like to redesign this subsystem before doing one-off fixes. In recent weeks, seemingly small changes caused substantial breakage for users.

For what it’s worth, I imagine that resolving files across all include_dirs would be part of the initial redesign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-support Category: support questions
Projects
None yet
Development

No branches or pull requests

3 participants