Skip to content

Commit eb6f410

Browse files
committed
refactor(project): Import/overwrite stages from cache after saving
1 parent d7eaed4 commit eb6f410

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

packages/project/lib/build/cache/ProjectBuildCache.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ export default class ProjectBuildCache {
365365

366366
await this.#cacheManager.writeBuildManifest(
367367
this.#project, this.#buildSignature, buildManifest);
368+
369+
// Import cached stages back into project to prevent inconsistent state during next build/save
370+
await this.#importCachedStages(buildManifest.cache.stages);
368371
}
369372

370373
#getStageNameForTask(taskName) {
@@ -392,7 +395,6 @@ export default class ProjectBuildCache {
392395
}));
393396
return [stageId, resourceMetadata];
394397
}));
395-
// Optional TODO: Re-import cache as base layer to reduce memory pressure?
396398
}
397399

398400
async #checkForIndexChanges(index, indexTimestamp) {

packages/project/lib/specifications/Project.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,16 @@ class Project extends Specification {
381381
this.#currentStageWorkspace = null;
382382
}
383383

384+
_resetStages() {
385+
this.#stages = [];
386+
this.#currentStage = null;
387+
this.#currentStageName = "<source>";
388+
this.#currentStageReadIndex = -1;
389+
this.#currentStageReaders = new Map();
390+
this.#currentStageWorkspace = null;
391+
this.#workspaceVersion = 0;
392+
}
393+
384394
#getReaderForStage(stage, style = "buildtime", includeCache = true) {
385395
const writers = stage.getAllWriters(includeCache);
386396
const readers = [];
@@ -406,23 +416,7 @@ class Project extends Specification {
406416
}
407417

408418
setStages(stageIds, cacheReaders) {
409-
if (this.#stages.length > 0) {
410-
// Stages have already been set. Compare existing stages with new ones and throw on mismatch
411-
for (let i = 0; i < stageIds.length; i++) {
412-
const stageId = stageIds[i];
413-
if (this.#stages[i].getId() !== stageId) {
414-
throw new Error(
415-
`Unable to set stages for project ${this.getName()}: Stage mismatch at position ${i} ` +
416-
`(existing: ${this.#stages[i].getId()}, new: ${stageId})`);
417-
}
418-
}
419-
if (cacheReaders?.length) {
420-
throw new Error(
421-
`Unable to set stages for project ${this.getName()}: Cache readers can only be set ` +
422-
`when stages are created for the first time`);
423-
}
424-
return; // Stages already set and matching, no further processing needed
425-
}
419+
this._resetStages(); // Reset current stages and metadata
426420
for (let i = 0; i < stageIds.length; i++) {
427421
const stageId = stageIds[i];
428422
const newStage = new Stage(stageId, cacheReaders?.[i]);

0 commit comments

Comments
 (0)