Skip to content

Commit 2c4953e

Browse files
authored
Clear the resolver cache on each build (#894)
1 parent 617a318 commit 2c4953e

File tree

7 files changed

+33
-3
lines changed

7 files changed

+33
-3
lines changed

.changeset/wild-ducks-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@atlaspack/rust': patch
3+
---
4+
5+
Add an `on_new_build` hook to the ResolverPlugin trait, which allows us to clear the resolver cache between builds. This allows for the discovery of previously non-existent assets on the next incremental build.

crates/atlaspack/src/atlaspack.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ impl Atlaspack {
158158
impl Atlaspack {
159159
pub fn build_asset_graph(&self) -> anyhow::Result<(Arc<AssetGraph>, bool)> {
160160
self.runtime.block_on(async move {
161+
// Notify all resolver plugins that a new build is starting
162+
for resolver in self.plugins.resolvers()? {
163+
resolver.on_new_build();
164+
}
165+
161166
let mut request_tracker = self.request_tracker.write().await;
162167

163168
let prev_asset_graph = request_tracker

crates/atlaspack_core/src/plugin/resolver_plugin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub trait ResolverPlugin: Any + Debug + Send + Sync {
8787
}
8888
/// Determines what the dependency specifier resolves to
8989
async fn resolve(&self, ctx: ResolveContext) -> Result<Resolved, anyhow::Error>;
90+
91+
/// Called when a new build is started. No-op by default.
92+
fn on_new_build(&self) {}
9093
}
9194

9295
#[cfg(test)]

crates/atlaspack_plugin_resolver/src/atlaspack_resolver.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ impl ResolverPlugin for AtlaspackResolver {
454454
}),
455455
}
456456
}
457+
458+
fn on_new_build(&self) {
459+
self.cache.clear();
460+
}
457461
}
458462

459463
fn should_include_node_module(include_node_modules: &IncludeNodeModules, name: &str) -> bool {

crates/atlaspack_shared_map/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ where
5353
map.insert(key, value);
5454
}
5555

56+
pub fn clear(&self) {
57+
let map_cell = self.inner.write();
58+
let mut map = map_cell;
59+
map.clear();
60+
}
61+
5662
pub fn len(&self) -> usize {
5763
let map_cell = self.inner.read();
5864
let map = map_cell;

packages/core/integration-tests/test/watcher.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,11 @@ describe('watcher', function () {
468468
]);
469469
});
470470

471-
it.v2('should rebuild if a missing file is added', async function () {
471+
it('should rebuild if a missing file is added', async function () {
472472
await outputFS.mkdirp(inputDir);
473473
await outputFS.writeFile(
474474
path.join(inputDir, '/index.js'),
475475
'import {other} from "./other";\nexport default other;',
476-
{encoding: 'utf8'},
477476
);
478477

479478
let b = bundler(path.join(inputDir, 'index.js'), {inputFS: overlayFS});
@@ -484,7 +483,6 @@ describe('watcher', function () {
484483
await outputFS.writeFile(
485484
path.join(inputDir, '/other.js'),
486485
'export const other = 2;',
487-
{encoding: 'utf8'},
488486
);
489487

490488
buildEvent = await getNextBuild(b);

packages/utils/node-resolver-rs/src/cache.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ impl Cache {
126126
Ok(self.fs.canonicalize(path, &self.realpath_cache)?)
127127
}
128128

129+
pub fn clear(&self) {
130+
self.packages.clear();
131+
self.package_duplicates.clear();
132+
self.tsconfigs.clear();
133+
self.is_dir_cache.clear();
134+
self.is_file_cache.clear();
135+
self.realpath_cache.clear();
136+
}
137+
129138
/// Calling this method before resolving will enable the package deduplication feature.
130139
/// It does thos by hydrating the "package_duplicates" lookup. This can
131140
/// the the be used when resolving packages that are duplicates, where a duplicate is a package

0 commit comments

Comments
 (0)