Skip to content

Commit

Permalink
Fix build when previous build steps already exist (#1137)
Browse files Browse the repository at this point in the history
Before it runs a build step, the code loads the results of the previous step.
If the file exists, it also loads the previous results of the current step, for
use as fallback if some network issue occurs.

The loading was done using `require()`. The problem is that `require()`
maintains a cache of loaded modules, so if the previous results of the current
step existed, the code actually reused them for the *following* step as well.

This bug did not occur in production because the build job starts from scratch.
In particular, it does not have previous results of build steps.
  • Loading branch information
tidoust authored Nov 23, 2023
1 parent 05cb3f5 commit e1ec1e6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/build-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,18 @@ async function generateIndex(specs, { step = "all", previousIndex = null, log =
async function generateIndexFile(specsFile, targetFile, step) {
// If the index already exists, reuse the info it contains when info cannot
// be refreshed due to some external (network) issue.
const previousIndex = (function () {
const previousIndex = await (async function () {
try {
return require(path.resolve(targetFile));
const json = await fs.readFile(path.resolve(targetFile), 'utf8');
return JSON.parse(json);
}
catch (err) {
return [];
}
})();

const specs = require(path.resolve(specsFile));
const specsJson = await fs.readFile(path.resolve(specsFile));
const specs = JSON.parse(specsJson);
const index = await generateIndex(specs, { previousIndex, step });
console.log(`Write ${targetFile}...`);
await fs.writeFile(targetFile, JSON.stringify(index, null, 2));
Expand Down

0 comments on commit e1ec1e6

Please sign in to comment.