Skip to content

Commit

Permalink
fix include src paths issues
Browse files Browse the repository at this point in the history
Summary:
the command `cd fbcode/common/rust/cargo_from_buck/fbcode/security/flare/if/flare_if/types&&cargo build` results in
```
  thread 'main' panicked at thrift_build.rs:71:20:
  Failed while running thrift compilation: No such file or directory (os error 2
```
similarly the command `cd fbcode/common/rust/cargo_from_buck/fbcode/security/flare/if/flare_if/&&CARGO_TARGET_DIR=/tmp cargo build`

this diff fixes both of these cases by avoiding writing files outside of the cargo provided `OUT_DIR`.

Reviewed By: dtolnay

Differential Revision: D53422085

fbshipit-source-id: 989850a5101c657cf20582d843b9ce5c9bda81b4
  • Loading branch information
Shayne Fletcher authored and facebook-github-bot committed Feb 5, 2024
1 parent b0651e7 commit 86a290c
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions shed/thrift_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ impl Config {
self
}

/// Transform a relative path so leading "../"'s are replaced with "_t".
pub fn remap_to_out_dir(&self, path: &str) -> PathBuf {
let mut rem = path;
let mut parts = vec![];
while let Some(p) = rem.strip_prefix("../") {
rem = p;
parts.push("_t");
}
Path::new(&parts.join("/")).join(rem)
}

/// Map `remap_to_out_dir` over the given paths and join the result with
/// ":".
pub fn include_srcs_arg(&self, ps: &[String]) -> String {
ps.iter()
.map(|p| self.remap_to_out_dir(p))
.collect::<Vec<PathBuf>>()
.iter()
.map(|p| p.to_str().unwrap())
.collect::<Vec<&str>>()
.join(":")
}

/// Run the compiler on the input files. As a result a `lib.rs` file will be
/// generated inside the output dir. The contents of the `lib.rs` can vary
/// according to the generation context (e.g. for a given thrift library,
Expand All @@ -198,13 +221,17 @@ impl Config {
for lib_include_src in &self.lib_include_srcs {
println!("cargo:rerun-if-changed={lib_include_src}");
if let GenContext::Lib = self.gen_context {
fs::copy(lib_include_src, out.join(lib_include_src))?;
let out_path = self.remap_to_out_dir(lib_include_src);
fs::create_dir_all(out.join(out_path.parent().unwrap()))?;
fs::copy(lib_include_src, out.join(out_path))?;
}
}
for types_include_src in &self.types_include_srcs {
println!("cargo:rerun-if-changed={types_include_src}");
if let GenContext::Types = self.gen_context {
fs::copy(types_include_src, out.join(types_include_src))?;
let out_path = self.remap_to_out_dir(types_include_src);
fs::create_dir_all(out.join(out_path.parent().unwrap()))?;
fs::copy(types_include_src, out.join(out_path))?;
}
}

Expand Down Expand Up @@ -440,13 +467,13 @@ impl Config {
if !self.lib_include_srcs.is_empty() {
args.push(format!(
"lib_include_srcs={}",
self.lib_include_srcs.join(":")
self.include_srcs_arg(&self.lib_include_srcs)
));
}
if !self.types_include_srcs.is_empty() {
args.push(format!(
"types_include_srcs={}",
self.types_include_srcs.join(":")
self.include_srcs_arg(&self.types_include_srcs)
));
}
if let Some(options) = &self.options {
Expand Down

0 comments on commit 86a290c

Please sign in to comment.