mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
### What?
The Turbopack persistent cache directory under
`.next/cache/turbopack/<version>/` is now versioned by the Next.js
package version concatenated with the git short SHA, instead of `git
describe`.
Example: `v16.0.1-canary.13-94e9fa6`
### Why?
The previous version key came from `VERGEN_GIT_DESCRIBE` (`git describe
--match 'v[0-9]' --dirty`), which depends on having an annotated `v*`
tag reachable from the build commit. That ties the cache version to
local git tag state and breaks down for builds against forks, shallow
clones, or branches without a recent matching tag.
The Next.js package version from `packages/next/package.json` is the
value users actually care about for cache compatibility, and it is
already plumbed through to the Rust side as
`ProjectOptions::next_version` (sourced from
`process.env.__NEXT_VERSION`). Combining it with the git short SHA keeps
per-commit cache uniqueness while removing the dependency on git tags.
### How?
**Rust (`crates/next-napi-bindings`)**
- `build.rs`: also emit `VERGEN_GIT_SHA` (short form) via
`vergen_gitcl`. `VERGEN_GIT_DESCRIBE` is still emitted and is still used
for the bug-report URL / panic log.
- `src/next_api/turbopack_ctx.rs`:
- New `cache_describe(next_version)` returns
`format!("v{next_version}-{}", env!("VERGEN_GIT_SHA"))`.
- `git_version_info` now takes the pre-built `describe` string instead
of reading `VERGEN_GIT_DESCRIBE` itself. `dirty` handling is unchanged
(still derived from `VERGEN_GIT_DIRTY`, still suppressed in CI), so
`handle_db_versioning`'s dirty-repo behavior is preserved.
- `create_turbo_tasks` takes `next_version: &str` and threads it
through.
- `src/next_api/project.rs`:
- `project_new` passes `options.next_version` into `create_turbo_tasks`.
No new field is needed on `NapiProjectOptions` — the `nextVersion` field
already exists and is already populated by all three call sites.
- `turbopack_database_compact` (the napi `databaseCompact` function used
by `next-post-build`) gains a `next_version: String` parameter so
post-build compaction targets the same versioned directory.
**JS (`packages/next`)**
- `src/build/swc/types.ts`, `generated-native.d.ts`, `index.ts`: update
the `databaseCompact(path, nextVersion)` signature on both the native
and WASM-fallback paths.
- `src/cli/next-post-build.ts`: passes `process.env.__NEXT_VERSION`
through to `bindings.turbo.databaseCompact`.
`turbopack/crates/turbopack-cli` (the standalone Turbopack CLI) is
intentionally left on `VERGEN_GIT_DESCRIBE` — it is not tied to a
Next.js package version.
---------
Co-authored-by: v-work-app[bot] <262237222+v-work-app[bot]@users.noreply.github.com>
Co-authored-by: Claude <[email protected]>
Co-authored-by: Tobias Koppers <[email protected]>
94 lines
3.8 KiB
Rust
94 lines
3.8 KiB
Rust
use std::{env, fs, path::Path, process::Command, str};
|
|
|
|
use serde_json::Value;
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
println!("cargo:rerun-if-env-changed=CI");
|
|
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_OS");
|
|
let is_macos_target = env::var("CARGO_CFG_TARGET_OS").is_ok_and(|value| value == "macos");
|
|
|
|
let nextjs_version = {
|
|
let package_json_path = Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../..")
|
|
.join("packages/next/package.json");
|
|
|
|
println!("cargo:rerun-if-changed={}", package_json_path.display());
|
|
|
|
let package_json_content = fs::read_to_string(&package_json_path)?;
|
|
let package_json: Value = serde_json::from_str(&package_json_content)?;
|
|
|
|
package_json["version"]
|
|
.as_str()
|
|
.expect("Expected a Next.js `version` string in its package.json")
|
|
.to_string()
|
|
};
|
|
|
|
// Make the Next.js version available as a build-time environment variable
|
|
println!("cargo:rustc-env=NEXTJS_VERSION={nextjs_version}");
|
|
|
|
// Generates, stores build-time information as static values.
|
|
// There are some places relying on correct values for this (i.e telemetry),
|
|
// So failing build if this fails.
|
|
let cargo = vergen_gitcl::CargoBuilder::default()
|
|
.target_triple(true)
|
|
.build()?;
|
|
// We use the git dirty state to disable filesystem cache (filesystem cache relies on a
|
|
// commit hash to be safe). One tradeoff of this is that we must invalidate the rust build more
|
|
// often.
|
|
//
|
|
// This invalidates the build if any untracked files change. That's sufficient for the case
|
|
// where we transition from dirty to clean.
|
|
//
|
|
// There's an edge-case here where the repository could be newly dirty, but we can't know
|
|
// because our build hasn't been invalidated, since the untracked files weren't untracked last
|
|
// time we ran. That will cause us to incorrectly report ourselves as clean.
|
|
//
|
|
// However, in practice that shouldn't be much of an issue: If no other dependency of this
|
|
// top-level crate has changed (which would've triggered our rebuild), then the resulting binary
|
|
// must be equivalent to a clean build anyways. Therefore, filesystem cache using the HEAD
|
|
// commit hash as a version is okay.
|
|
let git = vergen_gitcl::GitclBuilder::default()
|
|
.dirty(/* include_untracked */ true)
|
|
.sha(/* short */ true)
|
|
.build()?;
|
|
vergen_gitcl::Emitter::default()
|
|
.fail_on_error()
|
|
.add_instructions(&cargo)?
|
|
.add_instructions(&git)?
|
|
.emit()?;
|
|
|
|
match Command::new("git").args(["rev-parse", "HEAD"]).output() {
|
|
Ok(out) if out.status.success() => println!(
|
|
"cargo:warning=git HEAD: {}",
|
|
str::from_utf8(&out.stdout).unwrap()
|
|
),
|
|
Ok(out) => println!(
|
|
"cargo:warning=`git rev-parse HEAD` failed with status {}: {}",
|
|
out.status,
|
|
str::from_utf8(&out.stderr).unwrap()
|
|
),
|
|
Err(e) => println!("cargo:warning=`git rev-parse HEAD` could not be spawned: {e}"),
|
|
}
|
|
|
|
if !is_macos_target {
|
|
napi_build::setup();
|
|
}
|
|
|
|
// This is a workaround for napi always including a GCC-specific flag on macOS.
|
|
if is_macos_target {
|
|
println!("cargo:rerun-if-env-changed=DEBUG_GENERATED_CODE");
|
|
println!("cargo:rerun-if-env-changed=TYPE_DEF_TMP_PATH");
|
|
println!("cargo:rerun-if-env-changed=CARGO_CFG_NAPI_RS_CLI_VERSION");
|
|
|
|
println!("cargo:rustc-cdylib-link-arg=-undefined");
|
|
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
|
|
}
|
|
|
|
// Resolve a potential linker issue for unit tests on linux
|
|
// https://github.com/napi-rs/napi-rs/issues/1782
|
|
#[cfg(all(target_os = "linux", not(target_arch = "wasm32")))]
|
|
println!("cargo:rustc-link-arg=-Wl,--warn-unresolved-symbols");
|
|
|
|
Ok(())
|
|
}
|