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

include! macro silently gives empty string if destination file is outside of the crate #17040

Open
saschanaz opened this issue Apr 10, 2024 · 7 comments
Labels
C-bug Category: bug

Comments

@saschanaz
Copy link

saschanaz commented Apr 10, 2024

rust-analyzer version: 0.3.1916-standalone (7a8374c 2024-04-08)

rustc version: rustc 1.77.2 (25ef9e3d8 2024-04-09)

editor or extension: VSCode, rust-analyzer v0.3.1916

relevant settings: N/A?

repository link (if public, optional): https://github.com/saschanaz/rust-include-repro

code snippet to reproduce: (Please refer to the repo to see the exact file tree)

include!("in-src.rs");
include!("../in-crate.rs");
include!("../../in-workspace.rs");

fn main() {
    in_src();
    in_crate();
    in_workspace();
}

The issue is threefold:

  1. The function name of in_workspace does not autocomplete in main()
  2. Hovering over in_workspace(); shows nothing
  3. Clicking "Inline macro" feature from light bulb menu for include!("../../in-workspace.rs") just deletes the line

The build result works as expected, so this should be rust-analyzer specific.

See also: https://bugzilla.mozilla.org/show_bug.cgi?id=1890431

@saschanaz saschanaz added the C-bug Category: bug label Apr 10, 2024
@saschanaz
Copy link
Author

Got some hint from #3767 and added some println, and it seems relative_file() is failing to load the file:

let res = db
.resolve_path(path)
.ok_or_else(|| ExpandError::other(format!("failed to load file `{path_str}`")))?;

@saschanaz
Copy link
Author

#15836 (comment) seems to apply here?

@saschanaz
Copy link
Author

So basically only the files that included in load_crate_graph() is supported for include!().

Maybe a fix can parse and load include!() string literals early enough? Thoughts? @Veykril

@saschanaz
Copy link
Author

If parsing files requires a lot of changes, maybe at least allow adding some files in the list by environment variables?

@davidbarsky
Copy link
Contributor

So basically only the files that included in load_crate_graph() is supported for include!().

Yes, that's correct. I think that making source root resolution look outside the crate might be feasible, but I think you might be better off not include!ing those source files.

@saschanaz
Copy link
Author

saschanaz commented May 22, 2024

Yes, that's correct. I think that making source root resolution look outside the crate might be feasible, but I think you might be better off not include!ing those source files.

Sure if the whole code is Rust, but if one needs to bind between other languages and Rust and the bindgen is not in Rust side (e.g. some Python code as in our case), the generated file ends up being outside of the crate graph.

How are build.rs-generated files being include!()-ed by rust-analyzer right now? Can a similar way help this case too?

@Veykril
Copy link
Member

Veykril commented May 23, 2024

The corresponding out-dir is added correctly to the set of known files for a crate, so if your build script emits a file that the crate of that build script is then including that should work fine, it won't work cross crates though. This not working here is a known problem with our current VFS setup.

lann added a commit to lann/spin that referenced this issue Jul 10, 2024
Rust analyzer doesn't like `include!`s that reach outside of a crate's
own OUT_DIR: rust-lang/rust-analyzer#17040

Fix this by requiring any crate that wants expanded output (in tests)
to have a build script that sets a magic env var.

Signed-off-by: Lann Martin <lann.martin@fermyon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants