Skip to content

ci(protect-ffi): stop the test typecheck rewriting lib/ - #1909

Merged
auxesis merged 8 commits into
docs/ci-decision-gapsfrom
ci/protect-ffi-test-no-emit
Nov 26, 2026
Merged

auxesis merged 8 commits into
docs/ci-decision-gapsfrom
ci/protect-ffi-test-no-emit

Conversation

@auxesis

@auxesis auxesis commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This fixes a race that sometimes failed the Test JS job for a reason outside the code under test. The root pnpm test command runs turbo test (turbo is the task runner this repository uses), and turbo runs many package tasks at once in one checkout.

The test script of @cipherstash/protect-ffi (the native encryption module) typechecked with a plain tsc, and plain tsc rewrites every file in the package's lib/ folder. At the same time, the builds of @cipherstash/stack and other packages read the type files in that folder. A file that tsc is rewriting is empty for a moment, so a build sometimes saw no exports and failed with TS2305. Now the test typechecks without writing files, and turbo builds lib/ before the test runs.

Stack

This is pull request 4 of 4 in stack 1735. Below it are #1416 (merged), #1419 and #1845. Merging this pull request also merges #1419 and #1845 if they have not merged yet.

Changes

  • languages/typescript/packages/protect-ffi/package.json: test:typecheck now runs tsc --noEmit instead of plain tsc, so the test never writes lib/.
  • turbo.json: a new @cipherstash/protect-ffi#test task depends on the package's own build as well as its dependencies' builds. The unit tests read the built lib/index.cjs, and the test no longer builds it. The other settings copy the generic test task.
  • scripts/__tests__/protect-ffi-test-no-emit.test.mjs (new): checks that no tsc reachable from protect-ffi's test script writes files, and that turbo runs that test after its own build. It follows script calls with or without run and after flags, finds tsc by path, version or quoting, and reads tsconfig files as JSONC. Its helpers have their own tests.
  • languages/typescript/packages/protect-ffi/src/nativeLoading.test.ts: the header comment and the error message for a missing lib/ no longer say that the test script builds lib/. The suite now also stops when a compiled source under src/ is newer than lib/index.cjs, and says to run build.
  • src/nativeLoading.test.ts, src/lintWiring.test.ts and .github/workflows/tests.yml: five comments now quote the current root test filter.
  • languages/typescript/packages/protect-ffi/README.md: says that pnpm test in the package needs pnpm run build first, and that root pnpm test does this on its own. The fresh-clone promise now names root pnpm test only.

Verification

  • Ran the new test on the tree before the fix, with node_modules/.bin/vitest run --config scripts/vitest.config.mjs scripts/__tests__/protect-ffi-test-no-emit.test.mjs: 3 of 3 failed. It reported test:typecheck: tsc as a command that writes files, and no @cipherstash/protect-ffi#test task in turbo.json.
  • Ran the same command after the fix: 3 of 3 passed.
  • Ran the full scripts suite, node_modules/.bin/vitest run --config scripts/vitest.config.mjs: 86 files passed, 1442 tests passed, 3 skipped.
  • Each review fix to the guard's helpers has a test that failed before the fix and passes after it.
  • Built lib/ with tsc -p languages/typescript/packages/protect-ffi, then ran protect-ffi's vitest run: 9 files passed, 108 tests passed. After touching src/index.cts, the suite stopped with the new stale-lib/ message.
  • Ran tsc --noEmit and tsc -p tsconfig.test.json in protect-ffi: both exited 0. The modification times and sizes of lib/types.d.ts and lib/index.cjs were the same before and after.
  • Checked that tsc --noEmit still reports declaration errors when declaration: true is set. On TypeScript 5.9.3, a test project with export const C = class { private x = 1 } reported TS4094 under tsc --noEmit, the same as under plain tsc. So the typecheck catches the same errors as before, and build still runs plain tsc.
  • Ran node_modules/.bin/turbo run test --filter './languages/typescript/packages/**' --filter './packages/**' --dry=json: @cipherstash/protect-ffi#test now depends on @cipherstash/protect-ffi#build, plus the builds of the six platform packages.
  • Ran node_modules/.bin/biome check on the changed files: no problems.
  • Did not run root pnpm test, because pnpm is not installed on the machine where this change was made. CI runs it.

Related

Refs #1845: Test JS failed this way in run 45801077255, job "Run Tests (Node 22)".

Review notes

  • The race depends on timing. In a local test that ran tsc 10 times while reading lib/types.d.ts in a loop, 312 of 235,725 reads found the file empty. So green runs cannot show whether the race is present. The new test checks the two rules that close it.
  • Running pnpm test inside languages/typescript/packages/protect-ffi without a build first, or after editing a source, now fails at once, with a message that says to run build. Before this change, the test built lib/ itself. Every CI job that runs the package's scripts directly builds lib/ first, through .github/actions/build-ffi-binding.
  • There is no changeset. The change is to test scripts, turbo settings, and contributor notes. None of it changes what the published package does.

Summary by CodeRabbit

  • Documentation
    • Clarified that tests use existing build output, TypeScript checks do not generate files, and the package must be built before testing. The root test workflow builds prerequisites automatically.
    • Clarified that wasm typechecking is not included in the root test command because a fresh checkout lacks the required build output.
  • Chores
    • Added checks for build output freshness and safeguards to keep test commands and workflow configuration aligned.

Root `pnpm test` runs `turbo test` over every package at once. protect-ffi's
`test:typecheck` began with a plain `tsc`. That command rewrites every file
in `lib/`. The builds of @cipherstash/stack and other packages read
`lib/*.d.*` at the same time, and nothing in the turbo graph ordered the
two. A file tsc is rewriting is briefly empty, so a build sometimes read an
empty `lib/types.d.ts` and failed with TS2305. That failed Test JS in run
45801077255 on #1845.

The typecheck now runs `tsc --noEmit`, so `test` never writes `lib/`.
TypeScript 5.9 still reports declaration errors such as TS4094 under
`--noEmit`, so the check loses nothing. The unit tests still need the
emitted `lib/index.cjs`, so turbo.json gives protect-ffi's `test` its own
task that depends on the package's `build`.

A new scripts test checks both rules: no tsc reachable from protect-ffi's
`test` emits files, and turbo runs that `test` after its own `build`.

Refs #1845
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The protect-ffi typecheck now uses tsc --noEmit. Turbo runs dependency and package builds before its test task. Documentation and tests describe and check the build prerequisite and generated output.

Changes: protect-ffi test execution

Layer / File(s) Summary
No-emit typecheck and Turbo test task
languages/typescript/packages/protect-ffi/package.json, turbo.json
The typecheck command adds --noEmit. The package-specific Turbo test task depends on build and ^build, tracks its inputs and environment variables, and disables caching.
Test task configuration checks
scripts/__tests__/protect-ffi-test-no-emit.test.mjs
New checks follow reachable scripts, identify TypeScript compiler commands, determine whether they emit files, and verify the Turbo task dependencies and settings. Fixtures cover script parsing and JSONC configuration.
Generated-output checks and test documentation
languages/typescript/packages/protect-ffi/src/nativeLoading.test.ts, languages/typescript/packages/protect-ffi/README.md, languages/typescript/packages/protect-ffi/src/lintWiring.test.ts, .github/workflows/tests.yml
The native-loading test checks the generated entry and stale compiled sources. The README and test comments describe build prerequisites and recursive test filters. The workflow comment uses the recursive TypeScript package filter.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Other

Sequence Diagram(s)

sequenceDiagram
  participant Turbo
  participant ProtectFFIBuild
  participant ProtectFFITest
  participant NativeLoadingTest
  Turbo->>ProtectFFIBuild: Run package and dependency builds
  ProtectFFIBuild-->>Turbo: Produce lib/index.cjs
  Turbo->>ProtectFFITest: Run test task
  ProtectFFITest->>NativeLoadingTest: Execute unit tests
  NativeLoadingTest->>NativeLoadingTest: Read lib/index.cjs
Loading

Suggested reviewers: tobyhede, coderdan, cipherstash-agent

Merge Risk: Minimal · up to 3152e

This change switches the protect-ffi typecheck to a no-emit mode and makes Turbo build the package before its tests, which removes an intermittent CI race. No concrete merge-blocking defect was established. One unverified edge case remains: a locally cached build could trip the stale-output check. That case is not shown to affect CI.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the protect-ffi test typecheck no longer rewrites lib/. The changeset switches test:typecheck to tsc --noEmit, adds a protect-ffi#test Turbo task, and adds a regressio…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: the protect-ffi test typecheck no longer rewrites lib/. The changeset switches test:typecheck to tsc --noEmit, adds a protect-ffi#test Turbo task, and adds a regression test. The title uses the ci(protect-ffi) scope and a short, specific phrase. It matches the stated objective of PR #1909.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aba165cf-11cd-4723-af1a-86a5344f49b4
Commits

