Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(cli): issues() reads Core templates only for integration templates (#10008)

Christopher Chedeaue657e2e6a470
diff --git a/.changeset/cli-issues-templates-gate.md b/.changeset/cli-issues-templates-gate.md
new file mode 100644
index 0000000000..da3c86a784
--- /dev/null
+++ b/.changeset/cli-issues-templates-gate.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[perf] When no loaded integration ships templates, the integration-issue check no longer imports every Core template doc. `astryx doctor` in a project whose integrations only ship icon sets now imports none of them.
+
+@vjeux
diff --git a/packages/cli/foundation/config/project.issues.test.mjs b/packages/cli/foundation/config/project.issues.test.mjs
index 3f40c78c36..46b48bba3b 100644
--- a/packages/cli/foundation/config/project.issues.test.mjs
+++ b/packages/cli/foundation/config/project.issues.test.mjs
@@ -2,10 +2,11 @@
/**
* @file project.issues.test.mjs
- * @input A consumer project with no integration, and one with an integration
- * whose template has no source, each under a repo-local temp dir
- * @output Proves that Project.issues() reads no template doc when no
- * integration is loaded, and still finds an integration's template issue
+ * @input Consumer projects under a repo-local temp dir: one with no
+ * integration, one whose integration has no templates, and ones whose
+ * integration's templates are broken or replace a missing Core template
+ * @output Proves that Project.issues() reads no template doc unless an
+ * integration has templates, and still finds that integration's issues
* @position Beside project.test.mjs, in its own file because it replaces
* the doc compiler's import module to count template doc imports
*/
@@ -58,6 +59,29 @@ afterEach(() => {
fs.rmSync(tmpDir, {recursive: true, force: true});
});
+/**
+ * Installs `@acme/widgets` with `manifest` and names it in the config.
+ * @param {object} manifest
+ * @returns {string} the package folder
+ */
+function installIntegration(manifest) {
+ fs.writeFileSync(
+ path.join(tmpDir, 'astryx.config.mjs'),
+ `export default ${JSON.stringify({integrations: ['@acme/widgets']})};\n`,
+ );
+ const pkgDir = path.join(tmpDir, 'node_modules', '@acme', 'widgets');
+ fs.mkdirSync(pkgDir, {recursive: true});
+ fs.writeFileSync(
+ path.join(pkgDir, 'package.json'),
+ JSON.stringify({name: '@acme/widgets', version: '1.0.0'}),
+ );
+ fs.writeFileSync(
+ path.join(pkgDir, 'astryx.integration.mjs'),
+ `export default ${JSON.stringify(manifest)};\n`,
+ );
+ return pkgDir;
+}
+
describe('Project.issues()', () => {
it('reads no template doc for a project with no integration', async () => {
const project = await Project.load(tmpDir);
@@ -66,6 +90,40 @@ describe('Project.issues()', () => {
expect(templateImports.count).toBe(0);
});
+ it('reads no template doc when no loaded integration has templates', async () => {
+ const pkgDir = installIntegration({themes: './themes'});
+ fs.mkdirSync(path.join(pkgDir, 'themes'));
+ const project = await Project.load(tmpDir);
+ expect(project.loadedIntegrations).toHaveLength(1);
+ expect(await project.issues()).toEqual([]);
+ expect(templateImports.count).toBe(0);
+ });
+
+ it("still finds an integration's replacement of a missing Core template", async () => {
+ const pkgDir = installIntegration({templates: './templates'});
+ fs.mkdirSync(path.join(pkgDir, 'templates', 'pages', 'acme-shell'), {
+ recursive: true,
+ });
+ fs.writeFileSync(
+ path.join(pkgDir, 'templates', 'pages', 'acme-shell', 'template.doc.mjs'),
+ `export default {type: 'page', name: 'Acme shell', description: 'A shell', replaces: 'no-such-core-page'};\n`,
+ );
+ fs.writeFileSync(
+ path.join(pkgDir, 'templates', 'pages', 'acme-shell', 'template.tsx'),
+ 'export default function Template() { return null; }\n',
+ );
+ const project = await Project.load(tmpDir);
+ expect(await project.issues()).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ package: '@acme/widgets',
+ code: 'missing_template_replacement_target',
+ }),
+ ]),
+ );
+ expect(templateImports.count).toBeGreaterThan(0);
+ });
+
it("still finds an integration's template issue when nothing discovered templates first", async () => {
fs.writeFileSync(
path.join(tmpDir, 'astryx.config.mjs'),
diff --git a/packages/cli/foundation/config/project.mjs b/packages/cli/foundation/config/project.mjs
index 1745945b66..f4cd2be273 100644
--- a/packages/cli/foundation/config/project.mjs
+++ b/packages/cli/foundation/config/project.mjs
@@ -383,6 +383,15 @@ export function providerLedgerOf(project) {
return PROVIDER_LEDGERS.get(project) ?? new Map();
}
+/**
+ * True when an integration's templates root exists, the only place its
+ * templates come from (discoverIntegrationTemplatesForOne).
+ * @param {any} integration
+ * @returns {boolean}
+ */
+const hasTemplatesRoot = integration =>
+ integration?.templates != null && fs.existsSync(integration.templates);
+
/**
* The single API for reading resolved project configuration. Construct via the
* async {@link Project.load} factory.
@@ -1265,8 +1274,12 @@ export class Project {
if (this.#loadedIntegrations.length > 0) {
// Template replacement validity depends on the combined Core +
// integration catalog. Resolve it first so issue results never depend
- // on which discovery method the caller happened to invoke earlier.
- await this.templates();
+ // on which discovery method the caller happened to invoke earlier. Only
+ // an integration's own templates can replace one, so with none, Core's
+ // templates stay unread.
+ if (this.#loadedIntegrations.some(hasTemplatesRoot)) {
+ await this.templates();
+ }
// Component replacement validity likewise depends on Core plus every
// integration's components.
await this.componentReplacements();