Skip to content

Commit e31f798

Browse files
committed
Revert "Revert "fix(turbopack): Use vergen-git2 instead of shadow-rs for napi and next-api crates to fix stale git lock files" (#76879)"
This reverts commit ee5c7d3.
1 parent 15741c5 commit e31f798

File tree

16 files changed

+270
-280
lines changed

16 files changed

+270
-280
lines changed

Cargo.lock

+158-185
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ unicode-segmentation = "1.10.1"
426426
unsize = "1.1.0"
427427
url = "2.2.2"
428428
urlencoding = "2.1.2"
429+
vergen = { version = "9.0.4", features = ["cargo"] }
430+
vergen-git2 = { version = "1.0.5", features = ["cargo"] }
429431
webbrowser = "0.8.7"
430432

431433
[patch.crates-io]

crates/napi/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ rand = { workspace = true }
6666
rustc-hash = { workspace = true }
6767
serde = "1"
6868
serde_json = "1"
69-
shadow-rs = { workspace = true }
7069
tracing = { workspace = true }
7170
tracing-subscriber = { workspace = true }
7271
tracing-chrome = "0.5.0"
7372
url = { workspace = true }
7473
urlencoding = { workspace = true }
7574
once_cell = { workspace = true }
76-
dashmap = "6.1.0"
75+
dashmap = { workspace = true }
7776

