From 889015f03bb2e1ee483f3bec385775875e56d7bd Mon Sep 17 00:00:00 2001 From: James McCoy Date: Fri, 13 Jun 2025 11:11:22 -0700 Subject: [PATCH] build(rust): use $CARGO_PKG_RUST_VERSION when generating bindings (#4512) Since cargo 1.63, $CARGO_PKG_RUST_VERSION is set in the build environment to the value of the rust-version Cargo.toml field. This removes the need to manually invoke cargo from build.rs during a build of the tree-sitter crate with the bindgen feature enabled. Removing the cargo invocation also ensures the build doesn't write to the current directory when the target directory has been redirected elsewhere. "cargo metadata" will attempt to update Cargo.lock, which will fail if the source tree is read-only. --- lib/binding_rust/build.rs | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/lib/binding_rust/build.rs b/lib/binding_rust/build.rs index 5e918034..d17dea65 100644 --- a/lib/binding_rust/build.rs +++ b/lib/binding_rust/build.rs @@ -56,29 +56,6 @@ fn generate_bindings(out_dir: &std::path::Path) { use bindgen::RustTarget; - let output = Command::new("cargo") - .args(["metadata", "--format-version", "1"]) - .output() - .unwrap(); - - let metadata = serde_json::from_slice::(&output.stdout).unwrap(); - - let Some(rust_version) = metadata - .get("packages") - .and_then(|packages| packages.as_array()) - .and_then(|packages| { - packages.iter().find_map(|package| { - if package["name"] == "tree-sitter" { - package.get("rust_version").and_then(|v| v.as_str()) - } else { - None - } - }) - }) - else { - panic!("Failed to find tree-sitter package in cargo metadata"); - }; - const HEADER_PATH: &str = "include/tree_sitter/api.h"; println!("cargo:rerun-if-changed={HEADER_PATH}"); @@ -97,6 +74,8 @@ fn generate_bindings(out_dir: &std::path::Path) { "TSQueryPredicateStep", ]; + let rust_version = env!("CARGO_PKG_RUST_VERSION"); + let bindings = bindgen::Builder::default() .header(HEADER_PATH) .layout_tests(false)