on master help sign in
emit cargo metadata duiring build scripts to avoid outdated buildscript outputs (#6743)
896404cPascal Kuthe
helix-loader/build.rs+38−1
@@ -1,4 +1,5 @@
1use std::borrow::Cow;
1use std::borrow::Cow;
2+use std::path::Path;
2use std::process::Command;
3use std::process::Command;
3
4
4const VERSION: &str = include_str!("../VERSION");
5const VERSION: &str = include_str!("../VERSION");
@@ -11,7 +12,7 @@ fn main() {
11 .filter(|output| output.status.success())
12 .filter(|output| output.status.success())
12 .and_then(|x| String::from_utf8(x.stdout).ok());
13 .and_then(|x| String::from_utf8(x.stdout).ok());
13
14
14− let version: Cow<_> = match git_hash {
15+ let version: Cow<_> = match &git_hash {
15 Some(git_hash) => format!("{} ({})", VERSION, &git_hash[..8]).into(),
16 Some(git_hash) => format!("{} ({})", VERSION, &git_hash[..8]).into(),
16 None => VERSION.into(),
17 None => VERSION.into(),
17 };
18 };
@@ -23,4 +24,40 @@ fn main() {
23
24
24 println!("cargo:rerun-if-changed=../VERSION");
25 println!("cargo:rerun-if-changed=../VERSION");
25 println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version);
26 println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version);
27+
28+ if git_hash.is_none() {
29+ return;
30+ }
31+
32+ // we need to revparse because the git dir could be anywhere if you are
33+ // using detached worktrees but there is no good way to obtain an OsString
34+ // from command output so for now we can't accept non-utf8 paths here
35+ // probably rare enouch where it doesn't matter tough we could use gitoxide
36+ // here but that would be make it a hard dependency and slow compile times
37+ let Some(git_dir): Option<String> = Command::new("git")
38+ .args(["rev-parse", "--git-dir"])
39+ .output()
40+ .ok()
41+ .filter(|output| output.status.success())
42+ .and_then(|x| String::from_utf8(x.stdout).ok())
43+ else{ return; };
44+ // If heads starts pointing at something else (different branch)
45+ // we need to return
46+ let head = Path::new(&git_dir).join("HEAD");
47+ if head.exists() {
48+ println!("cargo:rerun-if-changed={}", head.display());
49+ }
50+ // if the thing head points to (branch) itself changes
51+ // we need to return
52+ let Some(head_ref): Option<String> = Command::new("git")
53+ .args(["symbolic-ref", "HEAD"])
54+ .output()
55+ .ok()
56+ .filter(|output| output.status.success())
57+ .and_then(|x| String::from_utf8(x.stdout).ok())
58+ else{ return; };
59+ let head_ref = Path::new(&git_dir).join(head_ref);
60+ if head_ref.exists() {
61+ println!("cargo:rerun-if-changed={}", head_ref.display());
62+ }
26}
63}
helix-loader/src/grammar.rs+12
@@ -413,6 +413,18 @@ fn build_tree_sitter_library(
413 let mut library_path = parser_lib_path.join(&grammar.grammar_id);
413 let mut library_path = parser_lib_path.join(&grammar.grammar_id);
414 library_path.set_extension(DYLIB_EXTENSION);
414 library_path.set_extension(DYLIB_EXTENSION);
415
415
416+ // if we are running inside a buildscript emit cargo metadata
417+ // to detect if we are running from a buildscript check some env variables
418+ // that cargo only sets for build scripts
419+ if std::env::var("OUT_DIR").is_ok() && std::env::var("CARGO").is_ok() {
420+ if let Some(scanner_path) = scanner_path.as_ref().and_then(|path| path.to_str()) {
421+ println!("cargo:rerun-if-changed={scanner_path}");
422+ }
423+ if let Some(parser_path) = parser_path.to_str() {
424+ println!("cargo:rerun-if-changed={parser_path}");
425+ }
426+ }
427+
416 let recompile = needs_recompile(&library_path, &parser_path, &scanner_path)
428 let recompile = needs_recompile(&library_path, &parser_path, &scanner_path)
417 .context("Failed to compare source and binary timestamps")?;
429 .context("Failed to compare source and binary timestamps")?;
418
430