Skip to content

Commit 72e8da2

Browse files
committed
fix(wasm): keep cli storage helpers out of browser build
1 parent d196569 commit 72e8da2

5 files changed

Lines changed: 140 additions & 22 deletions

File tree

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

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
use std::{borrow::Borrow, path::PathBuf, sync::Arc};
2+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
13
use std::{
2-
borrow::Borrow,
34
env,
4-
path::PathBuf,
5-
sync::{Arc, LazyLock, Mutex, PoisonError, Weak},
5+
sync::{LazyLock, Mutex, PoisonError, Weak},
66
};
77

8-
use anyhow::{Context, Result};
8+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
9+
use anyhow::Context;
10+
use anyhow::Result;
911
use smallvec::SmallVec;
12+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
1013
use turbo_bincode::{new_turbo_bincode_decoder, turbo_bincode_decode, turbo_bincode_encode};
14+
use turbo_tasks::{DynTaskInputs, RawVc, TaskId, macro_helpers::NativeFunction};
15+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
1116
use turbo_tasks::{
12-
DynTaskInputs, RawVc, TaskId,
13-
macro_helpers::NativeFunction,
1417
panic_hooks::{PanicHookGuard, register_panic_hook},
1518
parallel,
1619
};
@@ -19,10 +22,13 @@ use crate::{
1922
GitVersionInfo,
2023
backend::{AnyOperation, SpecificTaskDataCategory, storage_schema::TaskStorage},
2124
backing_storage::{SnapshotItem, SnapshotMeta, compute_task_type_hash_from_components},
25+
database::{db_invalidation::StartupCacheState, key_value_database::KeySpace},
26+
};
27+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
28+
use crate::{
2229
database::{
23-
db_invalidation::{StartupCacheState, check_db_invalidation_and_cleanup, invalidate_db},
30+
db_invalidation::{check_db_invalidation_and_cleanup, invalidate_db},
2431
db_versioning::handle_db_versioning,
25-
key_value_database::KeySpace,
2632
turbo::{TurboKeyValueDatabase, TurboWriteBatch},
2733
write_batch::WriteBuffer,
2834
},
@@ -32,20 +38,24 @@ use crate::{
3238
const META_KEY_OPERATIONS: u32 = 0;
3339
const META_KEY_NEXT_FREE_TASK_ID: u32 = 1;
3440

41+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
3542
struct IntKey([u8; 4]);
3643

44+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
3745
impl IntKey {
3846
fn new(value: u32) -> Self {
3947
Self(value.to_le_bytes())
4048
}
4149
}
4250

51+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
4352
impl AsRef<[u8]> for IntKey {
4453
fn as_ref(&self) -> &[u8] {
4554
&self.0
4655
}
4756
}
4857

58+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
4959
fn as_u32(bytes: impl Borrow<[u8]>) -> Result<u32> {
5060
let n = u32::from_le_bytes(bytes.borrow().try_into()?);
5161
Ok(n)
@@ -59,6 +69,7 @@ fn as_u32(bytes: impl Borrow<[u8]>) -> Result<u32> {
5969
//
6070
// These overrides let us avoid the cache invalidation / error suppression within Vercel so that we
6171
// feel these pain points and fix the root causes of bugs.
72+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
6273
fn should_invalidate_on_panic() -> bool {
6374
fn env_is_falsy(key: &str) -> bool {
6475
env::var_os(key)
@@ -70,6 +81,7 @@ fn should_invalidate_on_panic() -> bool {
7081
*SHOULD_INVALIDATE
7182
}
7283

84+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
7385
struct TurboBackingStorageInner {
7486
database: TurboKeyValueDatabase,
7587
/// Used when calling [`TurboBackingStorage::invalidate`]. Can be `None` in the
@@ -89,11 +101,16 @@ struct TurboBackingStorageInner {
89101
/// backend needs (snapshots, task-candidate lookups, etc.).
90102
///
91103
/// [`TurboTasksBackend::new`]: crate::TurboTasksBackend::new
104+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
92105
pub struct TurboBackingStorage {
93106
// wrapped so that `register_panic_hook` can hold a weak reference to `inner`.
94107
inner: Arc<TurboBackingStorageInner>,
95108
}
96109

110+
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
111+
pub struct TurboBackingStorage;
112+
113+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
97114
impl TurboBackingStorage {
98115
pub(crate) fn new_in_memory(database: TurboKeyValueDatabase) -> Self {
99116
Self {
@@ -157,6 +174,7 @@ impl TurboBackingStorage {
157174
}
158175
}
159176

177+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
160178
impl TurboBackingStorageInner {
161179
fn invalidate(&self, reason_code: &str) -> Result<()> {
162180
// `base_path` is `None` for in-memory backing storage (see `noop_backing_storage`).
@@ -190,6 +208,7 @@ impl TurboBackingStorageInner {
190208
}
191209
}
192210

211+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
193212
impl TurboBackingStorage {
194213
/// Called when the database should be invalidated upon re-initialization.
195214
///
@@ -408,6 +427,75 @@ impl TurboBackingStorage {
408427
}
409428
}
410429

430+
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
431+
impl TurboBackingStorage {
432+
pub(crate) fn new_in_memory() -> Self {
433+
Self
434+
}
435+
436+
pub(crate) fn invalidate(&self, _reason_code: &str) -> Result<()> {
437+
Ok(())
438+
}
439+
440+
pub(crate) fn next_free_task_id(&self) -> Result<TaskId> {
441+
Ok(TaskId::MIN)
442+
}
443+
444+
pub(crate) fn uncompleted_operations(&self) -> Result<Vec<AnyOperation>> {
445+
Ok(Vec::new())
446+
}
447+
448+
pub(crate) fn save_snapshot<I>(
449+
&self,
450+
_operations: Vec<Arc<AnyOperation>>,
451+
_snapshots: Vec<I>,
452+
) -> Result<SnapshotMeta>
453+
where
454+
I: IntoIterator<Item = SnapshotItem> + Send + Sync,
455+
{
456+
Ok(SnapshotMeta::default())
457+
}
458+
459+
pub(crate) fn lookup_task_candidates(
460+
&self,
461+
_native_fn: &'static NativeFunction,
462+
_this: Option<RawVc>,
463+
_arg: &dyn DynTaskInputs,
464+
) -> Result<SmallVec<[TaskId; 1]>> {
465+
Ok(SmallVec::new())
466+
}
467+
468+
pub(crate) fn lookup_data(
469+
&self,
470+
_task_id: TaskId,
471+
_category: SpecificTaskDataCategory,
472+
_storage: &mut TaskStorage,
473+
) -> Result<()> {
474+
Ok(())
475+
}
476+
477+
pub(crate) fn batch_lookup_data(
478+
&self,
479+
task_ids: &[TaskId],
480+
_category: SpecificTaskDataCategory,
481+
) -> Result<Vec<TaskStorage>> {
482+
Ok(task_ids.iter().map(|_| TaskStorage::new()).collect())
483+
}
484+
485+
pub(crate) fn compact(&self) -> Result<bool> {
486+
Ok(false)
487+
}
488+
489+
pub(crate) fn shutdown(&self) -> Result<()> {
490+
Ok(())
491+
}
492+
493+
pub(crate) fn has_unrecoverable_write_error(&self) -> bool {
494+
false
495+
}
496+
}
497+
498+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
411499
fn get_next_free_task_id(batch: &TurboWriteBatch<'_>) -> Result<u32, anyhow::Error> {
412500
Ok(
413501
match batch.get(
@@ -420,6 +508,7 @@ fn get_next_free_task_id(batch: &TurboWriteBatch<'_>) -> Result<u32, anyhow::Err
420508
)
421509
}
422510

511+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
423512
fn save_infra(
424513
batch: &TurboWriteBatch<'_>,
425514
next_task_id: u32,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ pub fn turbo_backing_storage(
5151
/// Creates an in-memory `BackingStorage` to be passed to [`TurboTasksBackend::new`]. Backed by
5252
/// an empty, read-only [`TurboPersistence`] — reads return `None`, writes are not expected
5353
/// (callers should set [`BackendOptions::storage_mode`] to `None`).
54+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
5455
pub fn noop_backing_storage() -> TurboBackingStorage {
5556
TurboBackingStorage::new_in_memory(TurboKeyValueDatabase::empty_in_memory())
5657
}
5758

59+
/// Creates a no-op `BackingStorage` for browser wasm builds.
60+
///
61+
/// Browser wasm has no on-disk persistence; callers should set
62+
/// [`BackendOptions::storage_mode`] to `None`.
63+
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
64+
pub fn noop_backing_storage() -> TurboBackingStorage {
65+
TurboBackingStorage::new_in_memory()
66+
}
67+
5868
/// Opens a Turbopack persistent cache database at the given base path and performs a full
5969
/// compaction. This is intended for use by the `next internal post-build` CLI command to optimize
6070
/// the database after a build, without requiring the full turbo-tasks runtime.

turbopack/crates/turbopack-cli-utils/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ workspace = true
1717
[dependencies]
1818
anyhow = { workspace = true }
1919
async-trait = { workspace = true }
20-
crossterm = "0.29.0"
2120
clap = { workspace = true, features = ["derive"] }
2221
owo-colors = { workspace = true }
2322
rustc-hash = { workspace = true }
@@ -27,3 +26,6 @@ turbo-tasks = { workspace = true }
2726
turbo-tasks-fs = { workspace = true }
2827
turbopack-core = { workspace = true }
2928
turbopack-resolve = { workspace = true }
29+
30+
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
31+
crossterm = "0.29.0"

turbopack/crates/turbopack-cli-utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(arbitrary_self_types)]
33
#![feature(arbitrary_self_types_pointers)]
44

5+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
56
pub mod issue;
67
pub mod runtime_entry;
78
pub mod source_context;

turbopack/crates/turbopack-ecmascript/src/analyzer/well_known/mod.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -593,19 +593,35 @@ fn require_context_require_resolve<'a>(
593593
fn path_to_file_url<'a>(arena: &'a Bump, args: BumpVec<'a, JsValue<'a>>) -> JsValue<'a> {
594594
if args.len() == 1 {
595595
if let Some(path) = args[0].as_str() {
596-
Url::from_file_path(path)
597-
.map(|url| JsValue::Url(String::from(url).into(), JsValueUrlKind::Absolute))
598-
.unwrap_or_else(|_| {
599-
JsValue::unknown(
600-
JsValue::call_from_parts(
601-
arena,
602-
JsValue::WellKnownFunction(WellKnownFunctionKind::PathToFileUrl),
603-
args,
604-
),
605-
true,
606-
rcstr!("url not parseable: path is relative or has an invalid prefix"),
607-
)
608-
})
596+
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
597+
{
598+
Url::from_file_path(path)
599+
.map(|url| JsValue::Url(String::from(url).into(), JsValueUrlKind::Absolute))
600+
.unwrap_or_else(|_| {
601+
JsValue::unknown(
602+
JsValue::call_from_parts(
603+
arena,
604+
JsValue::WellKnownFunction(WellKnownFunctionKind::PathToFileUrl),
605+
args,
606+
),
607+
true,
608+
rcstr!("url not parseable: path is relative or has an invalid prefix"),
609+
)
610+
})
611+
}
612+
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
613+
{
614+
let _ = path;
615+
JsValue::unknown(
616+
JsValue::call_from_parts(
617+
arena,
618+
JsValue::WellKnownFunction(WellKnownFunctionKind::PathToFileUrl),
619+
args,
620+
),
621+
true,
622+
rcstr!("pathToFileURL constant evaluation is not supported in browser wasm"),
623+
)
624+
}
609625
} else {
610626
JsValue::unknown(
611627
JsValue::call_from_parts(

0 commit comments

Comments
 (0)