Producing HIR across crate dependencies

I'm not sure this question is the best fit for this forum, since it is a support question, but I haven't had any luck in spaces for general Rust users. This question is about using rustc as a library via rustc_interface.

Essentially, I'm trying to use rustc_interface::run_compiler() to produce HIR, where the input is a simple rust module that contains use statements importing other crates. I can produce HIR on a simple file with no use statements just fine, but when I encounter a use, I get a compiler error saying the crate is not found and I must use extern crate. When using extern crate, I then get a metadata_cannot_find_crate error (actually, an error that that fluent string can't be found).

I'm wondering how I can configure the compiler to find all of the necessary crates. Do I need to list Externs? Or is this a job for SearchPath? I followed the example in rustc-dev-guide to get me this far, but can't figure out how to extend it to a multi-crate situation given my near-zero knowledge of rustc's architecture. I opened an issue linking to a minimal reproduction, for more context: Example of using rustc_interface to generate the HIR for a file referencing dependent crates? · Issue #1720 · rust-lang/rustc-dev-guide · GitHub

Do I need to list Externs?

I believe this is what you need to do. Have you tried it and it has not worked?

There's nothing too terribly special about the driver interface. It needs to be configured just like you would on the command-line.

I get a compiler error saying the crate is not found and I must use extern crate

To avoid this, you probably need to set the edition.

1 Like

Thank you, that helps a lot. I've only ever used cargo, so I don't know how to use rustc to reproduce the same effect as, say, cargo build, but your comment lets me know what to look for to learn how to do that.

cargo build -vv will tell you the rustc commands that cargo uses.

1 Like

Thanks for the guidance. I solved the issue after a few more stumbles: Minimal rustc flags needed to compile a single file which depends on other crates, using compilations from a previous `cargo build` run? - #2 by maackle - help - The Rust Programming Language Forum

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.