Skip to content

feat(project): Track external tool dependencies in the build cache - #1706

Open
devtomtom wants to merge 6 commits into
mainfrom
feat/build-cache-tools-dependencies
Open

devtomtom wants to merge 6 commits into
mainfrom
feat/build-cache-tools-dependencies

Conversation

@devtomtom

@devtomtom devtomtom commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

JIRA: CPOUI5FOUNDATION-1386
JIRA: CPOUI5FOUNDATION-1396

Summary

A build task's output can depend on the version of an external tool it loads to
transform content. That version is an input to the build, but the cache never tracked
it: upgrading a tool without a new @ui5/builder release left every signature
unchanged, so the build served a cache hit with stale output.

This change makes the cache aware of the tools a task loads. A main-thread
module-resolution hook observes which tool files each stage resolves while it runs, and
their content is hashed into a new tool-content component of the stage signature. No
per-task declaration and no version strings.

…cache tracking

minify (terser) and buildThemes (less-openui5) run their tool in a workerpool
thread, and jsdoc runs in a child process. Those realms are invisible to the build
cache's main-thread resolve hook, so each dispatch resolves the tool's entry on the
main thread inside the task's capture window. The recorder records the entry and
folds in its package.json, so a tool version bump moves the stage signature.

The minifier keeps terser as a static top-level import in minifierWorker.js, reached
by a dynamic import on both the worker and in-process paths. runTask also rejects a
step that mixes a scalar shape with keys/each.
Observe which external tool files a build task loads during its stage (via a
main-thread module-resolution hook) and content-hash them into a dedicated
tool-content component of the stage signature. A tool upgrade with no new
@ui5/builder release now invalidates the affected stage instead of serving
stale output. Includes the design documentation for the mechanism.
The round-trip test called the async recordToolFiles without awaiting
it, then read getToolContentSignature() before computeToolContent had
run. Both the captured signature and the restored one held the empty-set
digest, so the assertion passed without exercising the round-trip. A
regression that always returned the empty digest would go unnoticed.

Make the test async and await the call so the signature reflects the
recorded files.
… boundary

The recorder's correctness rests on two preconditions that were documented
but untested, so a regression would have been silent:

- Capture re-fire: a cold require is captured, a warm require of an
  already-cached CJS module is not, and a dynamic import() re-fires even when
  cached. This is why a tool a capture window must observe has to load for the
  first time inside it, or be reached via import().
- Serial execution (nesting): captures nest strictly (inner opens and closes
  within outer), so a resolution attributes to whichever window was open when
  it fired. The shipped executor runs tasks and projects serially, so windows
  only ever nest; this pins that contract so a future parallelization breaks
  loudly instead of misattributing.

Also add an integration test for the NOTES.md 2.6 limitation: a tool loaded
only inside a worker the task spawns itself is not tracked, so a bump of it
serves stale output (asserted at the output level: the built resource keeps
the old stub-tool@1 marker). This is a boundary test of current, fail-safe
behavior, not a bug.

New fixtures: cjs-refire-tool, esm-refire-tool, nested-inner-tool (recorder
unit tests), and task.tool-content-worker.js + tool-content-worker.js +
ui5-customTask-toolContentWorker.yaml (the worker-only task).
NOTES.md was a working design document for the tool-dependency tracking
feature, not a shipped artifact. Remove it and drop the five code comments
that cited its section numbers, rephrasing them to state the limitation
directly without pointing at a file that no longer exists.
@devtomtom
devtomtom requested a review from a team November 23, 2026 08:59
…oss suite order

The test matched captured tool paths with forward-slash literals, but the
recorder returns OS-native separators (fileURLToPath), so the assertions failed
on Windows CI. Use path.join / path.sep instead.

Also resolve a dedicated fixture task module whose top-level tool import nothing
else loads, so the capture no longer depends on a real task module being loaded
cold (the resolve hook fires only on a module's first resolution per process).
@devtomtom
devtomtom force-pushed the feat/build-cache-tools-dependencies branch from 8092bb5 to db0c031 Compare November 23, 2026 09:37
@@ -1,5 +1,6 @@
import {getLogger} from "@ui5/logger";
import crypto from "node:crypto";
import fs from "node:fs";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use here "node:fs/promises" and omit the fs.promises redundancies in the code

Comment on lines +70 to +83
async function statToolFiles(paths) {
const stats = Object.create(null);
const sorted = [...paths].sort();
const width = Math.min(STAT_CONCURRENCY, sorted.length);
let next = 0;
async function worker() {
while (next < sorted.length) {
const filePath = sorted[next++];
stats[filePath] = await statToolFile(filePath);
}
}
await Promise.all(Array.from({length: width}, worker));
return stats;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this method is quite smart, I find it very hard to read.
That asynchronousity of shared resources seems quite inconvenient to read. Wouldn't it be easier to simply split the sorted into STAT_CONCURRENCY chunks and run those chunks as arguments of i.e. worker function within promise all. It's basically the same, but more readable

// HashTree's isResourceUnchanged signals, a size, inode, or mtime difference falls to the content tier. The
// inode check is the soundness anchor: every realistic tool change (npm install/ci, patch-package, a
// relinked file:) replaces the file and moves its inode, so it re-hashes; mtime catches an in-place edit.
function toolStatsUnchanged(current, baseline) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the name and method's purpose, but double negation is something hard to grasp Unchanged + false value => the source actually changed 😄

for (const filePath of currentPaths) {
const now = current[filePath];
const was = baseline[filePath];
if (!now || !was || now[0] !== was[0] || now[1] !== was[1] || now[2] !== was[2]) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have some deep equal utility for those?

return;
}
const merged = [...new Set([...this.#toolPaths, ...captured])].sort();
const grew = merged.join("\0") !== this.#toolPaths.join("\0");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be completelly correct and to me it seems like it can potentially lead to unexpected output if future developemnt includes more assignments to #toolPaths.

Let me explain what are my toughts here:
#toolPaths is defined in the constructor as an array, therefore it can potentially hold unordered list and duplicate values.
While merged is sorted out and has only unique values (the definition of Set).
So, now we have assignment only on row #429, but in the future we might do some changes that might lead to the behaviour I described.

A simple solution would be simply to wrap #toolPaths here in a Set

Comment on lines +447 to +450
const {signature, stats} = await computeToolContent(this.#toolPaths);
this.#toolContentSignature = signature;
this.#toolStats = stats;
this.#toolSetModified = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as in the grew if statement. Can we optimise and maybe do something like that:

if (!grew && toolStatsUnchanged....){ ... } else { /* calculate the new signitures */ }

* @returns {Promise<void>}
*/
async refreshToolContentSignature() {
if (this.#toolPaths.length === 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this.#toolPaths.length === 0) {
if ( !this.hasToolContent() ) {

hookInstalled = true;
module.registerHooks({
resolve(specifier, context, nextResolve) {
const result = nextResolve(specifier, context);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context provides a parentURL, which would allow to track where the resolution was triggered from. Either I miss something in the code or this information is currently not taken into account.

Given that tasks run async, and we do not have full control over other imports / resolutions that are triggered from other modules (either ours or from the code that runs our APIs), wouldn't it be essential to take the context information into account to be able to associate the resolutions to the task and ignore other resolutions that happened outside of it?

I didn't fully check the details, so this might come with other problems, but I just asked myself this question.

I did also not check whether it is stated somewhere why the context is not used. If this is already stated somewhere (sorry for not reading it!), please also add the information to this code here, so it can be taken into account when looking at the hook implementation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

⌂ Proxy