Skip to content

Commit

Permalink
factors: Fix derive macro in rust-analyzer
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lann committed Jul 10, 2024
1 parent 5d9c9f8 commit f7c8914
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
30 changes: 17 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/factor-variables/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
println!("cargo::rerun-if-changed=build.rs");
// Enable spin-factors-derive to emit expanded macro output.
let out_dir = std::env::var("OUT_DIR").unwrap();
println!("cargo:rustc-env=SPIN_FACTORS_DERIVE_EXPAND_DIR={out_dir}");
}
File renamed without changes.
2 changes: 1 addition & 1 deletion crates/factors-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ proc-macro = true
expander = ["dep:expander"]

[dependencies]
expander = { version = "2.1.0", optional = true }
expander = { version = "2.2.1", optional = true }
proc-macro2 = "1.0.79"
quote = "1.0.35"
syn = "2.0.52"
Expand Down
10 changes: 7 additions & 3 deletions crates/factors-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ pub fn derive_factors(input: proc_macro::TokenStream) -> proc_macro::TokenStream
let expanded = expand_factors(&input).unwrap_or_else(|err| err.into_compile_error());

#[cfg(feature = "expander")]
let expanded = expander::Expander::new("factors")
.write_to_out_dir(expanded)
.unwrap();
let expanded = if let Some(dest_dir) = std::env::var_os("SPIN_FACTORS_DERIVE_EXPAND_DIR") {
expander::Expander::new("factors")
.write_to(expanded, std::path::Path::new(&dest_dir))
.unwrap()
} else {
expanded
};

expanded.into()
}
Expand Down
1 change: 1 addition & 0 deletions crates/factors-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = { workspace = true }
[dependencies]
spin-app = { path = "../app" }
spin-factors = { path = "../factors" }
spin-factors-derive = { path = "../factors-derive", features = ["expander"] }
spin-loader = { path = "../loader" }
tempfile = "3.10.1"
toml = "0.8.14"
Expand Down

0 comments on commit f7c8914

Please sign in to comment.