Skip to content

Commit 6224efd

Browse files
authored
test(shadcn-registry): ShadCN's temporary directories stay in the consumer project (#9223)
## Maintainer impact Each run of `internal/shadcn-registry/generate-shadcn-registry.test.mjs`, which is part of `pnpm test:node`, leaves 16 directories in the system temporary directory. `verify-full-catalog.mjs` leaves one for every file ShadCN transforms. On a shared machine that runs these often, the temporary directory held 187,072 `shadcn-*` directories, and every tool that reads that directory's listing slowed down (that size is what timed out the analyze-pr test fixed in #9212). ## Intended invariant The test and the catalog verifier check what they checked before, and leave nothing behind in the system temporary directory. ## Change The pinned ShadCN client, 4.19.0, makes an empty temporary directory (`mkdtemp(path.join(tmpdir(), "shadcn-"))`) for each file it transforms during `shadcn add` and never removes it. Both callers now give it a `TMPDIR` inside the consumer project they already create and remove in `finally`: the test's five `shadcn add` runs, and `runShadcn` in `verify-full-catalog.mjs`. The test also removes its no-op package-manager directory in `afterAll`; it was made once per run and never removed. Upstream origin: ShadCN's transform step in 4.19.0. 4.22.1, which #8750 bumps to, no longer makes these in its transform step, but its icons step still makes one. This change keeps whatever ShadCN makes inside the project either way. Nothing is filed upstream. ## Evidence - Failure before the change: with `TMPDIR` pointed at an empty directory, one run of the test file (29 tests pass) left 15 `shadcn-*` directories and 1 `astryx-noop-pm-*`. One run of `verify-full-catalog.mjs` over a 12-item catalog (18 installed files per mode, in TypeScript and JavaScript) left 60 `shadcn-*`. - Success after the change: the same runs left none: 0 and 0 from the test file (29 tests pass), and 0 from the verifier over the same catalog (it passes). Node's own `node-compile-cache` directory appears in both runs: a cache Node reuses, not one per run. - Product/runtime behavior verified unchanged: test and CI verifier only. ## Scope - [x] No intended public API, product behavior, visual direction, or policy change. - [x] Product changes discovered during the work were removed or split. - [x] Public text and artifacts contain no internal Meta context. ## Testing - Leftovers counted with `TMPDIR=<empty directory> pnpm exec vitest run --project node internal/shadcn-registry/generate-shadcn-registry.test.mjs`, and with `TMPDIR=<empty directory> node internal/shadcn-registry/verify-full-catalog.mjs` after `pnpm build` and `DOCSITE_TARGET=canary node apps/docsite/scripts/generate-data.mjs`, as CI runs it. The verifier ran over a 12-item subset of the 1,036-item canary catalog (4 components, 2 hooks, 4 examples, 2 templates; `registry.json` cut to them and restored afterwards), because the whole catalog was too heavy for a shared machine. - Local gates at 92ba203 (one commit on main dade717): build, typecheck and repo checks pass; the ShadCN registry tests and the CI contract test for the verifier pass (36); the node suite passes except the two `cache.test.mjs` lock tests that fail on this machine without the change too (#8520).
1 parent 0fa0207 commit 6224efd

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎internal/shadcn-registry/generate-shadcn-registry.test.mjs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* @file Registry serializer contract tests.
55
* @input Minimal component, block, page, and StyleX fixtures.
66
* @output Regression coverage for schema, package boundaries, names, and output.
7+
* The stock-ShadCN cases leave nothing in the system temporary directory:
8+
* ShadCN's own scratch directories stay in each consumer project, and the
9+
* no-op package managers are removed after the file.
710
* @position Node test lane for ShadCN compatibility.
811
*/
912

