Skip to content

Commit ac4cadd

Browse files
committed
fix(cache): skip context module cache
1 parent c16c41f commit ac4cadd

6 files changed

Lines changed: 28 additions & 104 deletions

File tree

crates/rspack_core/src/cache/codec.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ use rspack_paths::Utf8PathBuf;
99

1010
/// Internal cacheable context for serialization
1111
#[derive(Debug, Clone)]
12-
pub(crate) struct CacheCodecContext {
12+
struct Context {
1313
portable_project_root: Option<Utf8PathBuf>,
14-
omit_module_factory_state: bool,
1514
}
1615

17-
impl CacheCodecContext {
18-
pub(crate) fn omit_module_factory_state(&self) -> bool {
19-
self.omit_module_factory_state
20-
}
21-
}
22-
23-
impl rspack_cacheable::CacheableContext for CacheCodecContext {
16+
impl rspack_cacheable::CacheableContext for Context {
2417
fn project_root(&self) -> Option<&Path> {
2518
self.portable_project_root.as_ref().map(|p| p.as_std_path())
2619
}
@@ -44,24 +37,14 @@ impl rspack_cacheable::CacheableContext for CacheCodecContext {
4437
/// ```
4538
#[derive(Debug, Clone)]
4639
pub struct CacheCodec {
47-
context: CacheCodecContext,
40+
context: Context,
4841
}
4942

5043
impl CacheCodec {
5144
pub fn new(portable_project_root: Option<Utf8PathBuf>) -> Self {
5245
Self {
53-
context: CacheCodecContext {
54-
portable_project_root,
55-
omit_module_factory_state: false,
56-
},
57-
}
58-
}
59-
60-
pub(crate) fn new_for_module_cache(portable_project_root: Option<Utf8PathBuf>) -> Self {
61-
Self {
62-
context: CacheCodecContext {
46+
context: Context {
6347
portable_project_root,
64-
omit_module_factory_state: true,
6548
},
6649
}
6750
}

crates/rspack_core/src/cache/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ pub use cache_entry::{
99
CachedExtractedComments, CachedMinimizeEntry, CachedSourceMapDevToolPluginEntry,
1010
};
1111
pub use codec::CacheCodec;
12-
pub(crate) use codec::CacheCodecContext;
1312
pub use options::{BuildDepsOptions, PersistentCacheOptions, StorageOptions};
1413
pub use snapshot::{PathMatcher, SnapshotOptions, SnapshotStrategyOptions};

crates/rspack_core/src/compilation/build_module_graph/graph_updater/repair/add.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ impl Task<TaskContext> for AddTask {
114114
return Ok(vec![]);
115115
}
116116

117-
let module_build = match context.module_cache.restore(module_identifier)? {
118-
Some(mut build_result) => {
119-
build_result.module.update_cache_module(&mut module);
120-
ModuleBuild::Cached(build_result)
117+
let module_build = if module.as_normal_module().is_some() {
118+
match context.module_cache.restore(module_identifier)? {
119+
Some(mut build_result) => {
120+
build_result.module.update_cache_module(&mut module);
121+
ModuleBuild::Cached(build_result)
122+
}
123+
None => ModuleBuild::Fresh(module),
121124
}
122-
None => ModuleBuild::Fresh(module),
125+
} else {
126+
ModuleBuild::Fresh(module)
123127
};
124128

125129
context

crates/rspack_core/src/context_module.rs

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@ use futures::future::BoxFuture;
77
use indoc::formatdoc;
88
use itertools::Itertools;
99
use rspack_cacheable::{
10-
__private::rkyv::{
11-
Place,
12-
de::Pooling,
13-
rancor::Fallible,
14-
ser::Sharing,
15-
with::{ArchiveWith, DeserializeWith, SerializeWith},
16-
},
17-
ContextGuard, Error as CacheableError, cacheable, cacheable_dyn,
18-
with::{AsCacheable, AsOption, AsPreset, AsVec},
10+
cacheable, cacheable_dyn,
11+
with::{AsCacheable, AsOption, AsPreset, AsVec, Unsupported},
1912
};
2013
use rspack_collections::{Identifiable, Identifier};
2114
use rspack_error::{Result, impl_empty_diagnosable_trait};
@@ -41,9 +34,8 @@ use crate::{
4134
LibIdentOptions, Module, ModuleArgument, ModuleCodeGenerationContext, ModuleCodeTemplate,
4235
ModuleGraph, ModuleId, ModuleIdsArtifact, ModuleLayer, ModuleType, NeedBuildContext,
4336
RealDependencyLocation, ReferencedSpecifier, Resolve, RuntimeGlobals, RuntimeGlobalsRenderMode,
44-
RuntimeSpec, SnapshotValidationResult, SourceType, cache::CacheCodecContext, contextify,
45-
get_exports_type_with_strict, get_outgoing_async_modules, impl_module_meta_info,
46-
module_update_hash, property_access, to_path,
37+
RuntimeSpec, SnapshotValidationResult, SourceType, contextify, get_exports_type_with_strict,
38+
get_outgoing_async_modules, impl_module_meta_info, module_update_hash, property_access, to_path,
4739
};
4840

4941
static CHUNK_NAME_INDEX_PLACEHOLDER: &str = "[index]";
@@ -282,65 +274,6 @@ pub type ResolveContextModuleDependencies = Arc<
282274
+ Sync,
283275
>;
284276

285-
/// Context dependency resolution is factory state and is restored from the
286-
/// fresh module by `update_cache_module`, just like webpack's
287-
/// `ContextModule.resolveDependencies`.
288-
struct SkipResolveContextModuleDependencies;
289-
290-
impl ArchiveWith<ResolveContextModuleDependencies> for SkipResolveContextModuleDependencies {
291-
type Archived = ();
292-
type Resolver = ();
293-
294-
fn resolve_with(
295-
_field: &ResolveContextModuleDependencies,
296-
_resolver: Self::Resolver,
297-
_out: Place<Self::Archived>,
298-
) {
299-
}
300-
}
301-
302-
impl<S> SerializeWith<ResolveContextModuleDependencies, S> for SkipResolveContextModuleDependencies
303-
where
304-
S: Fallible<Error = CacheableError> + Sharing + ?Sized,
305-
{
306-
fn serialize_with(
307-
_field: &ResolveContextModuleDependencies,
308-
serializer: &mut S,
309-
) -> std::result::Result<Self::Resolver, CacheableError> {
310-
let context =
311-
ContextGuard::sharing_guard(serializer)?.downcast_context::<CacheCodecContext>()?;
312-
if context.omit_module_factory_state() {
313-
Ok(())
314-
} else {
315-
Err(CacheableError::UnsupportedField)
316-
}
317-
}
318-
}
319-
320-
impl<D> DeserializeWith<(), ResolveContextModuleDependencies, D>
321-
for SkipResolveContextModuleDependencies
322-
where
323-
D: Fallible<Error = CacheableError> + Pooling + ?Sized,
324-
{
325-
fn deserialize_with(
326-
_field: &(),
327-
deserializer: &mut D,
328-
) -> std::result::Result<ResolveContextModuleDependencies, CacheableError> {
329-
let context =
330-
ContextGuard::pooling_guard(deserializer)?.downcast_context::<CacheCodecContext>()?;
331-
if !context.omit_module_factory_state() {
332-
return Err(CacheableError::UnsupportedField);
333-
}
334-
Ok(Arc::new(|_| {
335-
Box::pin(async {
336-
Err(rspack_error::error!(
337-
"Context module resolver was not restored from the fresh module"
338-
))
339-
})
340-
}))
341-
}
342-
}
343-
344277
#[impl_source_map_config]
345278
#[cacheable]
346279
#[derive(Debug)]
@@ -353,7 +286,7 @@ pub struct ContextModule {
353286
build_info: BuildInfo,
354287
build_meta: BuildMeta,
355288
#[debug(skip)]
356-
#[cacheable(with=SkipResolveContextModuleDependencies)]
289+
#[cacheable(with=Unsupported)]
357290
resolve_dependencies: ResolveContextModuleDependencies,
358291
force_build: bool,
359292
}

crates/rspack_core/src/new_cache/module_cache.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ use crate::{
77
cache::{CacheCodec, SnapshotStrategyOptions},
88
};
99

10-
/// Persistent cache for completed module builds.
10+
/// Persistent cache for completed normal module builds.
1111
///
1212
/// Webpack caches the `Module` itself. Rspack keeps dependencies and blocks in
1313
/// `BuildResult` until they are inserted into the module graph, so caching the
1414
/// complete build result is the equivalent representation. The serialized form
1515
/// also gives each compilation an exclusively owned module while the generic
1616
/// cache continues to expose shared immutable values.
17+
///
18+
/// Context modules are intentionally built fresh until their factory state can
19+
/// be restored without serializing the process-local dependency resolver.
1720
#[derive(Debug, Clone)]
1821
pub(crate) struct ModuleCache {
1922
cache: CacheFacade,
@@ -31,7 +34,7 @@ impl ModuleCache {
3134
};
3235
Self {
3336
cache: cache.facade("Compilation/modules"),
34-
codec: CacheCodec::new_for_module_cache(project_root),
37+
codec: CacheCodec::new(project_root),
3538
// Incremental make reuses the previous module graph and owns its own
3639
// invalidation path. Keep that fast path unchanged.
3740
enabled: options.experiments.new_cache.module && !is_rebuild,
@@ -66,8 +69,11 @@ impl ModuleCache {
6669
}
6770

6871
let module = &mut result.module;
69-
if (module.as_normal_module().is_some() || module.as_context_module().is_some())
70-
&& module.build_info().cacheable
72+
if module.as_normal_module().is_none() {
73+
return Ok(());
74+
}
75+
76+
if module.build_info().cacheable
7177
&& !module
7278
.diagnostics()
7379
.iter()

tests/rspack-test/cacheCases/common/module-cache-new-cache/rspack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ module.exports = {
5555
} else {
5656
expect(builtModules).toEqual(['changed.js']);
5757
expect(stillValidModules.sort()).toEqual([
58-
'context',
5958
'index.js',
6059
'stable.js',
6160
'value.js',

0 commit comments

Comments
 (0)