7877
swc_core = { workspace = true, features = [
7978
"base_concurrent",
@@ -139,11 +138,11 @@ turbo-tasks-malloc = { workspace = true, default-features = false }
139138
tokio = { workspace = true, features = ["full"] }
140139

141140
[build-dependencies]
141+
anyhow = { workspace = true }
142142
napi-build = "2"
143143
serde = { workspace = true }
144144
serde_json = { workspace = true }
145-
# It is not a mistake this dependency is specified in dep / build-dep both.
146-
shadow-rs = { workspace = true }
145+
vergen-git2 = { workspace = true }
147146

148147
# build-dependencies for the native, non-wasm32 build
149148
[target.'cfg(not(target_arch = "wasm32"))'.build-dependencies]

crates/napi/build.rs

+44-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1-
use std::fs;
1+
use std::{env, process::Command, str};
22

33
extern 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
}

crates/napi/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ pub mod turbo_trace_server;
6161
pub mod turbopack;
6262
pub mod util;
6363

64-
// Declare build-time information variables generated in build.rs
65-
shadow_rs::shadow!(build);
66-
6764
#[cfg(not(any(feature = "__internal_dhat-heap", feature = "__internal_dhat-ad-hoc")))]
6865
#[global_allocator]
6966
static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;

crates/napi/src/next_api/utils.rs

+6-21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use turbo_tasks::{
1313
TryJoinIterExt, TurboTasks, TurboTasksApi, UpdateInfo, Vc,
1414
};
1515
use turbo_tasks_backend::{
16-
default_backing_storage, noop_backing_storage, DefaultBackingStorage, NoopBackingStorage,
16+
default_backing_storage, noop_backing_storage, DefaultBackingStorage, GitVersionInfo,
17+
NoopBackingStorage,
1718
};
1819
use turbo_tasks_fs::FileContent;
1920
use turbopack_core::{
@@ -130,26 +131,10 @@ pub fn create_turbo_tasks(
130131
dependency_tracking: bool,
131132
) -> Result<NextTurboTasks> {
132133
Ok(if persistent_caching {
133-
let dirty_suffix = if crate::build::GIT_CLEAN
134-
|| option_env!("CI").is_some_and(|value| !value.is_empty())
135-
{
136-
""
137-
} else {
138-
"-dirty"
139-
};
140-
#[allow(
141-
clippy::const_is_empty,
142-
reason = "LAST_TAG might be empty if the tag can't be determined"
143-
)]
144-
let version_info = if crate::build::LAST_TAG.is_empty() {
145-
format!("{}{}", crate::build::SHORT_COMMIT, dirty_suffix)
146-
} else {
147-
format!(
148-
"{}-{}{}",
149-
crate::build::LAST_TAG,
150-
crate::build::SHORT_COMMIT,
151-
dirty_suffix
152-
)
134+
let version_info = GitVersionInfo {
135+
describe: env!("VERGEN_GIT_DESCRIBE"),
136+
dirty: option_env!("CI").is_none_or(|value| value.is_empty())
137+
&& env!("VERGEN_GIT_DIRTY") == "true",
153138
};
154139
NextTurboTasks::PersistentCaching(TurboTasks::new(
155140
turbo_tasks_backend::TurboTasksBackend::new(

crates/napi/src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ pub fn log_internal_error_and_inform(err_info: &str) {
127127
}
128128

129129
#[napi]
130-
pub fn get_target_triple() -> String {
131-
crate::build::BUILD_TARGET.to_string()
130+
pub fn get_target_triple() -> &'static str {
131+
env!("VERGEN_CARGO_TARGET_TRIPLE")
132132
}
133133

134134
pub trait MapErr<T>: Into<Result<T, anyhow::Error>> {

crates/next-api/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ regex = { workspace = true }
2424
rustc-hash = { workspace = true }
2525
serde = { workspace = true }
2626
serde_json = { workspace = true }
27-
shadow-rs = { workspace = true }
2827
swc_core = { workspace = true }
2928
tracing = { workspace = true }
3029
turbo-rcstr = { workspace = true }
@@ -43,6 +42,6 @@ turbopack-node = { workspace = true }
4342
turbopack-nodejs = { workspace = true }
4443

4544
[build-dependencies]
46-
# It is not a mistake this dependency is specified in dep / build-dep both.
47-
shadow-rs = { workspace = true }
45+
anyhow = { workspace = true }
4846
turbo-tasks-build = { workspace = true }
47+
vergen = { workspace = true }

crates/next-api/build.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
use turbo_tasks_build::generate_register;
22

3-
fn main() {
3+
fn main() -> anyhow::Result<()> {
44
// Generates, stores build-time information as static values.
55
// There are some places relying on correct values for this (i.e telemetry),
66
// So failing build if this fails.
7-
shadow_rs::ShadowBuilder::builder()
8-
.build_pattern(shadow_rs::BuildPattern::Lazy)
9-
.build()
10-
.expect("Should able to generate build time information");
7+
let cargo = vergen::CargoBuilder::default()
8+
.target_triple(true)
9+
.build()?;
10+
vergen::Emitter::default()
11+
.add_instructions(&cargo)?
12+
.fail_on_error()
13+
.emit()?;
1114

1215
generate_register();
16+
17+
Ok(())
1318
}

crates/next-api/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ mod server_actions;
2323
mod versioned_content_map;
2424
mod webpack_stats;
2525

26-
// Declare build-time information variables generated in build.rs
27-
shadow_rs::shadow!(build);
28-
2926
pub fn register() {
3027
next_core::register();
3128
turbopack_nodejs::register();

crates/next-api/src/project.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ use turbopack_nodejs::NodeJsChunkingContext;
6868

6969
use crate::{
7070
app::{AppProject, OptionAppProject, ECMASCRIPT_CLIENT_TRANSITION_NAME},
71-
build,
7271
empty::EmptyEndpoint,
7372
entrypoints::Entrypoints,
7473
instrumentation::InstrumentationEndpoint,
@@ -1069,7 +1068,7 @@ impl Project {
10691068
// First, emit an event for the binary target triple.
10701069
// This is different to webpack-config; when this is being called,
10711070
// it is always using SWC so we don't check swc here.
1072-
emit_event(build::BUILD_TARGET, true);
1071+
emit_event(env!("VERGEN_CARGO_TARGET_TRIPLE"), true);
10731072

10741073
// Go over jsconfig and report enabled features.
10751074
let compiler_options = self.js_config().compiler_options().await?;

crates/next-build/Cargo.toml

-20
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,5 @@ workspace = true
1616
next-core = { workspace = true }
1717
turbopack-core = { workspace = true }
1818

19-
#turbopack-binding = { workspace = true, features = [
20-
# "__turbo_tasks",
21-
# "__turbo_tasks_memory",
22-
# "__turbo_tasks_env",
23-
# "__turbo_tasks_fs",
24-
# "__turbo_tasks_memory",
25-
# "__turbopack",
26-
# "__turbopack_nodejs",
27-
# "__turbopack_core",
28-
# "__turbopack_browser",
29-
# "__turbopack_ecmascript",
30-
# "__turbopack_ecmascript_runtime",
31-
# "__turbopack_env",
32-
# "__turbopack_node",
33-
#] }
34-
3519
[build-dependencies]
3620
turbo-tasks-build = { workspace = true }
37-
vergen = { version = "7.3.2", default-features = false, features = [
38-
"cargo",
39-
"build",
40-
] }

crates/next-build/build.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
use turbo_tasks_build::generate_register;
2-
use vergen::{vergen, Config};
32

43
fn main() {
54
generate_register();
6-
7-
// Attempt to collect some build time env values but will skip if there are any
8-
// errors.
9-
let _ = vergen(Config::default());
105
}

turbopack/crates/turbo-tasks-backend/src/database/db_versioning.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,31 @@ use std::{
77

88
use anyhow::Result;
99

10+
/// Information gathered by `vergen_git2` in the top-level binary crate and passed down. This
11+
/// information must be computed in the top-level crate for cargo incremental compilation to work
12+
/// correctly.
13+
///
14+
/// See `crates/napi/build.rs` for details.
15+
pub struct GitVersionInfo<'a> {
16+
/// Output of `git describe --match 'v[0-9]' --dirty`.
17+
pub describe: &'a str,
18+
/// Is the git repository dirty? Always forced to `false` when the `CI` environment variable is
19+
/// set and non-empty.
20+
pub dirty: bool,
21+
}
22+
1023
/// Specifies many databases that have a different version than the current one are retained.
1124
/// For example if MAX_OTHER_DB_VERSIONS is 2, there can be at most 3 databases in the directory,
1225
/// the current one and two older/newer ones.
1326
const MAX_OTHER_DB_VERSIONS: usize = 2;
1427

15-
pub fn handle_db_versioning(base_path: &Path, version_info: &str) -> Result<PathBuf> {
28+
pub fn handle_db_versioning(base_path: &Path, version_info: &GitVersionInfo) -> Result<PathBuf> {
1629
if let Ok(version) = env::var("TURBO_ENGINE_VERSION") {
1730
return Ok(base_path.join(version));
1831
}
1932
// Database versioning. Pass `TURBO_ENGINE_IGNORE_DIRTY` at runtime to ignore a
2033
// dirty git repository. Pass `TURBO_ENGINE_DISABLE_VERSIONING` at runtime to disable
2134
// versioning and always use the same database.
22-
let (version_info, git_dirty) = if let Some(version_info) = version_info.strip_suffix("-dirty")
23-
{
24-
(version_info, true)
25-
} else {
26-
(version_info, false)
27-
};
2835
let ignore_dirty = env::var("TURBO_ENGINE_IGNORE_DIRTY").ok().is_some();
2936
let disabled_versioning = env::var("TURBO_ENGINE_DISABLE_VERSIONING").ok().is_some();
3037
let version = if disabled_versioning {
@@ -33,14 +40,14 @@ pub fn handle_db_versioning(base_path: &Path, version_info: &str) -> Result<Path
3340
caching database might be required."
3441
);
3542
Some("unversioned")
36-
} else if !git_dirty {
37-
Some(version_info)
43+
} else if !version_info.dirty {
44+
Some(version_info.describe)
3845
} else if ignore_dirty {
3946
println!(
4047
"WARNING: The git repository is dirty, but Persistent Caching is still enabled. \
4148
Manual removal of the persistent caching database might be required."
4249
);
43-
Some(version_info)
50+
Some(version_info.describe)
4451
} else {
4552
println!(
4653
"WARNING: The git repository is dirty: Persistent Caching is disabled. Use \

turbopack/crates/turbo-tasks-backend/src/lib.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ use std::path::Path;
1515

1616
use anyhow::Result;
1717

18-
pub use self::{
19-
backend::{BackendOptions, StorageMode, TurboTasksBackend},
20-
kv_backing_storage::KeyValueDatabaseBackingStorage,
21-
};
2218
use crate::database::{
2319
db_versioning::handle_db_versioning, noop_kv::NoopKvDb, turbo::TurboKeyValueDatabase,
2420
};
21+
pub use crate::{
22+
backend::{BackendOptions, StorageMode, TurboTasksBackend},
23+
database::db_versioning::GitVersionInfo,
24+
kv_backing_storage::KeyValueDatabaseBackingStorage,
25+
};
2526

2627
#[cfg(feature = "lmdb")]
2728
pub type LmdbBackingStorage = KeyValueDatabaseBackingStorage<
@@ -35,7 +36,10 @@ pub type LmdbBackingStorage = KeyValueDatabaseBackingStorage<
3536
>;
3637

3738
#[cfg(feature = "lmdb")]
38-
pub fn lmdb_backing_storage(path: &Path, version_info: &str) -> Result<LmdbBackingStorage> {
39+
pub fn lmdb_backing_storage(
40+
path: &Path,
41+
version_info: &GitVersionInfo,
42+
) -> Result<LmdbBackingStorage> {
3943
use crate::database::{
4044
fresh_db_optimization::{is_fresh, FreshDbOptimization},
4145
read_transaction_cache::ReadTransactionCache,
@@ -53,7 +57,10 @@ pub fn lmdb_backing_storage(path: &Path, version_info: &str) -> Result<LmdbBacki
5357

5458
pub type TurboBackingStorage = KeyValueDatabaseBackingStorage<TurboKeyValueDatabase>;
5559

56-
pub fn turbo_backing_storage(path: &Path, version_info: &str) -> Result<TurboBackingStorage> {
60+
pub fn turbo_backing_storage(
61+
path: &Path,
62+
version_info: &GitVersionInfo,
63+
) -> Result<TurboBackingStorage> {
5764
let path = handle_db_versioning(path, version_info)?;
5865
let database = TurboKeyValueDatabase::new(path)?;
5966
Ok(KeyValueDatabaseBackingStorage::new(database))
@@ -69,14 +76,20 @@ pub fn noop_backing_storage() -> NoopBackingStorage {
6976
pub type DefaultBackingStorage = LmdbBackingStorage;
7077

7178
#[cfg(feature = "lmdb")]
72-
pub fn default_backing_storage(path: &Path, version_info: &str) -> Result<DefaultBackingStorage> {
79+
pub fn default_backing_storage(
80+
path: &Path,
81+
version_info: &GitVersionInfo,
82+
) -> Result<DefaultBackingStorage> {
7383
lmdb_backing_storage(path, version_info)
7484
}
7585

7686
#[cfg(not(feature = "lmdb"))]
7787
pub type DefaultBackingStorage = TurboBackingStorage;
7888

7989
#[cfg(not(feature = "lmdb"))]
80-
pub fn default_backing_storage(path: &Path, version_info: &str) -> Result<DefaultBackingStorage> {
90+
pub fn default_backing_storage(
91+
path: &Path,
92+
version_info: &GitVersionInfo,
93+
) -> Result<DefaultBackingStorage> {
8194
turbo_backing_storage(path, version_info)
8295
}

0 commit comments

Comments
 (0)