1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
From: James McCoy <jamessan@jamessan.com>
Date: Fri, 13 Jun 2025 11:11:22 -0700
Subject: 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.
(cherry picked from commit 889015f03bb2e1ee483f3bec385775875e56d7bd)
---
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 66c78be..25bdb40 100644
--- a/lib/binding_rust/build.rs
+++ b/lib/binding_rust/build.rs
@@ -57,29 +57,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::<serde_json::Value>(&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}");
@@ -98,6 +75,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)
|