An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): theme build scans the icon import from the source it loaded (#8598)
Christopher Chedeau68507bbe5356
diff --git a/.changeset/theme-build-single-read.md b/.changeset/theme-build-single-read.mdnew file mode 100644index 0000000000..28d1d5ee32--- /dev/null+++ b/.changeset/theme-build-single-read.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx theme build` reads the theme's icon import from the source it loaded: a theme saved in place while it builds now builds with its import, or fails and builds on the next save, instead of producing a module without the import.++@vjeuxdiff --git a/packages/cli/api/theme/build/build.mjs b/packages/cli/api/theme/build/build.mjsindex 70afeeb19c..0c034d7a3b 100644--- a/packages/cli/api/theme/build/build.mjs+++ b/packages/cli/api/theme/build/build.mjs@@ -2844,12 +2844,29 @@ async function themeBuildInternal(let themeExportName;/** Paths through the load that interception could not fully observe. */let loadDegradation;+ /**+ * The theme file's bytes as loaded: read before and after the load, and the+ * registry scan below reads these rather than the file again. An editor's+ * in-place save truncates before it writes, so a later read can find an+ * empty file the load never saw; a file that changed while it loaded is not+ * built from.+ * @type {string}+ */+ let themeSource;try {+ const before = fs.readFileSync(filePath, 'utf8');const loaded = await extractThemeDefinition(filePath,interception,options.__familyLoader,);+ const after = fs.readFileSync(filePath, 'utf8');+ if (before !== after || before === '') {+ throw new Error(+ `${filePath} changed while it was loading. Build again once it is saved.`,+ );+ }+ themeSource = before;themeDef = loaded.theme;themeExportName = loaded.exportName;loadDegradation = loaded.degraded;@@ -2939,7 +2956,10 @@ async function themeBuildInternal(: []),];const iconResolution = {- readSource: (/** @type {string} */ file) => fs.readFileSync(file, 'utf8'),+ readSource: (/** @type {string} */ file) =>+ path.resolve(file) === path.resolve(filePath)+ ? themeSource+ : fs.readFileSync(file, 'utf8'),resolveModule: resolveThemeModule,reservedNames: reservedNames(functionEntries),functionEntries,diff --git a/packages/cli/clients/cli/commands/build-theme.torn-read.test.mjs b/packages/cli/clients/cli/commands/build-theme.torn-read.test.mjsnew file mode 100644index 0000000000..4f35fb90dc--- /dev/null+++ b/packages/cli/clients/cli/commands/build-theme.torn-read.test.mjs@@ -0,0 +1,151 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file A theme saved in place while `astryx theme build` loads it.+ *+ * An editor's in-place save truncates the theme file before it writes, so a+ * read that lands in between finds it empty. The build loads the theme, then+ * scans its source for the icon registry import; with a registry that holds no+ * icons yet, a scan that read the empty file found no import and the build+ * succeeded without one. The build now scans the bytes it loaded, and a file+ * that changed while it loaded fails the build rather than build from a file+ * the load never saw.+ *+ * Each case runs the CLI with a preload that empties every read of the theme+ * file once the theme module has run: the moment right after the load, as an+ * in-place save landing then would.+ */++import {describe, it, expect, beforeAll, beforeEach, afterEach} from 'vitest';+import {spawnSync} from 'node:child_process';+import * as fs from 'node:fs';+import * as path from 'node:path';+import * as os from 'node:os';+import {fileURLToPath} from 'node:url';+import {ensureCoreBuilt} from './ensure-core-built.mjs';++const __dirname = path.dirname(fileURLToPath(import.meta.url));+const CLI_BIN = path.resolve(__dirname, '../bin/astryx.mjs');++beforeAll(() => {+ ensureCoreBuilt();+}, 200_000);++let tmpDir;+beforeEach(() => {+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-torn-theme-'));+});+afterEach(() => {+ fs.rmSync(tmpDir, {recursive: true, force: true});+});++/** The emitted icon import, or null. */+function iconImportLine(generated) {+ const match = generated.match(/^import .*$/m);+ return match ? match[0] : null;+}++/**+ * A theme whose registry, still empty, arrives through a relative import. Its+ * module marks when it has run, so the preload knows the load is done.+ */+function writeTheme(dir) {+ fs.writeFileSync(+ path.join(dir, 'icons.mjs'),+ 'export const testIcons = {};\n',+ );+ const file = path.join(dir, 'saved.mjs');+ fs.writeFileSync(+ file,+ `import {testIcons} from './icons.mjs';\n` ++ `globalThis.__astryxThemeLoaded = true;\n` ++ `export default {\n` ++ ` name: "saved",\n` ++ ` icons: testIcons,\n` ++ ` tokens: {'--color-bg': '#0a0b0c'},\n` ++ `};\n`,+ );+ return file;+}++/** Empties every read of `themeFile` once the theme module has run. */+function writeTruncatingPreload(dir, themeFile) {+ const preload = path.join(dir, 'truncate-after-load.cjs');+ fs.writeFileSync(+ preload,+ `const fs = require('node:fs');\n` ++ `const read = fs.readFileSync;\n` ++ `fs.readFileSync = function (file, ...rest) {\n` ++ ` if (globalThis.__astryxThemeLoaded && String(file) === ${JSON.stringify(themeFile)}) {\n` ++ ` return typeof rest[0] === 'string' || rest[0]?.encoding ? '' : Buffer.alloc(0);\n` ++ ` }\n` ++ ` return read.call(this, file, ...rest);\n` ++ `};\n`,+ );+ return preload;+}++describe('theme build while the theme is saved in place', () => {+ it('builds with its import, or fails the build: never a module without it', () => {+ const themeFile = writeTheme(tmpDir);+ const preload = writeTruncatingPreload(tmpDir, themeFile);+ const run = spawnSync(+ process.execPath,+ [+ CLI_BIN,+ 'theme',+ 'build',+ 'saved.mjs',+ '--out',+ 'out/theme.css',+ '--icons-specifier',+ './icons.mjs',+ ],+ {+ cwd: tmpDir,+ encoding: 'utf8',+ env: {+ ...process.env,+ FORCE_COLOR: '0',+ NODE_OPTIONS:+ `${process.env.NODE_OPTIONS ?? ''} --require ${preload}`.trim(),+ },+ },+ );+ const builtFile = path.join(tmpDir, 'out', 'saved.js');+ if (run.status === 0) {+ expect(iconImportLine(fs.readFileSync(builtFile, 'utf8'))).toBe(+ 'import { testIcons } from "./icons.mjs";',+ );+ } else {+ expect(`${run.stdout}\n${run.stderr}`).toMatch(+ /changed while it was loading/,+ );+ expect(fs.existsSync(builtFile)).toBe(false);+ }+ });++ it('builds the import from a theme left alone', () => {+ writeTheme(tmpDir);+ const run = spawnSync(+ process.execPath,+ [+ CLI_BIN,+ 'theme',+ 'build',+ 'saved.mjs',+ '--out',+ 'out/theme.css',+ '--icons-specifier',+ './icons.mjs',+ ],+ {cwd: tmpDir, encoding: 'utf8', env: {...process.env, FORCE_COLOR: '0'}},+ );+ expect(run.status, run.stderr).toBe(0);+ expect(+ iconImportLine(+ fs.readFileSync(path.join(tmpDir, 'out', 'saved.js'), 'utf8'),+ ),+ ).toBe('import { testIcons } from "./icons.mjs";');+ });+});