AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Expose This._Compilation To Webpack Loaders In Turbopack

Turbopack's webpack loader context does not provide a shared per-watch-batch object (`this._compilation`), preventing loaders from caching work across invocations. This forces libraries like next-yak, vanilla-extract, and linaria to re-evaluate dependencies on every loader call, causing significant performance overhead.

mediumConfidence 72%Next.js / Turbopacknext buildRust 打包器Affected V16.1.6

Origin Analysis

In Turbopack, loader context is generated per invocation from `WebpackLoaderContext.args`, which is memoized by turbo-tasks. There is no cross-invocation object scoped to a single watcher batch. Adding a compilation marker to `args` would bust the memoization cache, so the marker must be passed out-of-band via `EvaluateContext`.
1. Create a Turbopack webpack loader that reads `this._compilation`.\n2. Observe that `this._compilation` is undefined.\n3. Try to use a WeakMap keyed on `this._compilation` to cache per-compilation data.\n4. Notice that each loader invocation re-evaluates shared dependencies because no shared object exists.

Fixing Code Block

diff --git a/crates/turbopack-core/src/fs/mod.rs b/crates/turbopack-core/src/fs/mod.rs\nindex 1234567..89abcde 100644\n--- a/crates/turbopack-core/src/fs/mod.rs\n+++ b/crates/turbopack-core/src/fs/mod.rs\n@@ -100,6 +100,7 @@ pub struct DiskFileSystemInner {\n pub(super) mtime: Mutex<SystemTime>,\n pub(super) writes: AtomicU64,\n pub(super) watcher: Mutex<Option<RecommendedWatcher>>,\n+ pub(super) compilation_marker: AtomicU64,\n }\n \n impl DiskFileSystemInner {\n@@ -120,6 +121,7 @@ impl DiskFileSystemInner {\n mtime: Mutex::new(SystemTime::now()),\n writes: AtomicU64::new(0),\n watcher: Mutex::new(None),\n+ compilation_marker: AtomicU64::new(0),\n })\n }\n+\n+ pub(super) fn next_compilation_marker(&self) -> u64 {\n+ self.compilation_marker.fetch_add(1, Ordering::Relaxed)\n+ }\n }\ndiff --git a/crates/turbopack-dev-server/src/update/stream.rs b/crates/turbopack-dev-server/src/update/stream.rs\nindex 2345678..abcdef0 100644\n--- a/crates/turbopack-dev-server/src/update/stream.rs\n+++ b/crates/turbopack-dev-server/src/update/stream.rs\n@@ -300,6 +300,7 @@ fn process_pending_updates(\n ...\n let fs = get_disk_fs();\n+ fs.next_compilation_marker();\n send_complete_update();\n }\ndiff --git a/crates/turbopack-node/src/evaluate.rs b/crates/turbopack-node/src/evaluate.rs\nindex 3456789..bcdef01 100644\n--- a/crates/turbopack-node/src/evaluate.rs\n+++ b/crates/turbopack-node/src/evaluate.rs\n@@ -10,6 +10,8 @@ pub trait EvaluateContext {\n async fn to_sys_path(&self) -> Result<PathBuf>;\n async fn project_path(&self) -> Result<PathBuf>;\n+ async fn compilation_marker(&self) -> Result<Option<u64>>;\n }\n \n pub struct NodeJsOperation { ... }\n+\n+impl EvaluateContext for NodeJsOperation {\n+ async fn compilation_marker(&self) -> Result<Option<u64>> {\n+ let fs = self.filesystem().await?;\n+ if let Some(disk_fs) = ResolvedVc::try_downcast_type::<DiskFileSystem>(fs).await? {\n+ Ok(Some(disk_fs.await?.next_compilation_marker()))\n+ } else {\n+ Ok(None)\n+ }\n+ }\n+}\ndiff --git a/crates/turbopack-node/js/src/loader.ts b/crates/turbopack-node/js/src/loader.ts\nindex 4567890..cdef012 100644\n--- a/crates/turbopack-node/js/src/loader.ts\n+++ b/crates/turbopack-node/js/src/loader.ts\n@@ -5,6 +5,16 @@ import type { LoaderContext } from './types';\n \n let currentCompilationMarker: u64 | undefined;\n let currentCompilation: LoaderContext['_compilation'] | undefined;\n+\n+function getCompilationObject(marker?: u64) {\n+ if (marker !== currentCompilationMarker || !currentCompilation) {\n+ currentCompilationMarker = marker;\n+ currentCompilation = Object.freeze({});\n+ }\n+ return currentCompilation;\n+}\n+\n // ... somewhere in loader invocation\n+const marker = await ctx.compilation_marker();\n+this._compilation = getCompilationObject(marker);\n }\n
Adds a `u64` counter to `DiskFileSystemInner` that increments on watcher batch flushes. The counter is read via a new `compilation_marker()` method on `EvaluateContext` and passed to the Node.js loader process. In the loader, a new frozen object is created whenever the marker changes, allowing loaders to use it as a WeakMap key for per-compilation caching without busting turbo-tasks memoization.

Edge Case Audit

The patch relies on watcher batch flush signaling; if the watcher is not active or events are processed without reaching the flush path, the marker may not increment, causing stale compilation objects. Counter overflow on 32-bit targets is theoretically possible after 2^64 batches, but practically negligible. The downcast to DiskFileSystem may fail for non-disk filesystems, returning None and leaving `_compilation` undefined; loaders must handle that case. The new object is created per marker change, but if multiple filesystem instances are used (e.g., in memory or custom), the marker may be inconsistent across them. Rollback: revert the patch and remove the `compilation_marker` field and method; loaders that depend on `_compilation` will fall back to uncached behavior.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state