Skip to content

Commit 96c3a5f

Browse files
committed
refactor(project): Use cleanup hooks in update builds
1 parent 0409506 commit 96c3a5f

3 files changed

Lines changed: 61 additions & 29 deletions

File tree

packages/project/lib/build/ProjectBuilder.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ class ProjectBuilder {
181181
}
182182

183183
const projectBuildContexts = await this._createRequiredBuildContexts(requestedProjects);
184-
const cleanupSigHooks = this._registerCleanupSigHooks();
185184
let fsTarget;
186185
if (destPath) {
187186
fsTarget = resourceFactory.createAdapter({
@@ -191,7 +190,6 @@ class ProjectBuilder {
191190
}
192191

193192
const queue = [];
194-
const alreadyBuilt = [];
195193

196194
// Create build queue based on graph depth-first search to ensure correct build order
197195
await this._graph.traverseDepthFirst(async ({project}) => {
@@ -202,15 +200,38 @@ class ProjectBuilder {
202200
// => This project needs to be built or, in case it has already
203201
// been built, it's build result needs to be written out (if requested)
204202
queue.push(projectBuildContext);
205-
if (!await projectBuildContext.requiresBuild()) {
206-
alreadyBuilt.push(projectName);
207-
}
208203
}
209204
});
210205

206+
if (destPath && cleanDest) {
207+
this.#log.info(`Cleaning target directory...`);
208+
await rmrf(destPath);
209+
}
210+
211+
await this.#build(queue, projectBuildContexts, requestedProjects, fsTarget);
212+
213+
if (watch) {
214+
const relevantProjects = queue.map((projectBuildContext) => {
215+
return projectBuildContext.getProject();
216+
});
217+
return this._buildContext.initWatchHandler(relevantProjects, async () => {
218+
await this.#updateBuild(projectBuildContexts, requestedProjects, fsTarget);
219+
});
220+
}
221+
}
222+
223+
async #build(queue, projectBuildContexts, requestedProjects, fsTarget) {
211224
this.#log.setProjects(queue.map((projectBuildContext) => {
212225
return projectBuildContext.getProject().getName();
213226
}));
227+
228+
const alreadyBuilt = [];
229+
for (const projectBuildContext of queue) {
230+
if (!await projectBuildContext.requiresBuild()) {
231+
const projectName = projectBuildContext.getProject().getName();
232+
alreadyBuilt.push(projectName);
233+
}
234+
}
214235
if (queue.length > 1) { // Do not log if only the root project is being built
215236
this.#log.info(`Processing ${queue.length} projects`);
216237
if (alreadyBuilt.length) {
@@ -240,13 +261,9 @@ class ProjectBuilder {
240261
.join("\n ")}`);
241262
}
242263
}
243-
244-
if (destPath && cleanDest) {
245-
this.#log.info(`Cleaning target directory...`);
246-
await rmrf(destPath);
247-
}
248-
const startTime = process.hrtime();
264+
const cleanupSigHooks = this._registerCleanupSigHooks();
249265
try {
266+
const startTime = process.hrtime();
250267
const pWrites = [];
251268
for (const projectBuildContext of queue) {
252269
const project = projectBuildContext.getProject();
@@ -285,21 +302,26 @@ class ProjectBuilder {
285302
await Promise.all(pWrites);
286303
this.#log.info(`Build succeeded in ${this._getElapsedTime(startTime)}`);
287304
} catch (err) {
288-
this.#log.error(`Build failed in ${this._getElapsedTime(startTime)}`);
305+
this.#log.error(`Build failed`);
289306
throw err;
290307
} finally {
291308
this._deregisterCleanupSigHooks(cleanupSigHooks);
292309
await this._executeCleanupTasks();
293310
}
311+
}
294312

295-
if (watch) {
296-
const relevantProjects = queue.map((projectBuildContext) => {
297-
return projectBuildContext.getProject();
298-
});
299-
const watchHandler = this._buildContext.initWatchHandler(relevantProjects, async () => {
300-
await this.#update(projectBuildContexts, requestedProjects, fsTarget);
301-
});
302-
return watchHandler;
313+
async #updateBuild(projectBuildContexts, requestedProjects, fsTarget) {
314+
const cleanupSigHooks = this._registerCleanupSigHooks();
315+
try {
316+
const startTime = process.hrtime();
317+
await this.#update(projectBuildContexts, requestedProjects, fsTarget);
318+
this.#log.info(`Update succeeded in ${this._getElapsedTime(startTime)}`);
319+
} catch (err) {
320+
this.#log.error(`Update failed`);
321+
this.#log.error(err);
322+
} finally {
323+
this._deregisterCleanupSigHooks(cleanupSigHooks);
324+
await this._executeCleanupTasks();
303325
}
304326
}
305327

packages/project/lib/build/TaskRunner.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ class TaskRunner {
205205
}
206206
const usingCache = supportsDifferentialUpdates && cacheInfo;
207207

208-
this._log.info(
209-
`Executing task ${taskName} for project ${this._project.getName()}`);
208+
this._log.verbose(
209+
`Executing task ${taskName} for project ${this._project.getName()}` +
210+
(usingCache ? ` (using differential update)` : ""));
210211
const workspace = createMonitor(this._project.getWorkspace());
211212
const params = {
212213
workspace,
@@ -220,9 +221,6 @@ class TaskRunner {
220221
params.dependencies = dependencies;
221222
}
222223
if (usingCache) {
223-
this._log.info(
224-
`Using differential update for task ${taskName} of project ${this._project.getName()}`);
225-
// workspace =
226224
params.changedProjectResourcePaths = Array.from(cacheInfo.changedProjectResourcePaths);
227225
if (requiresDependencies) {
228226
params.changedDependencyResourcePaths = Array.from(cacheInfo.changedDependencyResourcePaths);

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ export default class CacheManager {
129129
// Cache miss
130130
return null;
131131
}
132-
throw err;
132+
throw new Error(`Failed to read build manifest for ` +
133+
`${projectId} / ${buildSignature}: ${err.message}`, {
134+
cause: err,
135+
});
133136
}
134137
}
135138

@@ -183,7 +186,10 @@ export default class CacheManager {
183186
// Cache miss
184187
return null;
185188
}
186-
throw err;
189+
throw new Error(`Failed to read resource index cache for ` +
190+
`${projectId} / ${buildSignature}: ${err.message}`, {
191+
cause: err,
192+
});
187193
}
188194
}
189195

@@ -243,7 +249,10 @@ export default class CacheManager {
243249
// Cache miss
244250
return null;
245251
}
246-
throw err;
252+
throw new Error(`Failed to read stage metadata from cache for ` +
253+
`${projectId} / ${buildSignature} / ${stageId} / ${stageSignature}: ${err.message}`, {
254+
cause: err,
255+
});
247256
}
248257
}
249258

@@ -302,7 +311,10 @@ export default class CacheManager {
302311
// Cache miss
303312
return null;
304313
}
305-
throw err;
314+
throw new Error(`Failed to read task metadata from cache for ` +
315+
`${projectId} / ${buildSignature} / ${taskName}: ${err.message}`, {
316+
cause: err,
317+
});
306318
}
307319
}
308320

0 commit comments

Comments
 (0)