Reviewing files that changed from the base of the PR and between 9038078 and 3152edc.

Files selected for processing: 5

  • .github/workflows/tests.yml
  • languages/typescript/packages/protect-ffi/README.md
  • languages/typescript/packages/protect-ffi/src/lintWiring.test.ts
  • languages/typescript/packages/protect-ffi/src/nativeLoading.test.ts
  • scripts/__tests__/protect-ffi-test-no-emit.test.mjs
Files skipped from review as they are similar to previous changes: 1

  • languages/typescript/packages/protect-ffi/README.md

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 3152edc

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@auxesis
auxesis added this pull request to stack #1735 November 26, 2026 11:47
@auxesis
auxesis marked this pull request as ready for review November 26, 2026 12:05
@auxesis
auxesis requested a review from a team as a code owner November 26, 2026 12:05

@cipherstash-agent cipherstash-agent 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.

Review attestation

Reviewed 9038078 · posted from Claude Code (scheduled routine) · claude-opus-5-5 · effort medium
Sources: code-review (medium) · claude-opus-5-5; mattpocock-skills:code-review Standards and Spec agents · claude-opus-5-5; review-cross-check verifiers · claude-opus-5-5
Skills: code-review, mattpocock-skills:code-review, company-skills:rust-security (loaded; the diff contains no Rust, so it did not apply), company-skills:review-cross-check

Findings (8): 8 minor. 7 are inline comments and 1 is below. None blocks the race fix. Every finding was checked against 9038078 before posting.

  • languages/typescript/packages/protect-ffi/src/nativeLoading.test.ts:30 (outside the diff, and it predates this PR) minor The header still quotes turbo test --filter './languages/typescript/packages/*'. The root script is now --filter './languages/typescript/packages/**' --filter './packages/**'. The same old quote is at nativeLoading.test.ts:203, src/lintWiring.test.ts:399 and :427, and .github/workflows/tests.yml:132.

Refuted during verification: tsc --noEmit with declaration: true still reports declaration-only errors (TS4094 and TS4058 on TypeScript 5.9.3), so the typecheck catches as much as before. No changeset is needed, because no published file changes.


Generated by Claude Code

The package's test no longer writes lib/, so after a source edit a bare test run asserted on the previous build's JavaScript and passed. The guard now also fails when a compiled source under src/ is newer than lib/index.cjs, and tells the reader to run build.
No CI job runs protect-ffi's test or test:unit directly. CI reaches them only through root pnpm test, which runs turbo.
The unit tests no longer run tsc. They run vitest against the lib/ that build writes. The fresh-clone promise now names root pnpm test, because the package's own test needs build first.
Root pnpm test now runs turbo test with two filters, './languages/typescript/packages/**' and './packages/**'. Five comments still quoted the old single filter.
tsc accepts comments and trailing commas in a tsconfig. JSON.parse threw a SyntaxError on them, so the guard failed for the wrong reason. It now uses readJsonc, which the file already imports.
The guard looked for the exact word tsc, so ./node_modules/.bin/tsc, npx [email protected], sh -c 'tsc' and (tsc) were missed, and an emitting tsc spelled that way passed. A word now counts as tsc when its last path part, without quotes, parentheses or an @Version, is tsc.
The walk followed only 'pnpm run X' and 'npm run X'. pnpm also runs a script as 'pnpm X', and after flags such as -s or --silent, so an emitting tsc reached that way passed the guard. The walk now follows those calls, and only names that are scripts.
@auxesis

auxesis commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

On the review-body finding about the old root test filter (nativeLoading.test.ts:30):

Fixed in 011934d.

All five comments now quote turbo test --filter './languages/typescript/packages/**' --filter './packages/**': nativeLoading.test.ts (2), src/lintWiring.test.ts (2) and .github/workflows/tests.yml (1). No other copy of the old filter is left in the repository.

@auxesis
auxesis requested a review from calvinbrewer November 26, 2026 14:33
@auxesis
auxesis merged commit 4573f99 into main Nov 26, 2026
41 checks passed
@auxesis
auxesis deleted the ci/protect-ffi-test-no-emit branch November 26, 2026 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants