Skip to content

test: share one mock Toolchain between the build-script tests - #37817

Closed
robobun wants to merge 1 commit into
mainfrom
farm/7e32bb4f/shared-mock-toolchain
Closed

robobun wants to merge 1 commit into
mainfrom
farm/7e32bb4f/shared-mock-toolchain

Conversation

@robobun

@robobun robobun commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Fix

  • One shared helper in test/_util/: a unix fixture, plus a windows-cross variant that overrides it, so a new field has exactly one place to go. The five local copies are deleted.
  • A new source-lints test checks that both fixtures have exactly the properties interface Toolchain declares. It runs on every PR touching scripts/build/** or test/_util/**, so the next added field fails CI until the mock follows; dropping hostCc / hostCxx from the helper makes it fail.
  • Deriving the windows fixture from the unix one moves a few pass-through values (hostCc / hostCxx, clangResourceDir, ranlib / dsymutil / mt); every field resolveConfig() derives from them comes out identical, and no assertion reads the moved ones.
  • Verification is unit tests only: the six files pass under released bun and bun bd test, and a narrowed tsc run over the touched files is clean.

Background

  • Toolchain (scripts/build/config.ts) is the record of resolved tool paths (clang, lld, ar, cmake, ...) that resolveConfig() turns, together with os / arch / build type, into the full build Config. It only records the paths, so tests pass fakes and run without LLVM.
  • For a windows target the resolver switches to the MSVC-flavoured tools (clang-cl, llvm-lib, lld-link, ...) but keeps plain clang / clang++ as host compilers, hence the second fixture.
  • test/internal/source-lints/ holds tests a GitHub workflow runs with released bun on a bare checkout when certain paths change; they may only import built-ins, relative paths, harness and _util/*.
  • Nothing type-checks the test tree in CI, so a typed literal missing a field is only caught at runtime.
Original description

What

The build-script tests under test/internal/ each carried a private copy of a fake Toolchain for resolveConfig(): build-debug-info-flags, build-post-link-ordering, macos-cross-config, source-lints/windows-cross-config and source-lints/webkit-prebuilt-url (#36139 added the fourth copy and left the dedupe as a follow-up). This replaces them with one helper, test/_util/mock-toolchain.ts, and adds a source-lints test that keeps it in sync with the interface.

Why

The copies had already drifted. Toolchain.hostCc / hostCxx were added in #31300; the macos-cross-config and windows-cross-config copies never got them, so those two files fail to type-check against Toolchain:

internal/macos-cross-config.test.ts(29,3): error TS2322: Type '{ cc: string; ... }' is not assignable to type 'Toolchain'.
  Property 'hostCc' is optional in type '{ ... }' but required in type 'Toolchain'.
internal/source-lints/windows-cross-config.test.ts(24,3): error TS2322: ... Property 'hostCc' is optional ... but required in type 'Toolchain'.

(tsc --noEmit with a config extending test/tsconfig.json and including just these files.) Nobody noticed because the test tree is not type-checked in CI. The same fan-out shows up in open PRs: #34299 (LLVM 22) bumps clangVersion in three of the five copies, and #37575 adds what would be a sixth copy; both can use the shared helper after a rebase.

Change

  • test/_util/mock-toolchain.ts: mockToolchain(overrides?) is the unix fixture (identical values to the three copies that were up to date). mockWindowsCrossToolchain(overrides?) layers the MSVC-flavored family (clang-cl, llvm-lib, lld-link, llvm-rc, llvm-mt, nasm) on top of it instead of being a second full literal, so a new Toolchain field has exactly one place to go. test/_util/ is already a trigger path of the source-lints workflow, and _util/* already resolves there through test/harness.ts.
  • The five test files import the helper; the local bodies are deleted (-185 lines). windows-cross-config's linux case uses mockToolchain() directly instead of patching cc/cxx/ld onto the windows copy.
  • test/internal/source-lints/mock-toolchain.test.ts: asserts Object.keys() of both fixtures equals the property list parsed out of export interface Toolchain in scripts/build/config.ts. This is the runtime stand-in for the type check CI does not run; the source-lints workflow runs it on every PR touching scripts/build/** or test/_util/**, so the next added field fails there in seconds unless the mock is updated. With hostCc/hostCxx removed from the helper (the drift above) it fails with Expected - 2 naming the two fields.

Deriving the windows fixture from the unix one changes a few of its values compared with the old windows copy. Resolving the windows x64/arm64, lto/non-lto, ci/local shapes the tests use with both versions and diffing the resulting Configs, the fields that move are hostCc/hostCxx (clang-cl -> clang/clang++, which is what resolveLlvmToolchain() actually returns for a windows target on a unix host), clangResourceDir (now undefined, as tools.ts skips the probe for msvc targets), and ranlib/dsymutil/mt (now defined, as they are on a linux host). All of them are pass-through fields; computeFlags, webkit.source, rustTarget, ld, strip and msvcLinker come out identical, and no assertion in the file reads the moved fields.

Verification

  • bun test (released bun, as the source-lints workflow runs it) and bun bd test on the six files: 43 pass.
  • bun test test/internal/source-lints/: 83 pass across 19 files.
  • The narrowed tsc run above reports no errors in any of the touched files after the change.

Five test files under test/internal each carried their own copy of a fake
Toolchain for resolveConfig(). Two of them (macos-cross-config,
windows-cross-config) predate Toolchain.hostCc/hostCxx and no longer
type-check against the interface; an LLVM bump has to edit all five.

Move the mock to test/_util/mock-toolchain.ts: mockToolchain() is the
unix variant, mockWindowsCrossToolchain() layers the MSVC-flavored tool
family on top of it. The windows variant now also carries the plain
clang/clang++ host compilers that tools.ts resolves for a windows
cross-compile.

The test tree is not type-checked in CI, so add a source-lints test that
pins the mocks' keys to the properties declared by `interface Toolchain`
at runtime; it runs on every PR that touches scripts/build/ or
test/_util/.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 27 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8bf79695-497c-464b-bac2-5c75d51fcec9

📥 Commits

Reviewing files that changed from the base of the PR and between 9a543cc and 7217904.

📒 Files selected for processing (8)
  • test/_util/mock-toolchain.ts
  • test/internal/build-debug-info-flags.test.ts
  • test/internal/build-post-link-ordering.test.ts
  • test/internal/macos-cross-config.test.ts
  • test/internal/source-lints/README.md
  • test/internal/source-lints/mock-toolchain.test.ts
  • test/internal/source-lints/webkit-prebuilt-url.test.ts
  • test/internal/source-lints/windows-cross-config.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for review.

Reproduced the drift with a narrowed tsc --noEmit over the five files (TS2322 in macos-cross-config.test.ts and windows-cross-config.test.ts, missing hostCc/hostCxx); after this change the same run reports nothing in the touched files. The six test files pass on both the released bun (how the source-lints workflow runs them) and a debug build, and the new mock-toolchain.test.ts fails when the drift is reintroduced in the helper.

The automated review above raised nothing to change. A further self-review pass is in progress; this comment will be updated if it turns up anything.

Open PRs that touch these copies (#34299, #37575) will need a small rebase onto the shared helper; noted in the description.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — mechanical test-infrastructure dedup, no production code touched.

What was reviewed:

  • Confirmed the shared mockToolchain() literal matches all 29 fields of export interface Toolchain in scripts/build/config.ts, and that the three up-to-date copies it replaces were byte-identical to it.
  • Verified _util/* is already mapped in test/tsconfig.json and test/_util/** is already a trigger path in .github/workflows/source-lints.yml, so the new import path and the sync test both work in the source-lints lane.
  • Checked that the windows fixture's inherited-from-unix values (strip, ld64Lld, ranlib, etc.) aren't read by any assertion in windows-cross-config.test.ts — only cfg.ld and cfg.msvcLinker are asserted, and toolchain.msvcLinker was already undefined in the old copy.
Extended reasoning...

Overview

This PR consolidates five duplicated mockToolchain() fixtures across test/internal/ build-script tests into a single shared helper at test/_util/mock-toolchain.ts, and adds a source-lints test (mock-toolchain.test.ts) that pins the helper's keys to the property list parsed from export interface Toolchain in scripts/build/config.ts. Net -185 lines. All eight touched files are test infrastructure; no runtime, build-script, or CI-workflow code changes.

Security risks

None. This is a test-fixture refactor. No user-facing surface, no parsing of untrusted input, no auth/crypto/permissions.

Level of scrutiny

Low. The change is a textbook "the second time a multi-line block appears, extract a named helper" dedup that the repo's own review guidelines call for. The five deleted copies had already drifted (two were missing hostCc/hostCxx and failed tsc), which is exactly the failure mode a shared fixture prevents. I checked the Toolchain interface at scripts/build/config.ts:397-483 against the new literal — all 29 fields present with matching types. The source-lints regex (^\\s+(\\w+)\\??: over the comment-stripped interface body) correctly extracts every field of the current interface shape and throws on "interface not found", so it fails loudly rather than vacuously if the file is reshaped.

Other factors

  • _util/* is already a paths entry in test/tsconfig.json:18 and test/_util/** is already a trigger path in .github/workflows/source-lints.yml, so no new plumbing is needed and the sync test runs where the PR says it will.
  • The one semantic wrinkle is that mockWindowsCrossToolchain() now derives from the unix base instead of being a full literal, so a handful of fields the old windows copy set to undefined (ranlib, ld64Lld, dsymutil) or to different paths (strip, llvmStrip) now inherit unix values. The PR description addresses this by diffing the resolved Configs across every shape the file exercises; I additionally grepped the file for assertions on those fields and found none — only cfg.ld and cfg.msvcLinker are asserted, both of which are set explicitly (ld) or were already undefined in the old toolchain (msvcLinker, derived by resolveConfig).
  • Verification section reports 43 passing tests across the six files under both released bun and bun bd, plus the full source-lints/ suite (83 tests), plus a clean narrowed tsc run.

@robobun

robobun commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 10:51 AM PT - Aug 12th, 2026

✅ @robobun, your commit 7217904c40dd9db7aafb6faca0b73fe352da63a6 passed in Build #93134! 🎉


🧪   To try this PR locally:

bunx bun-pr 37817

That installs a local version of the PR into your bun-37817 executable, so you can run:

bun-37817 --bun

@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has been inactive for more than 90 days.

@github-actions github-actions Bot closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant