Skip to content

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

TypeScript

perf(cli): discovery reads its listings; CLI tests warm up once (#9722)

Christopher Chedeauc40d382f9f14
diff --git a/.changeset/cli-discovery-listing-reads.md b/.changeset/cli-discovery-listing-reads.md
new file mode 100644
index 0000000000..30be490a2c
--- /dev/null
+++ b/.changeset/cli-discovery-listing-reads.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[perf] `integrationComponentConflicts()`, `integrationDocConflicts()` and `integrationCheck()` from `@astryxdesign/cli/api`, and `doctor integration components`, `doctor integration docs` and `integration check`, find Core's components from the folder listings they already read, with far fewer file system calls. Their results are unchanged.
+
+@vjeux
diff --git a/packages/cli/api/integration/authoring-checks.mjs b/packages/cli/api/integration/authoring-checks.mjs
index 12549a0e3a..0b45c690d5 100644
--- a/packages/cli/api/integration/authoring-checks.mjs
+++ b/packages/cli/api/integration/authoring-checks.mjs
@@ -27,6 +27,7 @@ import {
packageReferenceProblems,
} from '../docs/_adapter.mjs';
import {findProjectCoreDir} from '../../foundation/fs/paths.mjs';
+import {withListingCache} from '../../foundation/fs/listing-cache.mjs';
import {
discoverIntegrationComponents,
discoverOwnedComponents,
@@ -154,6 +155,16 @@ function addErrors(issues, errors, code) {
* @returns {Promise<import('./authoring-checks.type.mjs').IntegrationTemplateConflictResponse>}
*/
export async function integrationTemplateConflicts(pkg, options = {}) {
+ // Read-only: one listing of each folder serves the whole check.
+ return withListingCache(() => templateConflicts(pkg, options));
+}
+
+/**
+ * @param {string | undefined} pkg
+ * @param {import('./authoring-checks.type.mjs').IntegrationAuthoringOptions} options
+ * @returns {Promise<import('./authoring-checks.type.mjs').IntegrationTemplateConflictResponse>}
+ */
+async function templateConflicts(pkg, options) {
const {cwd = process.cwd()} = options;
const resolved = await resolveIntegration(pkg, cwd);
const validated = resolved.found;
@@ -291,6 +302,16 @@ export async function integrationTemplateConflicts(pkg, options = {}) {
* @returns {Promise<import('./authoring-checks.type.mjs').IntegrationComponentConflictResponse>}
*/
export async function integrationComponentConflicts(pkg, options = {}) {
+ // Read-only: one listing of each folder serves the whole check.
+ return withListingCache(() => componentConflicts(pkg, options));
+}
+
+/**
+ * @param {string | undefined} pkg
+ * @param {import('./authoring-checks.type.mjs').IntegrationAuthoringOptions} options
+ * @returns {Promise<import('./authoring-checks.type.mjs').IntegrationComponentConflictResponse>}
+ */
+async function componentConflicts(pkg, options) {
const {cwd = process.cwd()} = options;
const resolved = await resolveIntegration(pkg, cwd);
const validated = resolved.found;
@@ -387,6 +408,16 @@ export async function integrationComponentConflicts(pkg, options = {}) {
* @returns {Promise<import('./authoring-checks.type.mjs').IntegrationDocConflictResponse>}
*/
export async function integrationDocConflicts(pkg, options = {}) {
+ // Read-only: one listing of each folder serves the whole check.
+ return withListingCache(() => docConflicts(pkg, options));
+}
+
+/**
+ * @param {string | undefined} pkg
+ * @param {import('./authoring-checks.type.mjs').IntegrationAuthoringOptions} options
+ * @returns {Promise<import('./authoring-checks.type.mjs').IntegrationDocConflictResponse>}
+ */
+async function docConflicts(pkg, options) {
const {cwd = process.cwd()} = options;
const resolved = await resolveIntegration(pkg, cwd);
const validated = resolved.found;
diff --git a/packages/cli/api/integration/authoring-checks.test.mjs b/packages/cli/api/integration/authoring-checks.test.mjs
index 97d9750f5f..9cc16a00a9 100644
--- a/packages/cli/api/integration/authoring-checks.test.mjs
+++ b/packages/cli/api/integration/authoring-checks.test.mjs
@@ -30,6 +30,11 @@ import {
integrationTemplateConflicts,
shellArg,
} from './authoring-checks.mjs';
+import {warmIntegrationChecks} from '../../test-utils/warm-integration-checks.mjs';
+
+// The checks' one-time start in this file (TypeScript and its lib files) runs
+// here, while the file is collected, so no test carries it.
+await warmIntegrationChecks();
let tmpDir;
diff --git a/packages/cli/api/integration/check/check.mjs b/packages/cli/api/integration/check/check.mjs
index 7a8c2ae15c..d67d6908b3 100644
--- a/packages/cli/api/integration/check/check.mjs
+++ b/packages/cli/api/integration/check/check.mjs
@@ -36,6 +36,7 @@ import {
summarizeIssues,
validateIntegration,
} from '../validate-integration.mjs';
+import {withListingCache} from '../../../foundation/fs/listing-cache.mjs';
/** @typedef {import('../integration-check.type.mjs').IntegrationCheckName} IntegrationCheckName */
/** @typedef {import('../integration-check.type.mjs').IntegrationCheckReport} IntegrationCheckReport */
@@ -244,9 +245,13 @@ export async function integrationCheck(options = {}) {
/** @type {IntegrationCheckResponse[]} */
const responses = [];
responses.push(await validateIntegration(undefined, {cwd}));
- responses.push(await integrationComponentConflicts(undefined, {cwd}));
- responses.push(await integrationTemplateConflicts(undefined, {cwd}));
- responses.push(await integrationDocConflicts(undefined, {cwd}));
+ // The three authoring checks only read, so they share one listing of each
+ // folder.
+ await withListingCache(async () => {
+ responses.push(await integrationComponentConflicts(undefined, {cwd}));
+ responses.push(await integrationTemplateConflicts(undefined, {cwd}));
+ responses.push(await integrationDocConflicts(undefined, {cwd}));
+ });
const [validation] = responses;
return {
diff --git a/packages/cli/api/integration/example-checks.test.mjs b/packages/cli/api/integration/example-checks.test.mjs
index 7670f80e89..73737b12bb 100644
--- a/packages/cli/api/integration/example-checks.test.mjs
+++ b/packages/cli/api/integration/example-checks.test.mjs
@@ -9,6 +9,11 @@ import {integrationCheck} from './check/check.mjs';
import {outsideFiles} from '../../foundation/integrations/validate-contributions.mjs';
import {spawnSync} from 'node:child_process';
import {npmCommand} from '../../foundation/release/cache.mjs';
+import {warmIntegrationChecks} from '../../test-utils/warm-integration-checks.mjs';
+
+// The checks' one-time start in this file (TypeScript and its lib files) runs
+// here, while the file is collected, so no test carries it.
+await warmIntegrationChecks({inRepository: false});
/**
* Whether the npm the CLI starts packs `rel` from the test package. npm
diff --git a/packages/cli/api/integration/validate-integration-gaps.test.mjs b/packages/cli/api/integration/validate-integration-gaps.test.mjs
index 82c429879e..ee75032150 100644
--- a/packages/cli/api/integration/validate-integration-gaps.test.mjs
+++ b/packages/cli/api/integration/validate-integration-gaps.test.mjs
@@ -17,7 +17,6 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {parse} from '@babel/parser';
-import {spawnSync} from 'node:child_process';
import {
validateIntegration,
validateLocalIntegration,
@@ -29,20 +28,16 @@ import {
} from './check/check.mjs';
import {componentDocNamesCheck} from '../../foundation/integrations/validate-contributions.mjs';
import {formatCliCommand} from '../../foundation/env/package-manager.mjs';
+import {runCli} from '../../test-utils/run-cli.mjs';
+import {warmIntegrationChecks} from '../../test-utils/warm-integration-checks.mjs';
+
+// The checks' one-time start in this file (TypeScript and its lib files) runs
+// here, while the file is collected, so no test carries it.
+await warmIntegrationChecks();
let tmpDir;
let copies = 0;
-const CLI = path.join(
- process.cwd(),
- 'packages',
- 'cli',
- 'clients',
- 'cli',
- 'bin',
- 'astryx.mjs',
-);
-
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-validate-gaps-'));
// The fixture is a package with its own TypeScript config, scoped to its
@@ -322,18 +317,14 @@ describe('a name that differs from its file name', () => {
},
);
- it('prints the doc names it could not check', () => {
+ it('prints the doc names it could not check', async () => {
const dir = writeNamed({
[MANIFEST]: manifest({components: './components'}),
'components/AcmeWidget.doc.mjs': namedDoc('AcmeWidget', 'AcmeWidgets'),
'components/AcmeWidget.tsx': "export {AcmeWidgets} from '@acme/ui';\n",
});
- const run = spawnSync(
- process.execPath,
- [CLI, 'doctor', 'integration', 'validate'],
- {cwd: dir, encoding: 'utf-8', timeout: 60_000},
- );
+ const run = await runCli(['doctor', 'integration', 'validate'], dir);
expect(run.status, run.stderr).toBe(0);
expect(run.stdout).toContain(
'[info] Component names not checked: 1 doc(s) whose source exports this check cannot read:',
@@ -654,7 +645,7 @@ describe('block component names', () => {
]);
});
- it('prints the block names it could not judge', () => {
+ it('prints the block names it could not judge', async () => {
const dir = writeKit({
[MANIFEST]: manifest({templates: './templates'}),
'templates/account-card.doc.mjs': block(" componentsUsed: ['Panel']"),
@@ -662,11 +653,7 @@ describe('block component names', () => {
"import * as UI from '@acme/kit';\nexport default function A() {\n return <UI.Panel />;\n}\n",
});
- const run = spawnSync(
- process.execPath,
- [CLI, 'doctor', 'integration', 'validate'],
- {cwd: dir, encoding: 'utf-8', timeout: 60_000},
- );
+ const run = await runCli(['doctor', 'integration', 'validate'], dir);
expect(run.status, run.stderr).toBe(0);
expect(run.stdout).toContain(
'[info] Block component names not checked: 1 name(s) this check could not judge:',
@@ -1934,7 +1921,7 @@ describe('layout findings 0.7.2 did not fail on', () => {
},
);
- it('prints the notes and counts none of them', () => {
+ it('prints the notes and counts none of them', async () => {
const dir = writePackage({
[MANIFEST]: manifest({components: './components'}),
'components/AcmeWidget.doc.mjs': componentDoc('AcmeWidget'),
@@ -1942,11 +1929,7 @@ describe('layout findings 0.7.2 did not fail on', () => {
'components/AcmeWidget.tsx': componentSource('AcmeWidget'),
});
- const run = spawnSync(
- process.execPath,
- [CLI, 'doctor', 'integration', 'validate'],
- {cwd: dir, encoding: 'utf-8', timeout: 60_000},
- );
+ const run = await runCli(['doctor', 'integration', 'validate'], dir);
expect(run.status, run.stderr).toBe(0);
expect(run.stdout).toContain(
'note (duplicate_component_doc): [next minor] Component "AcmeWidget" has 2 docs:',
@@ -1991,13 +1974,9 @@ describe('name findings listed until the next minor release', () => {
},
);
- it('prints the name notes and counts none', () => {
+ it('prints the name notes and counts none', async () => {
const dir = blockLayout();
- const run = spawnSync(
- process.execPath,
- [CLI, 'doctor', 'integration', 'validate'],
- {cwd: dir, encoding: 'utf-8', timeout: 60_000},
- );
+ const run = await runCli(['doctor', 'integration', 'validate'], dir);
expect(run.status, run.stderr).toBe(0);
expect(run.stdout).toContain(
'note (unknown_block_component): [next minor] Template "account-card" names "AcmeGizmo" in componentsUsed',
diff --git a/packages/cli/api/integration/validate-integration.test.mjs b/packages/cli/api/integration/validate-integration.test.mjs
index 9434840f95..d2e662ff69 100644
--- a/packages/cli/api/integration/validate-integration.test.mjs
+++ b/packages/cli/api/integration/validate-integration.test.mjs
@@ -23,6 +23,11 @@ import {
integrationDocConflicts,
integrationTemplateConflicts,
} from './authoring-checks.mjs';
+import {warmIntegrationChecks} from '../../test-utils/warm-integration-checks.mjs';
+
+// The checks' one-time start in this file (TypeScript and its lib files) runs
+// here, while the file is collected, so no test carries it.
+await warmIntegrationChecks();
let tmpDir;
@@ -660,17 +665,33 @@ describe('integration diagnostics are read-only', () => {
);
}
- it('leaves local and installed packages byte-for-byte unchanged', async () => {
+ it('leaves the local package byte-for-byte unchanged', async () => {
const pkgDir = path.join(tmpDir, 'pkg');
// A TypeScript config scoped to the fixture's own files, beside the
- // packages: with none, the local package gets the repository's root
- // config, as `tsc` reads it, and the props comparison compiles every file
- // in the repository. The installed copy, under `node_modules`, reads none.
+ // package. Without it the package gets the repository's root config, as
+ // `tsc` reads it, and the props comparison compiles the whole repository.
fs.writeFileSync(
path.join(tmpDir, 'tsconfig.json'),
`${JSON.stringify({extends: '../tsconfig.json', include: ['**/*']})}\n`,
);
writeDiagnosedPackage(pkgDir);
+ const before = snapshot(pkgDir);
+
+ const local = await validateIntegration(undefined, {cwd: pkgDir});
+ await integrationTemplateConflicts(undefined, {cwd: pkgDir});
+ await integrationComponentConflicts(undefined, {cwd: pkgDir});
+ await integrationDocConflicts(undefined, {cwd: pkgDir});
+
+ expect(summarizeIssues(local.data.issues).errors).toBeGreaterThan(0);
+ expect(byCode(local.data.issues, 'unreachable_contribution')).toHaveLength(
+ 1,
+ );
+ expect(snapshot(pkgDir)).toEqual(before);
+ });
+
+ // Its own test: all 8 calls in one test take longer than the test limit on
+ // a busy machine.
+ it('leaves the installed package byte-for-byte unchanged', async () => {
const consumer = path.join(tmpDir, 'consumer');
fs.mkdirSync(consumer, {recursive: true});
fs.writeFileSync(
@@ -680,29 +701,17 @@ describe('integration diagnostics are read-only', () => {
writeDiagnosedPackage(
path.join(consumer, 'node_modules', '@acme', 'widgets'),
);
- const before = {pkg: snapshot(pkgDir), consumer: snapshot(consumer)};
+ const before = snapshot(consumer);
- const local = await validateIntegration(undefined, {cwd: pkgDir});
const installed = await validateIntegration('@acme/widgets', {
cwd: consumer,
});
- for (const [pkg, cwd] of [
- [undefined, pkgDir],
- ['@acme/widgets', consumer],
- ]) {
- await integrationTemplateConflicts(pkg, {cwd});
- await integrationComponentConflicts(pkg, {cwd});
- await integrationDocConflicts(pkg, {cwd});
- }
+ await integrationTemplateConflicts('@acme/widgets', {cwd: consumer});
+ await integrationComponentConflicts('@acme/widgets', {cwd: consumer});
+ await integrationDocConflicts('@acme/widgets', {cwd: consumer});
- expect(summarizeIssues(local.data.issues).errors).toBeGreaterThan(0);
- expect(byCode(local.data.issues, 'unreachable_contribution')).toHaveLength(
- 1,
- );
expect(summarizeIssues(installed.data.issues).errors).toBeGreaterThan(0);
- expect({pkg: snapshot(pkgDir), consumer: snapshot(consumer)}).toEqual(
- before,
- );
+ expect(snapshot(consumer)).toEqual(before);
});
});
diff --git a/packages/cli/clients/cli/commands/doctor-integration.test.mjs b/packages/cli/clients/cli/commands/doctor-integration.test.mjs
index d41962d99c..ae30a9efff 100644
--- a/packages/cli/clients/cli/commands/doctor-integration.test.mjs
+++ b/packages/cli/clients/cli/commands/doctor-integration.test.mjs
@@ -25,6 +25,11 @@ import {
} from '../../../foundation/fs/paths.mjs';
import {printIntegrationCheckResponse, registerDoctor} from './doctor.mjs';
import {expectRecordsCarry} from '../../../test-utils/text-projection.mjs';
+import {warmIntegrationChecks} from '../../../test-utils/warm-integration-checks.mjs';
+
+// The checks' one-time start in this file (TypeScript and its lib files) runs
+// here, while the file is collected, so no test carries it.
+await warmIntegrationChecks();
let tmpDir;
let previousCwd;
diff --git a/packages/cli/foundation/discovery/component-discovery.mjs b/packages/cli/foundation/discovery/component-discovery.mjs
index c1e6ff3f5b..871efe1fe4 100644
--- a/packages/cli/foundation/discovery/component-discovery.mjs
+++ b/packages/cli/foundation/discovery/component-discovery.mjs
@@ -8,7 +8,12 @@ import * as fs from 'node:fs';
import {createRequire} from 'node:module';
import * as path from 'node:path';
import {DOC_EXPORTS} from '../doc-compiler/read.mjs';
-import {listDir, listDirEntries, pathExists} from '../fs/listing-cache.mjs';
+import {
+ listDir,
+ listDirEntries,
+ pathExists,
+ withListingCache,
+} from '../fs/listing-cache.mjs';
import {existsCaseExact} from '../fs/paths.mjs';
import {CORE_PROVIDER_ID} from '../identity/providers.mjs';
import {loadComponentDoc} from './component-loader.mjs';
@@ -595,20 +600,25 @@ export function findComponentSource(coreDir, name) {
* @param {string} dirPath
* @returns {string | null}
*/
- function searchDir(dirPath) {
- if (!pathExists(dirPath)) return null;
+ function searchDir(dirPath, listed = false) {
+ // A folder from its parent's listing exists; only a starting folder
+ // needs the check.
+ if (!listed && !pathExists(dirPath)) return null;
const entries = listDirEntries(dirPath);
- // Check for an exact match (prefixed or bare) first
+ // Check for an exact match (prefixed or bare) first. The listing in hand
+ // holds every name in the folder, so no name is probed on disk.
for (const candidate of candidateFiles) {
+ const entry = entries.find(e => e.name === candidate);
+ if (!entry) continue;
const exact = path.join(dirPath, candidate);
- if (existsCaseExact(exact, dirPath)) return exact;
+ if (!entry.isSymbolicLink() || pathExists(exact)) return exact;
}
// Recurse into subdirectories
for (const entry of entries) {
if (entry.isDirectory()) {
- const found = searchDir(path.join(dirPath, entry.name));
+ const found = searchDir(path.join(dirPath, entry.name), true);
if (found) return found;
}
}
@@ -1703,6 +1713,16 @@ export function unscopedComponentOwners(owners) {
* @returns {Array<{name: string, package: string, group: string|null, docPath: string|null, sourcePath: string|null, issuesUrl: string|undefined}>}
*/
export function discoverOwnedComponents(coreDir, loadedIntegrations = []) {
+ // Read-only: one listing of each folder serves the whole discovery.
+ return withListingCache(() => ownedComponents(coreDir, loadedIntegrations));
+}
+
+/**
+ * @param {string} coreDir
+ * @param {Array<{name: string, components?: string, issuesUrl?: string}>} loadedIntegrations
+ * @returns {Array<{name: string, package: string, group: string|null, docPath: string|null, sourcePath: string|null, issuesUrl: string|undefined}>}
+ */
+function ownedComponents(coreDir, loadedIntegrations) {
/** @type {Array<{name: string, package: string, group: string|null, docPath: string|null, sourcePath: string|null, issuesUrl: string|undefined}>} */
const records = [];
diff --git a/packages/cli/foundation/discovery/component-discovery.test.mjs b/packages/cli/foundation/discovery/component-discovery.test.mjs
index 74cab3355c..34629f71c2 100644
--- a/packages/cli/foundation/discovery/component-discovery.test.mjs
+++ b/packages/cli/foundation/discovery/component-discovery.test.mjs
@@ -334,3 +334,51 @@ describe('integration component discovery (ownership-aware)', () => {
expect(recs.find(r => r.name === 'MetaAppShell').package).toBe('@acme/astryx-meta');
}, SLOW);
});
+
+describe('findComponentSource reads names from the folders it lists', () => {
+ /** A Core folder with these files; a value of `{link}` is a symbolic link. */
+ function core(files) {
+ const dir = mkTmp('astryx-core-source-');
+ for (const [rel, contents] of Object.entries(files)) {
+ const file = path.join(dir, 'src', rel);
+ fs.mkdirSync(path.dirname(file), {recursive: true});
+ if (typeof contents === 'object') {
+ fs.symlinkSync(path.join(dir, 'src', contents.link), file);
+ } else {
+ fs.writeFileSync(file, contents);
+ }
+ }
+ return dir;
+ }
+
+ it('finds the prefixed source before the bare one, in a nested folder', () => {
+ const dir = core({
+ 'Layout/Card/Card.tsx': '',
+ 'Layout/Card/XDSCard.tsx': '',
+ });
+ expect(findComponentSource(dir, 'Card')).toBe(
+ path.join(dir, 'src', 'Layout', 'Card', 'XDSCard.tsx'),
+ );
+ });
+
+ it('passes over a case variant and a link to a missing file', () => {
+ const dir = core({
+ 'Card/card.tsx': '',
+ 'Card/XDSCard.tsx': {link: 'Card/Gone.tsx'},
+ 'Card/inner/Card.tsx': '',
+ });
+ expect(findComponentSource(dir, 'Card')).toBe(
+ path.join(dir, 'src', 'Card', 'inner', 'Card.tsx'),
+ );
+ });
+
+ it('follows a link to a file that is there', () => {
+ const dir = core({
+ 'shared/Card.tsx': '',
+ 'Card/Card.tsx': {link: 'shared/Card.tsx'},
+ });
+ expect(findComponentSource(dir, 'Card')).toBe(
+ path.join(dir, 'src', 'Card', 'Card.tsx'),
+ );
+ });
+});
diff --git a/packages/cli/foundation/fs/listing-cache.mjs b/packages/cli/foundation/fs/listing-cache.mjs
index bfb2237013..b8253577e6 100644
--- a/packages/cli/foundation/fs/listing-cache.mjs
+++ b/packages/cli/foundation/fs/listing-cache.mjs
@@ -23,6 +23,7 @@ import * as fs from 'node:fs';
* @typedef {{
* names: Map<string, string[]>,
* entries: Map<string, fs.Dirent[]>,
+ * byName: Map<string, Map<string, fs.Dirent>>,
* exists: Map<string, boolean>,
* }} ListingCache
*/
@@ -40,11 +41,24 @@ const scope = new AsyncLocalStorage();
export function withListingCache(fn) {
if (scope.getStore()) return fn();
return scope.run(
- {names: new Map(), entries: new Map(), exists: new Map()},
+ {
+ names: new Map(),
+ entries: new Map(),
+ byName: new Map(),
+ exists: new Map(),
+ },
fn,
);
}
+/**
+ * Whether an operation's listing cache is active.
+ * @returns {boolean}
+ */
+export function listingCacheActive() {
+ return scope.getStore() != null;
+}
+
/**
* `fs.readdirSync(dir)`, read once per operation. Returns a fresh array, so a
* caller that sorts it cannot reorder another caller's listing.
@@ -78,6 +92,41 @@ export function listDirEntries(dir) {
return entries.slice();
}
+/**
+ * The entry named exactly `name` in `dir`'s listing, or null when the folder
+ * holds no such name or cannot be read. Inside an operation, a name the
+ * listing lacks costs no probe of the disk.
+ * @param {string} dir
+ * @param {string} name
+ * @returns {fs.Dirent | null}
+ */
+export function findEntry(dir, name) {
+ const cache = scope.getStore();
+ if (!cache) {
+ try {
+ return (
+ fs
+ .readdirSync(dir, {withFileTypes: true})
+ .find(entry => entry.name === name) ?? null
+ );
+ } catch {
+ return null;
+ }
+ }
+ let byName = cache.byName.get(dir);
+ if (!byName) {
+ let entries;
+ try {
+ entries = listDirEntries(dir);
+ } catch {
+ return null;
+ }
+ byName = new Map(entries.map(entry => [entry.name, entry]));
+ cache.byName.set(dir, byName);
+ }
+ return byName.get(name) ?? null;
+}
+
/**
* `fs.existsSync(target)`, probed once per operation.
* @param {string} target
diff --git a/packages/cli/foundation/fs/listing-cache.test.mjs b/packages/cli/foundation/fs/listing-cache.test.mjs
index 9696104ca5..56b762e597 100644
--- a/packages/cli/foundation/fs/listing-cache.test.mjs
+++ b/packages/cli/foundation/fs/listing-cache.test.mjs
@@ -5,8 +5,10 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {
+ findEntry,
listDir,
listDirEntries,
+ listingCacheActive,
pathExists,
withListingCache,
} from './listing-cache.mjs';
@@ -85,4 +87,73 @@ describe('listing cache', () => {
expect(existsCaseExact(path.join(dir, 'button'), dir)).toBe(false);
});
});
+
+ it('says whether an operation is running', async () => {
+ expect(listingCacheActive()).toBe(false);
+ await withListingCache(async () => {
+ await new Promise(resolve => setTimeout(resolve, 0));
+ expect(listingCacheActive()).toBe(true);
+ });
+ expect(listingCacheActive()).toBe(false);
+ });
+
+ it('finds an entry by its exact name, from the listing inside an operation', () => {
+ withListingCache(() => {
+ expect(findEntry(dir, 'a.txt')?.isFile()).toBe(true);
+ expect(findEntry(dir, 'A.txt')).toBeNull();
+ fs.writeFileSync(path.join(dir, 'c.txt'), '');
+ // The listing answers, not the disk.
+ expect(findEntry(dir, 'c.txt')).toBeNull();
+ expect(findEntry(path.join(dir, 'missing'), 'a.txt')).toBeNull();
+ });
+ expect(findEntry(dir, 'c.txt')?.isFile()).toBe(true);
+ });
+
+ it('answers existsCaseExact from the listings inside an operation, as outside', () => {
+ fs.mkdirSync(path.join(dir, 'src', 'Button'), {recursive: true});
+ fs.writeFileSync(path.join(dir, 'src', 'Button', 'Button.tsx'), '');
+ fs.symlinkSync(
+ path.join(dir, 'src', 'Button', 'Button.tsx'),
+ path.join(dir, 'src', 'Linked.tsx'),
+ );
+ fs.symlinkSync(
+ path.join(dir, 'src', 'Gone.tsx'),
+ path.join(dir, 'src', 'Dangling.tsx'),
+ );
+ const cases = [
+ [path.join('src', 'Button', 'Button.tsx'), true],
+ [path.join('src', 'button', 'Button.tsx'), false],
+ [path.join('src', 'Button', 'button.tsx'), false],
+ [path.join('src', 'Linked.tsx'), true],
+ [path.join('src', 'Dangling.tsx'), false],
+ [path.join('src', 'Button', 'Button.tsx', 'x'), false],
+ [path.join('src', 'Missing.tsx'), false],
+ // Not plain paths under the root: these take path.relative.
+ [`src${path.sep}..${path.sep}src${path.sep}Linked.tsx`, true],
+ [`src${path.sep}${path.sep}Linked.tsx`, true],
+ [
+ `..${path.sep}${path.basename(dir)}${path.sep}src${path.sep}Linked.tsx`,
+ true,
+ ],
+ ];
+ const outside = cases.map(([rel]) =>
+ existsCaseExact(`${dir}${path.sep}${rel}`, dir),
+ );
+ const inside = withListingCache(() =>
+ cases.map(([rel]) => existsCaseExact(`${dir}${path.sep}${rel}`, dir)),
+ );
+ expect(outside).toEqual(cases.map(([, exists]) => exists));
+ expect(inside).toEqual(outside);
+ withListingCache(() => {
+ expect(existsCaseExact(dir, dir)).toBe(true);
+ expect(existsCaseExact(path.dirname(dir), dir)).toBe(false);
+ // A root written with a trailing separator.
+ expect(
+ existsCaseExact(
+ path.join(dir, 'src', 'Linked.tsx'),
+ `${dir}${path.sep}`,
+ ),
+ ).toBe(true);
+ });
+ });
});
diff --git a/packages/cli/foundation/fs/paths.mjs b/packages/cli/foundation/fs/paths.mjs
index 98b96004aa..bfe2735eed 100644
--- a/packages/cli/foundation/fs/paths.mjs
+++ b/packages/cli/foundation/fs/paths.mjs
@@ -13,7 +13,12 @@ import {createRequire} from 'node:module';
import * as os from 'node:os';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {listDir, pathExists} from './listing-cache.mjs';
+import {
+ findEntry,
+ listDir,
+ listingCacheActive,
+ pathExists,
+} from './listing-cache.mjs';
import {ERROR_CODES} from '../response/error-codes.mjs';
import {carriedCore, runChoice} from '../release/run-context.mjs';
import {findDeclaringPackage} from '../release/project.mjs';
@@ -867,6 +872,31 @@ export function listComponents(coreDir) {
.sort();
}
+/**
+ * `path.relative(root, target)`, read straight off the string when `target`
+ * is a plain path under `root`. A resolver probes tens of thousands of such
+ * paths, and `path.relative` normalizes both on every call.
+ * @param {string} root
+ * @param {string} target
+ * @returns {string}
+ */
+function relativeInside(root, target) {
+ const prefix = root.endsWith(path.sep) ? root : `${root}${path.sep}`;
+ if (path.isAbsolute(root) && target.startsWith(prefix)) {
+ const rest = target.slice(prefix.length);
+ const plain =
+ rest !== '' &&
+ !(path.sep === '\\' && rest.includes('/')) &&
+ rest
+ .split(path.sep)
+ .every(
+ segment => segment !== '' && segment !== '.' && segment !== '..',
+ );
+ if (plain) return rest;
+ }
+ return path.relative(root, target);
+}
+
/**
* Does `targetPath` exist with EXACTLY this spelling?
*
@@ -883,13 +913,28 @@ export function listComponents(coreDir) {
* @returns {boolean}
*/
export function existsCaseExact(targetPath, rootDir) {
- if (!pathExists(targetPath)) return false;
-
- const rel = path.relative(rootDir, targetPath);
- if (rel === '') return true;
+ const rel = relativeInside(rootDir, targetPath);
+ if (rel === '') return pathExists(targetPath);
if (path.isAbsolute(rel) || rel === '..' || rel.startsWith(`..${path.sep}`))
return false;
+ if (listingCacheActive()) {
+ // Inside an operation the folders' listings answer, so a name that a
+ // listing lacks costs no probe of the disk. A symbolic link still needs
+ // its target.
+ let folder = rootDir;
+ let entry = null;
+ for (const segment of rel.split(path.sep)) {
+ entry = findEntry(folder, segment);
+ if (!entry) return false;
+ folder = folder.endsWith(path.sep)
+ ? `${folder}${segment}`
+ : `${folder}${path.sep}${segment}`;
+ }
+ return !entry?.isSymbolicLink() || pathExists(targetPath);
+ }
+
+ if (!pathExists(targetPath)) return false;
let dir = rootDir;
for (const segment of rel.split(path.sep)) {
let entries;
diff --git a/packages/cli/test-utils/warm-integration-checks.mjs b/packages/cli/test-utils/warm-integration-checks.mjs
new file mode 100644
index 0000000000..f1c63dec5a
--- /dev/null
+++ b/packages/cli/test-utils/warm-integration-checks.mjs
@@ -0,0 +1,83 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The integration checks' one-time start in a test file, paid while the
+ * file is collected.
+ *
+ * @input Where the test file keeps its fixtures: under the repository root,
+ * or in the system's temporary folder.
+ * @output `warmIntegrationChecks()`: one `integrationCheck()` on a tiny
+ * package there. It throws when the checks do not run in full.
+ * @position The first check in a test file parses TypeScript's lib files,
+ * and the first in a worker also loads TypeScript. Later checks reuse that
+ * work. A test file awaits this at its top level, which vitest does not
+ * time, so no test carries that start.
+ */
+
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {integrationCheck} from '../api/integration/check/check.mjs';
+
+/**
+ * Runs the checks once on a tiny component package, laid out like the
+ * calling file's own fixtures.
+ * @param {{inRepository?: boolean}} [options] `false` for a file whose
+ * fixtures live in the system's temporary folder.
+ * @returns {Promise<void>}
+ */
+export async function warmIntegrationChecks({inRepository = true} = {}) {
+ const parent = inRepository ? process.cwd() : os.tmpdir();
+ const dir = fs.mkdtempSync(path.join(parent, '.astryx-warm-checks-'));
+ try {
+ if (inRepository) {
+ // As the repository's fixtures do: the root's compiler options, and no
+ // file outside the package.
+ fs.writeFileSync(
+ path.join(dir, 'tsconfig.json'),
+ `${JSON.stringify({extends: '../tsconfig.json', include: ['**/*']})}\n`,
+ );
+ }
+ const pkg = path.join(dir, 'pkg');
+ fs.mkdirSync(path.join(pkg, 'components'), {recursive: true});
+ fs.writeFileSync(
+ path.join(pkg, 'package.json'),
+ JSON.stringify({name: '@acme/warm', version: '1.0.0'}),
+ );
+ fs.writeFileSync(
+ path.join(pkg, 'astryx.integration.mjs'),
+ "export default {components: './components'};\n",
+ );
+ fs.writeFileSync(
+ path.join(pkg, 'components', 'AcmeWarm.doc.mjs'),
+ "export default {type: 'component', name: 'AcmeWarm', displayName: 'AcmeWarm', usage: {description: 'AcmeWarm.'}, props: []};\n",
+ );
+ fs.writeFileSync(
+ path.join(pkg, 'components', 'AcmeWarm.tsx'),
+ 'export function AcmeWarm() { return null; }\n',
+ );
+ const result = await integrationCheck({cwd: pkg});
+ /** @type {Map<string, any>} */
+ const ran = new Map(
+ result.data.checks.map(each => [each.check, each.response.data]),
+ );
+ const validated = ran.get('validate');
+ // The package validates with no issue. In the repository both TypeScript
+ // checks run too. Outside it no Core resolves, as in the calling file's
+ // own fixtures.
+ const complete =
+ validated?.validated === true &&
+ validated.issues.length === 0 &&
+ (!inRepository ||
+ (result.data.errors === 0 &&
+ validated.docTypes?.status === 'checked' &&
+ ran.get('components')?.propDrift?.status === 'checked'));
+ if (!complete) {
+ throw new Error(
+ `warmIntegrationChecks: the checks did not run in full on the warm-up package: ${JSON.stringify(result.data.checks)}`,
+ );
+ }
+ } finally {
+ fs.rmSync(dir, {recursive: true, force: true});
+ }
+}