Seamless integration of generated Rust source code

Hello,

I have a code generator written in Java (Xtext) that outputs Rust code. Now, I would like to debug the generated code (e.g. check for type errors), I would like to use rust-analyzer for this. There is a symlink in my Rust project to the path of the generated source file. When I open the symlink with Emacs/VSCode (with a respective LSP plugin + rust-analyzer), rust-analyzer complains about the generated source file not being in a module and ignores the contents of the file.

I was wondering if there is some way to get rust-analyzer to deal with symlinks differently or perhaps some entirely different approach to debugging automatically generated code (possibly without rust-analyzer)?

Thanks in advance.

Where is the file located? Where is your rust project located? And how are you using the rust file? (using mod generated_file;, #[path = "path/to/generated_file.rs"] mod generated_file; or include!("path/to/generated_file.rs")?)

The file is in the directory where the source file is located (= the file that was used to generate the Rust code). It's a complete module with imports from the crate I want to integrate into (it's pretty much like a plugin). include! won't work because it's not inline code but a whole module... and I think #[path] only works with files that are within the crate but are not automatically included over the crate folder structure.

...which can be then used with mod foo { include!("path/to/foo.rs") }, I guess?

1 Like

This works, but rust-analyzer can't deal with files outside of the current crate manually included with the macro: `include!` macro silently gives empty string if destination file is outside of the crate · Issue #17040 · rust-lang/rust-analyzer · GitHub

I guess I need to look for another Rust language server.

I don't think there are any. Rust Rover might work.

But can the build script just copy the file into the project src directory?

1 Like

Files are never automatically included in a crate. You always have to either use include!() or mod foo; and in the later case you can optionally use #[path] to control which file to include for the module.

Indeed; RustRover seems to load the module over include! just fine.

1 Like