Skip to content

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

TypeScript

fix(cli): searchCore reuses the gathered candidates, as search does (#8512)

Christopher Chedeaud0a310646964
diff --git a/.changeset/cli-search-core-reuse.md b/.changeset/cli-search-core-reuse.md
new file mode 100644
index 0000000000..50dec5d98d
--- /dev/null
+++ b/.changeset/cli-search-core-reuse.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] The lexical ranker behind `astryx build`'s search ranks the candidates search gathers, reused the same way. A later query in the same folder, for the same `type`, gathers again only when an input changed. In a long-running caller, a build that follows a build or a search no longer reads every component, hook, doc, template and theme again. A search that follows a build no longer reads them again either. Results are unchanged.
+
+@vjeux
diff --git a/packages/cli/api/search/_adapter.test.mjs b/packages/cli/api/search/_adapter.test.mjs
index 87566e30fa..6d59e4b7bb 100644
--- a/packages/cli/api/search/_adapter.test.mjs
+++ b/packages/cli/api/search/_adapter.test.mjs
@@ -4,7 +4,8 @@
* @file Tests for search's adapter: a long-running caller reuses the gathered
* candidates while their inputs are unchanged (gatherCandidatesReused), shares
* a gather that is still running, and gathers again when an input changes or
- * for another folder or `type`.
+ * for another folder or `type`. The lexical ranker (searchCore) ranks from the
+ * same reused gather.
*
* Whether inputs changed is read-record's answer (tested in
* read-record.test.mjs). Other tests running at the same time change shared
@@ -18,8 +19,11 @@ import {afterAll, beforeAll, describe, it, expect, vi} from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {Project} from '../../foundation/config/project.mjs';
import * as readRecord from '../../foundation/fs/read-record.mjs';
import {gatherCandidates, gatherCandidatesReused} from './_adapter.mjs';
+import {searchCore} from './search.mjs';
vi.mock('../../foundation/fs/read-record.mjs', async original => {
const real = /** @type {typeof readRecord} */ (await original());
@@ -29,6 +33,12 @@ const unchangedSince = vi.mocked(readRecord.unchangedSince);
const SLOW = 60_000;
+/** The repository root: Core's source is there, so every domain gathers. */
+const REPO = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ '../../../..',
+);
+
/** A folder with only a package.json: a docs search there reads the CLI's docs. */
function scratchProject() {
const dir = fs.mkdtempSync(path.join(TEMP_ROOT, 'astryx-gather-reuse-'));
@@ -222,3 +232,23 @@ describe('gatherCandidatesReused', () => {
SLOW,
);
});
+
+describe('searchCore', () => {
+ it(
+ 'ranks from the reused gather: a second query in the same folder gathers nothing again',
+ async () => {
+ const first = await searchCore('Button', {cwd: REPO});
+ const load = vi.spyOn(Project, 'load');
+ try {
+ unchangedSince.mockReturnValueOnce(true);
+ const second = await searchCore('Button', {cwd: REPO});
+ // A gather loads the project for each domain; the reused one, none.
+ expect(load).not.toHaveBeenCalled();
+ expect(second).toEqual(first);
+ } finally {
+ load.mockRestore();
+ }
+ },
+ SLOW,
+ );
+});
diff --git a/packages/cli/api/search/search.mjs b/packages/cli/api/search/search.mjs
index 046b1e7d11..b6c34ae4b5 100644
--- a/packages/cli/api/search/search.mjs
+++ b/packages/cli/api/search/search.mjs
@@ -19,11 +19,7 @@ import {
runWithDocPackageManager,
} from '../../foundation/env/package-manager.mjs';
import {withListingCache} from '../../foundation/fs/listing-cache.mjs';
-import {
- gatherCandidates,
- gatherCandidatesReused,
- loadSearchData,
-} from './_adapter.mjs';
+import {gatherCandidatesReused, loadSearchData} from './_adapter.mjs';
import {rankLexical} from './_lexical.mjs';
import {checkSearchArgs} from './_result.mjs';
import {runTree} from '../../foundation/route/route.mjs';
@@ -76,7 +72,10 @@ async function searchRead(query, options) {
await runTree(
SEARCH_TREE,
{query, options},
- {...(await gatherCandidatesReused(options)), data: loadSearchData()},
+ {
+ ...(await gatherCandidatesReused(options)),
+ data: loadSearchData(),
+ },
)
),
options.cwd ?? process.cwd(),
@@ -87,7 +86,9 @@ async function searchRead(query, options) {
/**
* The lexical ranker alone: keyword and fuzzy scoring of every candidate.
- * `build` ranks from it.
+ * `build` ranks from it. It ranks the candidates search gathers, reused the
+ * same way. A later query in the same folder, for the same `type`, gathers
+ * again only when an input changed.
*
* @param {string} query
* @param {{cwd?: string, type?: 'component'|'hook'|'doc'|'template'|'theme', limit?: number}} [options]
@@ -109,7 +110,7 @@ export async function searchCore(query, options = {}) {
async function searchCoreRead(query, options) {
return withListingCache(async () =>
linkSearchResponse(
- rankLexical(query, options, await gatherCandidates(options)),
+ rankLexical(query, options, await gatherCandidatesReused(options)),
options.cwd ?? process.cwd(),
),
);