|
643 | 643 | // `node_modules` exclusion: an exact-path list matches nothing it does not name. |
644 | 644 | import { defineConfig } from 'vitest/config'; |
645 | 645 | import path from 'path'; |
646 | | -import { parseCLI, type TestUserConfig } from 'vitest/node'; |
| 646 | +import { createHash } from 'node:crypto'; |
| 647 | +import { BaseSequencer, parseCLI, type TestSpecification, type TestUserConfig } from 'vitest/node'; |
647 | 648 | import { |
648 | 649 | runFilterPreflight, |
649 | 650 | runProjectCliOverridePreflight, |
@@ -740,6 +741,103 @@ runProjectCliOverridePreflight({ |
740 | 741 | // lives at its use site in the `test` block below, not in a helper binding |
741 | 742 | // that could outlive the spread. |
742 | 743 |
|
| 744 | +// #25870 — THE FILES THAT READ BUILT DECLARATIONS RUN IN SLICE 1/n, AND ONLY |
| 745 | +// THERE. Test Core builds a shard's dependency closure without `.d.ts` |
| 746 | +// (`OS_SKIP_DTS=1`, ci.yml "Choose this shard's declaration build") unless the |
| 747 | +// shard tests a package whose tests compile against BUILT declarations. In this |
| 748 | +// package exactly the files in `DECLARATION_READER_FILES` do: each runs `tsc` |
| 749 | +// over a packed tarball or a scaffolded project that resolves |
| 750 | +// `@objectstack/spec` (or this package) through `dist/*.d.ts`, and on a |
| 751 | +// declaration-less closure each reds with TS7016 or ENOENT. Measured, never |
| 752 | +// recalled: build the closure with `OS_SKIP_DTS=1`, run the whole suite, and |
| 753 | +// the red files are this list; with declarations they are green (the PR that |
| 754 | +// added this section carries both readings). |
| 755 | +// |
| 756 | +// vitest's own `shard()` scatters files by the sha1 of their path, so those |
| 757 | +// files sat in all four CLI slices and every slice shard paid the ~4.5 min |
| 758 | +// declaration closure for one or two files. `DeclarationReaderSequencer` puts |
| 759 | +// them first, then every other file in vitest's own sha1 order, and cuts that |
| 760 | +// one sequence into vitest's own equal-count ranges, so the readers take the |
| 761 | +// first places of slice 1/n and each other file moves at most a few places |
| 762 | +// along vitest's order. ci.yml then keeps declarations for slice 1/n only, and |
| 763 | +// `scripts/partition-test-shards.mjs` (`DECLARATION_READER_SLICES`) charges |
| 764 | +// that slice the closure so its shard carries less whole-package work. The |
| 765 | +// index is spelled in those three places; the partitioner's self-test reads the |
| 766 | +// other two back. |
| 767 | +// |
| 768 | +// ⛔ Checked at config load, both directions of drift: |
| 769 | +// - a listed file that is not in this run's queue population (renamed, |
| 770 | +// deleted, or moved to a nightly tier) refuses to load the config, naming |
| 771 | +// it -- a pin on a file that no longer runs would keep slice 1 holding the |
| 772 | +// closure for nothing; |
| 773 | +// - a NEW reader that is not listed lands wherever sha1 puts it. On slices |
| 774 | +// 2..n it reds its own PR, loudly and deterministically, with TS7016 or |
| 775 | +// ENOENT on a declaration-less closure: add it here. On slice 1 it passes |
| 776 | +// until a later file moves the cut past it, and then reds the same way. |
| 777 | +const DECLARATION_READER_SLICE = 1; |
| 778 | +export const DECLARATION_READER_FILES = Object.freeze([ |
| 779 | + 'test/create-in-repo-scaffold-typechecks.test.ts', |
| 780 | + 'test/published-subpath-console.pin.test.ts', |
| 781 | + 'test/published-subpath-hook-body.pin.test.ts', |
| 782 | + 'test/scaffold-emission-typechecks.test.ts', |
| 783 | + 'test/scaffold-test-script-passes.test.ts', |
| 784 | +]); |
| 785 | + |
| 786 | +// The run's queue population holds every listed reader; a nightly-tier run |
| 787 | +// holds none of them, and the pin is inert there (its closure builds |
| 788 | +// declarations on every shard). |
| 789 | +if (readTierMode() === 'queue') { |
| 790 | + const population = new Set([...UNIT_FILES, ...INTEGRATION_FILES]); |
| 791 | + const missing = DECLARATION_READER_FILES.filter((file) => !population.has(file)); |
| 792 | + if (missing.length > 0) { |
| 793 | + throw new Error( |
| 794 | + `packages/cli/vitest.config.ts: DECLARATION_READER_FILES names ${missing.join(', ')}, which this ` + |
| 795 | + 'package\'s queue test population does not hold. Remove or rename the entry: a pinned reader that ' + |
| 796 | + 'never runs keeps slice 1/n building declarations for nothing.' |
| 797 | + ); |
| 798 | + } |
| 799 | +} |
| 800 | + |
| 801 | +// vitest's equal-count range for slice `index` of `count` over `total` files |
| 802 | +// (`BaseSequencer.calculateShardRange`, which vitest declares private). |
| 803 | +function shardRange(total: number, index: number, count: number): [number, number] { |
| 804 | + const base = Math.floor(total / count); |
| 805 | + const remainder = total % count; |
| 806 | + if (remainder >= index) return [(base + 1) * (index - 1), (base + 1) * index]; |
| 807 | + const start = remainder * (base + 1) + (index - remainder - 1) * base; |
| 808 | + return [start, start + base]; |
| 809 | +} |
| 810 | + |
| 811 | +const READER_SET = new Set<string>(DECLARATION_READER_FILES); |
| 812 | + |
| 813 | +class DeclarationReaderSequencer extends BaseSequencer { |
| 814 | + override async shard(files: TestSpecification[]): Promise<TestSpecification[]> { |
| 815 | + const { root, shard } = this.ctx.config; |
| 816 | + if (!shard) return files; |
| 817 | + const { index, count } = shard; |
| 818 | + const relative = (spec: TestSpecification) => |
| 819 | + path.relative(root, spec.moduleId).split(path.sep).join('/'); |
| 820 | + // vitest's own key: the path below the root, with its leading separator. |
| 821 | + const key = (spec: TestSpecification) => createHash('sha1').update(`/${relative(spec)}`).digest('hex'); |
| 822 | + const readers = files.filter((spec) => READER_SET.has(relative(spec))); |
| 823 | + const rest = files |
| 824 | + .filter((spec) => !READER_SET.has(relative(spec))) |
| 825 | + .map((spec) => ({ spec, hash: key(spec) })) |
| 826 | + .sort((a, b) => (a.hash < b.hash ? -1 : a.hash > b.hash ? 1 : 0)) |
| 827 | + .map(({ spec }) => spec); |
| 828 | + const ordered = [...readers.sort((a, b) => (relative(a) < relative(b) ? -1 : 1)), ...rest]; |
| 829 | + const [start, end] = shardRange(ordered.length, DECLARATION_READER_SLICE, count); |
| 830 | + if (readers.length > end - start) { |
| 831 | + throw new Error( |
| 832 | + `packages/cli/vitest.config.ts: ${readers.length} declaration readers do not fit in slice ` + |
| 833 | + `${DECLARATION_READER_SLICE}/${count}, which holds ${end - start} files. Pin fewer files, or slice fewer ways.` |
| 834 | + ); |
| 835 | + } |
| 836 | + const [from, to] = shardRange(ordered.length, index, count); |
| 837 | + return ordered.slice(from, to); |
| 838 | + } |
| 839 | +} |
| 840 | + |
743 | 841 | // #23181 slice 14 — THE OCLIF MANIFEST SETUP. One `globalSetup` module writes |
744 | 842 | // `oclif.manifest.json` from source when the `integration` project runs (the |
745 | 843 | // tier every CLI-spawning file is derived into) and removes it at teardown, so |
@@ -869,6 +967,10 @@ export default defineConfig({ |
869 | 967 | // The file-level slice, when Test Core runs one (#19278) — see the section |
870 | 968 | // above `export default` for why it is spread and typed this way. |
871 | 969 | ...({ shard: process.env.OS_TEST_SHARD } satisfies Pick<TestUserConfig, 'shard'>), |
| 970 | + // The declaration readers into slice 1/n (#25870) -- see the section of |
| 971 | + // that number above `export default`. Read by the root context only, which |
| 972 | + // is the one that shards. |
| 973 | + sequence: { sequencer: DeclarationReaderSequencer }, |
872 | 974 | // A late console.* must not redden a green suite (#10374): vitest's worker |
873 | 975 | // forwards console output over RPC and discards the promise, and a write |
874 | 976 | // landing after teardown's rpcDone() snapshot is rejected into an unhandled |
|
0 commit comments