Skip to content

Commit ff6d9b7

Browse files
committed
Fix build.rs to read files during cargo publish
1 parent a23238a commit ff6d9b7

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

rust/ruby-rbs-sys/build.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,36 @@ fn build(include_dir: &Path, src_dir: &Path) -> Result<(), Box<dyn Error>> {
3333
}
3434

3535
fn root_dir() -> Result<PathBuf, Box<dyn Error>> {
36-
Ok(Path::new(env!("CARGO_MANIFEST_DIR"))
37-
.ancestors()
38-
.nth(2)
39-
.ok_or("Failed to find project root directory")?
40-
.to_path_buf())
36+
// Allow overriding via environment variable (useful for packaging)
37+
if let Ok(source_dir) = env::var("RBS_SOURCE_DIR") {
38+
let root = PathBuf::from(source_dir);
39+
let include_dir = root.join("include");
40+
let src_dir = root.join("src");
41+
42+
if include_dir.exists() && src_dir.exists() {
43+
return Ok(root);
44+
} else {
45+
return Err(format!(
46+
"RBS_SOURCE_DIR is set to {:?}, but include/ and src/ directories not found",
47+
root
48+
)
49+
.into());
50+
}
51+
}
52+
53+
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
54+
55+
// Try workspace structure (development)
56+
if let Some(workspace_root) = manifest_dir.ancestors().nth(2) {
57+
let include_dir = workspace_root.join("include");
58+
let src_dir = workspace_root.join("src");
59+
60+
if include_dir.exists() && src_dir.exists() {
61+
return Ok(workspace_root.to_path_buf());
62+
}
63+
}
64+
65+
Err("Cannot find include/ and src/ directories. Set RBS_SOURCE_DIR environment variable to the repository root.".into())
4166
}
4267

4368
fn source_files<P: AsRef<Path>>(root_dir: P) -> Result<Vec<String>, Box<dyn Error>> {

rust/ruby-rbs/build.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,19 @@ impl Node {
5151
}
5252

5353
fn main() -> Result<(), Box<dyn Error>> {
54-
let config_path = Path::new(env!("CARGO_MANIFEST_DIR"))
55-
.join("../../config.yml")
56-
.canonicalize()?;
54+
// Allow overriding the source directory via environment variable (useful for packaging)
55+
let config_path = if let Ok(source_dir) = env::var("RBS_SOURCE_DIR") {
56+
Path::new(&source_dir).join("config.yml")
57+
} else {
58+
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../config.yml")
59+
};
60+
61+
let config_path = config_path.canonicalize().map_err(|e| {
62+
format!(
63+
"Failed to find config.yml at {:?}: {}. Set RBS_SOURCE_DIR environment variable to the repository root.",
64+
config_path, e
65+
)
66+
})?;
5767

5868
println!("cargo:rerun-if-changed={}", config_path.display());
5969

0 commit comments

Comments
 (0)