Skip to content

3D engine that AI agents drive

Keep the old pack output when a build fails and refuse unknown flags (#2562, #2563)

Nguyen Xuan An0aa1a964c046
diff --git a/changes/2562.md b/changes/2562.md
new file mode 100644
index 00000000..25239e58
--- /dev/null
+++ b/changes/2562.md
@@ -0,0 +1 @@
+- `build-pack-index` keeps the previous output folder when a build fails, and refuses a misspelled or repeated flag (#2562, #2563)
diff --git a/scripts/build-pack-index.mjs b/scripts/build-pack-index.mjs
index bb12e053..26dbb572 100644
--- a/scripts/build-pack-index.mjs
+++ b/scripts/build-pack-index.mjs
@@ -4,7 +4,7 @@
// Usage: node scripts/build-pack-index.mjs [--packs <folder>] [--out <folder>]
import { createHash } from 'node:crypto';
import { existsSync } from 'node:fs';
-import { copyFile, mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises';
+import { copyFile, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises';
import { extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
import { fileURLToPath } from 'node:url';
import { crc32, deflateRawSync } from 'node:zlib';
@@ -165,8 +165,22 @@ export async function buildPackIndex({ packsDirectory = join(repositoryRoot, 'pa
if (entry.isDirectory() || entry.isSymbolicLink()) folders.push(entry.name);
}
folders.sort();
- await rm(output, { recursive: true, force: true });
- await mkdir(output, { recursive: true });
+ const staging = `${output}.building-${process.pid}`;
+ await rm(staging, { recursive: true, force: true });
+ await mkdir(staging, { recursive: true });
+ try {
+ const packs = await buildIntoFolder(source, folders, staging);
+ await rm(output, { recursive: true, force: true });
+ await rename(staging, output);
+ return packs;
+ } catch (error) {
+ await rm(staging, { recursive: true, force: true });
+ throw error;
+ }
+}
+
+/** Builds into a staging folder, so a failed build never empties or half fills the real output folder. */
+async function buildIntoFolder(source, folders, output) {
const packs = [];
const failures = [];
for (const name of folders) {
@@ -188,17 +202,25 @@ export async function buildPackIndex({ packsDirectory = join(repositoryRoot, 'pa
return packs;
}
-function optionValue(name) {
- const position = process.argv.indexOf(name);
- if (position === -1) return undefined;
- const value = process.argv[position + 1];
- if (value === undefined || value.startsWith('--')) throw new Error(`${name} needs a folder after it, such as ${name} public/packs.`);
- return value;
+const USAGE = 'Usage: node scripts/build-pack-index.mjs [--packs <folder>] [--out <folder>]';
+
+/** Reads `--packs` and `--out`, each at most once with a folder after it; anything else is a usage error. */
+export function parseBuildArguments(argumentList) {
+ const options = {};
+ for (let position = 0; position < argumentList.length; position += 2) {
+ const flag = argumentList[position];
+ const value = argumentList[position + 1];
+ const key = { '--packs': 'packsDirectory', '--out': 'outputDirectory' }[flag];
+ if (!key || key in options) throw new Error(`${flag} is not a flag to pass here, or is repeated. ${USAGE}`);
+ if (value === undefined || value.startsWith('--')) throw new Error(`${flag} needs a folder after it, such as ${flag} public/packs. ${USAGE}`);
+ options[key] = value;
+ }
+ return options;
}
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
try {
- const packs = await buildPackIndex({ packsDirectory: optionValue('--packs'), outputDirectory: optionValue('--out') });
+ const packs = await buildPackIndex(parseBuildArguments(process.argv.slice(2)));
for (const pack of packs) console.log(`pack ${pack.name} ${pack.version} ${pack.bytes} bytes sha256 ${pack.sha256.slice(0, 12)}`);
if (packs.length === 0) console.log('no packs found');
} catch (error) {
diff --git a/tests/build-pack-index.test.js b/tests/build-pack-index.test.js
index 00b4bb5a..eddd62ee 100644
--- a/tests/build-pack-index.test.js
+++ b/tests/build-pack-index.test.js
@@ -1,9 +1,11 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
-import { cpSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
+import { spawnSync } from 'node:child_process';
+import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
import { inflateRawSync } from 'node:zlib';
import { buildPackIndex, createZip } from '../scripts/build-pack-index.mjs';
@@ -37,7 +39,30 @@ test('an invalid pack stops the build with its own error', async (context) => {
const options = packsFolderWithExample(context);
writeFileSync(join(options.packsDirectory, 'example-starter/run.js'), 'console.log(1)');
await assert.rejects(buildPackIndex(options), /Refusing to publish 1 invalid pack[\s\S]*example-starter[\s\S]*run\.js/);
- assert.ok(!readdirSync(options.outputDirectory).includes('index.json'));
+ assert.ok(!existsSync(join(options.outputDirectory, 'index.json')));
+});
+
+test('a failed build leaves the previous output folder untouched', async (context) => {
+ const options = packsFolderWithExample(context);
+ mkdirSync(options.outputDirectory);
+ writeFileSync(join(options.outputDirectory, 'index.json'), 'previous index');
+ writeFileSync(join(options.packsDirectory, 'example-starter/run.js'), 'console.log(1)');
+ await assert.rejects(buildPackIndex(options), /Refusing to publish/);
+ assert.deepEqual(readdirSync(options.outputDirectory), ['index.json']);
+ assert.equal(readFileSync(join(options.outputDirectory, 'index.json'), 'utf8'), 'previous index');
+ assert.deepEqual(readdirSync(join(options.outputDirectory, '..')).sort(), ['out', 'packs']);
+});
+
+const BUILD_SCRIPT = fileURLToPath(new URL('../scripts/build-pack-index.mjs', import.meta.url));
+
+test('an unknown or repeated flag stops the build with a usage line instead of being ignored', (context) => {
+ for (const extraArguments of [['--pack', 'elsewhere'], ['--out', 'again'], ['stray']]) {
+ const options = packsFolderWithExample(context);
+ const result = spawnSync(process.execPath, [BUILD_SCRIPT, '--packs', options.packsDirectory, '--out', options.outputDirectory, ...extraArguments], { encoding: 'utf8' });
+ assert.equal(result.status, 1, extraArguments.join(' '));
+ assert.match(result.stderr, /Usage: node scripts\/build-pack-index\.mjs/);
+ assert.ok(!existsSync(options.outputDirectory), extraArguments.join(' '));
+ }
});
test('two builds give identical bytes', async (context) => {