1- use std:: fs ;
1+ use std:: { env , process :: Command , str } ;
22
33extern crate napi_build;
44
5- fn main ( ) {
5+ fn main ( ) -> anyhow:: Result < ( ) > {
6+ println ! ( "cargo:rerun-if-env-changed=CI" ) ;
7+ let is_ci = env:: var ( "CI" ) . is_ok_and ( |value| !value. is_empty ( ) ) ;
8+
69 // Generates, stores build-time information as static values.
710 // There are some places relying on correct values for this (i.e telemetry),
811 // So failing build if this fails.
9- shadow_rs:: ShadowBuilder :: builder ( )
10- . build ( )
11- . expect ( "Should able to generate build time information" ) ;
12+ let cargo = vergen_git2:: CargoBuilder :: default ( )
13+ . target_triple ( true )
14+ . build ( ) ?;
15+ // We use the git dirty state to disable persistent caching (persistent caching relies on a
16+ // commit hash to be safe). One tradeoff of this is that we must invalidate the rust build more
17+ // often.
18+ //
19+ // This invalidates the build if any untracked files change. That's sufficient for the case
20+ // where we transition from dirty to clean.
21+ //
22+ // There's an edge-case here where the repository could be newly dirty, but we can't know
23+ // because our build hasn't been invalidated, since the untracked files weren't untracked last
24+ // time we ran. That will cause us to incorrectly report ourselves as clean.
25+ //
26+ // However, in practice that shouldn't be much of an issue: If no other dependency of this
27+ // top-level crate has changed (which would've triggered our rebuild), then the resulting binary
28+ // must be equivalent to a clean build anyways. Therefore, persistent caching using the HEAD
29+ // commit hash as a version is okay.
30+ let git = vergen_git2:: Git2Builder :: default ( )
31+ . dirty ( /* include_untracked */ true )
32+ . describe (
33+ /* tags */ true ,
34+ /* dirty */ !is_ci, // suppress the dirty suffix in CI
35+ /* matches */ Some ( "v[0-9]*" ) , // find the last version tag
36+ )
37+ . build ( ) ?;
38+ vergen_git2:: Emitter :: default ( )
39+ . add_instructions ( & cargo) ?
40+ . add_instructions ( & git) ?
41+ . fail_on_error ( )
42+ . emit ( ) ?;
1243
13- let git_head = fs:: read_to_string ( "../../.git/HEAD" ) . unwrap_or_default ( ) ;
14- if !git_head. is_empty ( ) && !git_head. starts_with ( "ref: " ) {
15- println ! ( "cargo:warning=git version {}" , git_head) ;
44+ match Command :: new ( "git" ) . args ( [ "rev-parse" , "HEAD" ] ) . output ( ) {
45+ Ok ( out) if out. status . success ( ) => println ! (
46+ "cargo:warning=git HEAD: {}" ,
47+ str :: from_utf8( & out. stdout) . unwrap( )
48+ ) ,
49+ _ => println ! ( "cargo:warning=`git rev-parse HEAD` failed" ) ,
1650 }
1751
1852 #[ cfg( not( all( target_os = "macos" , target_arch = "aarch64" ) ) ) ]
@@ -36,4 +70,6 @@ fn main() {
3670
3771 #[ cfg( not( target_arch = "wasm32" ) ) ]
3872 turbo_tasks_build:: generate_register ( ) ;
73+
74+ Ok ( ( ) )
3975}
0 commit comments