Skip to content

Commit 7081f7b

Browse files
committed
ci(test-core): build declarations on one CLI slice only — pin the CLI's declaration readers into slice 1/n
The five CLI test files that compile against built .d.ts sat in all four CLI slices by vitest's sha1 split, so five Test Core shards built the declaration closure (~4.5 min) for one or two files each. A sequencer in packages/cli/vitest.config.ts now puts them first in vitest's own order, so they land in slice 1/n; ci.yml keeps declarations for a whole reader or slice 1/* only; the partitioner charges that slice the closure (270 s x 2.9 of packing weight) and seats @objectstack/metadata beside it, so one shard builds declarations instead of five. Claude-Session: https://claude.ai/code/session_01JQwxbaFpnMwgpCgneKe2qS Co-authored-by: Claude <noreply@anthropic.com>
1 parent 912076f commit 7081f7b

3 files changed

Lines changed: 480 additions & 22 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -913,16 +913,29 @@ jobs:
913913
# hash in one namespace (turbo.json `globalEnv`).
914914
#
915915
# ⛔ EXCEPT a shard carrying a package in DTS_READERS, whose tests compile
916-
# against BUILT declarations and go red without them (TS7016). Measured
917-
# on 830a63a, the whole Test Core list under `OS_SKIP_DTS=1`: red only in
916+
# against BUILT declarations and go red without them (TS7016, or ENOENT
917+
# on a `dist/*.d.ts`). Measured on 830a63a, the whole Test Core list under
918+
# `OS_SKIP_DTS=1`, re-measured for the CLI on 43731031 (#25870): red only in
918919
# @objectstack/cli published-subpath-console/-hook-body.pin (its
919920
# own dist/*.d.ts), scaffold-emission-typechecks,
920-
# scaffold-test-script-passes (spec's .d.ts)
921+
# scaffold-test-script-passes,
922+
# create-in-repo-scaffold-typechecks (spec's .d.ts)
921923
# @objectstack/metadata typescript-serializer-annotation (spec's .d.ts)
922-
# A new test that reads a built `.d.ts` reds the same loud way on a
923-
# skipping shard: add its package here. Every other package's counts
924-
# matched the DTS-on run but one: spec's root-entry-type-nameability
925-
# pin skips (by its own design) and runs in lint.yml's built-dist step.
924+
# Every other package's counts matched the DTS-on run but one: spec's
925+
# root-entry-type-nameability pin skips (by its own design) and runs in
926+
# lint.yml's built-dist step.
927+
#
928+
# ⛔ A SLICED reader keeps declarations on ONE slice, DTS_READER_SLICE
929+
# (#25870). The CLI's vitest config pins its reader files into slice 1/n
930+
# (DECLARATION_READER_FILES, packages/cli/vitest.config.ts), so its other
931+
# slices read no `.d.ts` and skip them like any other shard; the
932+
# partitioner charges slice 1/n the closure and puts the whole readers
933+
# here on its shard (DECLARATION_READER_SLICES / DECLARATION_READER_PACKAGES,
934+
# scripts/partition-test-shards.mjs, whose self-test reads this step back).
935+
# A whole reader, or the CLI unsliced, keeps them as before. A new test
936+
# that reads a built `.d.ts` reds the same loud way on a skipping shard:
937+
# add its package here, or, for the CLI, its file to
938+
# DECLARATION_READER_FILES.
926939
#
927940
# Local cold closure, 65 packages, 4 cores, `--force`: 5m59s with
928941
# declarations, 1m24s without. The cost is replay: a skipping shard
@@ -931,10 +944,14 @@ jobs:
931944
- name: Choose this shard's declaration build
932945
run: |
933946
DTS_READERS='@objectstack/cli @objectstack/metadata'
947+
DTS_READER_SLICE='1'
934948
KEEP=""
935-
while read -r PKG _; do
949+
while read -r PKG SLICE; do
936950
for R in $DTS_READERS; do
937-
[ "$PKG" = "$R" ] && KEEP="$KEEP $PKG"
951+
[ "$PKG" = "$R" ] || continue
952+
case "$SLICE" in
953+
''|"$DTS_READER_SLICE"/*) KEEP="$KEEP $PKG${SLICE:+ $SLICE}" ;;
954+
esac
938955
done
939956
done < "$RUNNER_TEMP/shard-packages.txt"
940957
if [ -n "$KEEP" ]; then

‎packages/cli/vitest.config.ts‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,8 @@
643643
// `node_modules` exclusion: an exact-path list matches nothing it does not name.
644644
import { defineConfig } from 'vitest/config';
645645
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';
647648
import {
648649
runFilterPreflight,
649650
runProjectCliOverridePreflight,
@@ -740,6 +741,103 @@ runProjectCliOverridePreflight({
740741
// lives at its use site in the `test` block below, not in a helper binding
741742
// that could outlive the spread.
742743

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+
743841
// #23181 slice 14 — THE OCLIF MANIFEST SETUP. One `globalSetup` module writes
744842
// `oclif.manifest.json` from source when the `integration` project runs (the
745843
// tier every CLI-spawning file is derived into) and removes it at teardown, so
@@ -869,6 +967,10 @@ export default defineConfig({
869967
// The file-level slice, when Test Core runs one (#19278) — see the section
870968
// above `export default` for why it is spread and typed this way.
871969
...({ 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 },
872974
// A late console.* must not redden a green suite (#10374): vitest's worker
873975
// forwards console output over RPC and discards the promise, and a write
874976
// landing after teardown's rpcDone() snapshot is rejected into an unhandled

0 commit comments

Comments
 (0)