@@ -22,7 +25,7 @@ import {tmpdir} from 'node:os';
2225
import path from 'node:path';
2326
import {fileURLToPath} from 'node:url';
2427
import {promisify} from 'node:util';
25-
import {describe, expect, it} from 'vitest';
28+
import {afterAll, describe, expect, it} from 'vitest';
2629
import {reconcileRegistryCompositions} from '../../packages/cli/api/upgrade/registry/registry.mjs';
2730
import {
2831
createShadcnPrecompiledDeclaration,
@@ -79,6 +82,17 @@ const SHADCN_ENV = {
7982
...process.env,
8083
PATH: `${NOOP_PACKAGE_MANAGERS}${path.delimiter}${process.env.PATH ?? ''}`,
8184
};
85+
afterAll(() => {
86+
rmSync(NOOP_PACKAGE_MANAGERS, {recursive: true, force: true});
87+
});
88+
89+
// ShadCN leaves an empty temporary directory behind for every file it
90+
// transforms, so it gets a TMPDIR inside the project, which each test removes.
91+
function shadcnEnv(project) {
92+
const scratch = path.join(project, '.tmp');
93+
mkdirSync(scratch, {recursive: true});
94+
return {...SHADCN_ENV, TMPDIR: scratch};
95+
}
8296

8397
const packages = [
8498
{
@@ -618,7 +632,7 @@ describe('buildShadcnRegistry', () => {
618632
await execFileAsync(
619633
process.execPath,
620634
[SHADCN_ENTRY, 'add', itemPath, '--yes'],
621-
{cwd: project, timeout: 30_000, env: SHADCN_ENV},
635+
{cwd: project, timeout: 30_000, env: shadcnEnv(project)},
622636
);
623637

624638
expect(
@@ -711,7 +725,7 @@ describe('buildShadcnRegistry', () => {
711725
await execFileAsync(
712726
process.execPath,
713727
[SHADCN_ENTRY, 'add', itemPath, '--yes'],
714-
{cwd: project, timeout: 30_000, env: SHADCN_ENV},
728+
{cwd: project, timeout: 30_000, env: shadcnEnv(project)},
715729
);
716730

717731
const sourcePath = path.join(
@@ -792,7 +806,7 @@ describe('buildShadcnRegistry', () => {
792806
await execFileAsync(
793807
process.execPath,
794808
[SHADCN_ENTRY, 'add', itemPath, '--yes'],
795-
{cwd: project, timeout: 30_000, env: SHADCN_ENV},
809+
{cwd: project, timeout: 30_000, env: shadcnEnv(project)},
796810
);
797811
await new Promise((resolve, reject) => {
798812
server.once('error', reject);
@@ -941,7 +955,7 @@ describe('buildShadcnRegistry', () => {
941955
await execFileAsync(
942956
process.execPath,
943957
[SHADCN_ENTRY, 'add', itemPath, '--yes'],
944-
{cwd: project, timeout: 30_000, env: SHADCN_ENV},
958+
{cwd: project, timeout: 30_000, env: shadcnEnv(project)},
945959
);
946960

947961
expect(
@@ -1022,7 +1036,7 @@ describe('buildShadcnRegistry', () => {
10221036
cwd: project,
10231037
timeout: 30_000,
10241038
env: {
1025-
...SHADCN_ENV,
1039+
...shadcnEnv(project),
10261040
NO_PROXY: '127.0.0.1,localhost',
10271041
no_proxy: '127.0.0.1,localhost',
10281042
},

‎internal/shadcn-registry/verify-full-catalog.mjs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* ShadCN client in TypeScript and JavaScript modes, verifies exact written
88
* bytes and dependencies, then compiles every installed source file against
99
* current local exports or the exact published packages named by production.
10+
* ShadCN's own scratch directories stay inside each consumer, which is
11+
* removed afterwards.
1012
* @input A generated preview or hidden production registry under
1113
* apps/docsite/public/shadcn.
1214
* @output A clean-consumer proof for every canonical item and alias route.
@@ -449,13 +451,17 @@ function runShadcn(project, itemPaths) {
449451
if (!fs.existsSync(SHADCN_BIN)) {
450452
fail('the pinned ShadCN binary is not installed');
451453
}
454+
// ShadCN leaves an empty temporary directory behind for every file it
455+
// transforms: keep them inside the consumer, which main() removes.
456+
const scratch = path.join(project, '.tmp');
457+
fs.mkdirSync(scratch, {recursive: true});
452458
const result = spawnSync(
453459
SHADCN_BIN,
454460
['add', ...itemPaths, '--yes', '--overwrite', '--silent'],
455461
{
456462
cwd: project,
457463
encoding: 'utf8',
458-
env: {...process.env, CI: 'true'},
464+
env: {...process.env, CI: 'true', TMPDIR: scratch},
459465
maxBuffer: MAX_COMMAND_OUTPUT,
460466
timeout: 10 * 60_000,
461467
},

0 commit comments

Comments
 (0)