Skip to content

perf(ts): add type views for type-only actions - #404

Merged
aronstrandberg merged 7 commits into
mainfrom
aronstrandberg/type-views
Dec 2, 2026
Merged

aronstrandberg merged 7 commits into
mainfrom
aronstrandberg/type-views

Conversation

@aronstrandberg

@aronstrandberg aronstrandberg commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

TsEmit, TsgoCheck, TsgoDeclare and TsLint now stage a type view of each npm package, the files TypeScript's resolution reads, instead of the whole tree. Over the lovable monorepo's 1,058 programs at today's main, TsgoCheck and TsLint stage a third fewer files and spend 10% less worker time, with every emitted declaration byte-identical. Runtime actions keep the trees. type_views = False on a target, or --//ts:type_views=false for a build, restores them for the type actions too.

Claude Opus 5.5, low

Why. A type check reads package.json, JSON, .ts and declarations. It never opens a source map, a stylesheet, an image, or a .js with its declaration beside it, yet each TsgoCheck and TsLint stages all of them for every package in its closure. On the lovable monorepo's web programs those were about a third of the input files, and every one is a link a Buildbarn worker makes for every action.

What a view holds. tsaction typeview copies a store tree's package.json, every .json, .ts, .tsx, .mts and .cts (declarations included), and JavaScript only where TypeScript tries no declaration in its place: .js and .jsx without a .d.ts, .mjs without a .d.mts, .cjs without a .d.cts. One NpmTypeView action per store target writes it. Bazel builds it only when a type action stages it.

Layout. Every node_modules/ path gets a twin at the same path under node_modules.types/, so the views' lookup order is the trees':

flowchart LR
  subgraph full["runtime actions"]
    I1["web/node_modules/zod"] --> T1["node_modules/.pnpm/zod@3/node_modules/zod"]
  end
  subgraph views["type actions"]
    I2["web/node_modules.types/node_modules/zod"] --> T2["node_modules.types/node_modules/.pnpm/zod@3/node_modules/zod"]
  end
  T1 -- "NpmTypeView" --> T2
Loading

Edge links, hoist links (node_modules.types/node_modules/.pnpm/node_modules) and importer links each have a twin into the views. NpmStoreInfo.view/view_links/view_transitive, NpmLinkInfo.view_link, NpmHoistInfo.view_members and TsInfo.npm_view_files carry them. compile_program hands the type actions npm_view_files and the importers' node_modules.types/node_modules directories, and the ownership manifest names the views.

tsaction. Three changes, all only when an importer is a twin:

  • importerDir maps <dir>/node_modules.types/node_modules to <dir>.
  • The written tsconfig sits in the output tree, and TypeScript resolves its types entries by walking up from there. So the program root's bazel-out becomes real directories down to each importer's node_modules, linked to its twin, with every other entry linked as before. Without this, every types: ["node"] fails with TS2688.
  • On the full layout, a source outside every importer's package resolves by walking up past the program root into the output tree. For example, //npm-packages/slides's test compiles templates/slides_tanstack_ts sources. On views, the root importer's twin in the program root is a merge of the chain, with the root's own names first.

TsLint. It stages the views, and each importer link the lint configuration's data names is laid over them as its tree (tsaction tsgo -runtime_link), where the program root holds that importer, so the configuration's imports run under Node from the trees. Putting TsLint on views matters for time, not only for files: a target's TsgoCheck and TsLint then stage the same input root, and the worker reuses it. With TsLint on the trees and TsgoCheck on views, both lost that reuse and the pair ran about 20% slower than with views off.

Compatibility. A TsInfo built without npm_view_files makes its dependents' programs read the trees, so previous_shape_provider still analyzes. Analysis declares the views and twin links whatever the flag, which roughly doubles a lockfile's symlink artifacts.

Escape hatches. type_views = False on a ts_compile or ts_test stages the trees in that program's type actions only; its dependents keep their own choice, and no configuration changes, so the stores are analyzed once. --//ts:type_views=false does it for every program, for comparisons and rollback. Analysis tests cover both.

Benchmark. The lovable monorepo at main (821ff4ed, which has //:oxlint_config_node_modules in ts.lint's data) builds every ts_compile and ts_test (1,058 programs) against this branch through --override_module. TsgoCheck, TsLint and TsgoDeclare are forced to execute (no-cache, --use_action_cache=false) on Buildbarn, in the order off, on, off. Times are worker spans from Buildbarn traces, summed over the actions, and the off column averages the two off runs.

Views off Views on Change
TsgoCheck input files 28.2M 19.0M −33%
TsLint input files 28.4M 19.3M −32%
TsgoCheck input fetch 2,075 s 1,792 s −14%
TsgoCheck execution 1,280 s 1,251 s −2%
TsLint input fetch 2,141 s 1,849 s −14%
TsLint execution 1,254 s 1,158 s −8%
TsgoCheck + TsLint 6,749 s 6,050 s −10%
TsgoDeclare input fetch 16.8 s 7.1 s −58%

The execution rows move with cluster load (the two off runs differ by 12% there); the input files and input fetch are the change's own effect.

Verification.

  • All 1,332 declarations the monorepo's main emits hash identically in the three runs, and no type action fails with views on.
  • bazel test --config=fast //... passes, apart from targets this NixOS machine cannot build (no /bin/bash, protoc, Cargo build scripts), which fail with views off too.
  • New tests: TestTypeViewKeepsWhatResolutionReads pins the filter, links included, and TestBinRelative pins the twin importer paths. TestRuntimeLink… pin where a lint configuration's links land, the lint analysis tests that TsLint lays them over the views, and //tests/npm/trees:app_chain_test and //tests/npm:app_trees_flag_chain_test that the attribute and the flag stage only trees. The analysis tests that pin the importer chain, the staged inputs and the ownership manifest now expect the views.
  • An adversarial review found four issues before this version, all fixed: TsLint and Node configurations, the walk reaching the root importer's twins, the .mjs/.cjs pairing, and the old TsInfo shape.

@aronstrandberg
aronstrandberg requested a review from mikn as a code owner November 20, 2026 23:09
@aronstrandberg aronstrandberg changed the title perf(ts): stage npm type views in the type actions perf(ts): add type views for type-only actions Nov 20, 2026
Each store target declares a view of its tree with the files TypeScript's
resolution reads, at the same path under node_modules.types/, with twins of
the edge, hoist and importer links. TsEmit, TsgoCheck, TsgoDeclare and TsLint
stage the views; a TsLint whose lint configuration names a node_modules, and
runtime actions, stage the trees. --//ts:type_views=false restores the trees.
@aronstrandberg
aronstrandberg force-pushed the aronstrandberg/type-views branch from d6d19e8 to defa22a Compare November 30, 2026 07:55

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread npm/private/store.bzl Resolved
types_path took only paths below node_modules/, and the public hoist's
directory is node_modules itself, so a lockfile that hoists a workspace member
publicly failed analysis.

@aherrmann-lov aherrmann-lov left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The benchmark predates a Lovable lint change that turns views off for TsLint, so we don't know this PR's effect on Lovable yet. Please re-run the benchmark against current Lovable before we bump.

Why. TsLint stages views only when the lint configuration's data names no node_modules. Lovable's ts.lint names //:oxlint_config_node_modules since lovablelabs/lovable#176469, merged 2026-11-26. Before that, its data had no node_modules, which is the configuration the table measures. On today's Lovable, TsgoCheck stages views and TsLint stages the trees, the mix this PR's description measured at about 20% slower for the pair than views off.

Evidence. bazel aquery of //packages/emails:emails in Lovable at origin/main, at the current pin and with --override_module=rules_typescript= this PR's head (ef10012e):

Current pin This PR
TsgoCheck npm inputs 192 trees and links 198 views and view links
TsLint npm inputs 213 trees and links 213 trees and links

So the −32% TsLint input files and the −14% TsgoCheck + TsLint time can't come from this code on Lovable today. The result may even be a slowdown.

Options. Either keep the program's npm closure on views and stage only lint.data's packages as trees, so TsgoCheck and TsLint share an input root again, or change Lovable so its oxlint configuration doesn't need a node_modules in data. Either way, the numbers in the description need a fresh run.

via Claude Code, Claude Opus 5.5

@aherrmann-lov aherrmann-lov left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What is --//ts:type_views for? The answer decides its shape.

The off path is untested. Views are the default, and nothing in tests/, CI or the .bazelrc files sets --//ts:type_views=false. So the trees path for type actions, which the flag promises to restore, runs nowhere, and it will rot unnoticed. If we keep the flag, please add at least one test lane or analysis test that runs with it off.

As a build-wide switch, it is a coarse escape hatch. scope = "target" resets the flag on exec transitions; it is still one value per build. If one package's resolution needs a file its view drops, the only fix is views off everywhere, which gives up the whole saving. That is fine for an A/B benchmark or an emergency rollback, but not as a per-package escape hatch.

Please don't make it per target with a transition. A transition on this flag puts the transitioned subgraph in its own configuration, so every npm store tree, view and link under it is analyzed and built a second time, though the stores never read the flag.

If per-target control is the goal, an attribute works. I tried a type_views = attr.bool(default = True) on ts_compile/ts_test, ANDed into the existing check at ts/private/rules/ts_compile.bzl:632 (two lines). In Lovable, with type_views = False on //packages/applocal-mcp:applocal-mcp:

Target TsgoCheck TsLint
:applocal-mcp (type_views = False) trees trees
:applocal-mcp_test__native_program (depends on it) views trees (see the lint comment)

All 2,585 target-config stores in the test's closure stay in one configuration; the only other configuration holds the 7 exec-config lint and format tool stores, as without the attribute. bazel build of both targets, validations included, passes. Each program already falls back on its own (None not in dep_npm_view_sets), so one target's choice does not change what its dependents stage.

Analysis cost, for reference. In Lovable (bazel build --nobuild //web/... //packages/..., fresh server, this PR's base against its head, two runs each), retained heap after GC grows from 1,072 MB to 1,088 MB (+1.4%). The action graph grows from 67,855 to 91,410 actions: 5,200 NpmTypeView and 18,355 more UnresolvedSymlink. I saw no analysis-time regression. Declaring views whatever the flag says is cheap enough not to gate.

via Claude Code, Claude Opus 5.5

The lint configuration's data links are laid over the root importer's twin
with -runtime_link, so its imports run from the trees while the program stays
on views and shares TsgoCheck's input root. Add a type_views attribute for a
per-program escape hatch, and test the trees path, by attribute and by flag.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9e5975e. Configure here.

Comment thread ts/tools/tsaction/tsgo.go Resolved
The chain's last importer is laid at the program root's node_modules, so its
runtime links go there, not at their bin-relative path.
@aronstrandberg

Copy link
Copy Markdown
Member Author

@aherrmann-lov thanks, both points held. Changes are in 9e5975e4 and d37fff05.

Lint on today's Lovable. Right: since lovablelabs/lovable#176469 TsLint fell back to the trees, the slow mix. I took your first option. TsLint now always stages the views, and each importer link the lint configuration's data names is laid over them as its tree with a new tsaction tsgo -runtime_link. A link of the chain's last importer goes to the program root's node_modules, where tsaction lays that importer (Bugbot caught the nested-root case). On //packages/emails:emails the TsLint action now names the .types importers plus -runtime_link=bazel-out/k8-fastbuild/bin/node_modules/@cloudflare/oxlint-config, and the config loads.

Fresh benchmark on Lovable main (821ff4ed). 1,058 programs, forced to execute, off/on/off, all 1,332 emitted declarations identical, no failures. TsgoCheck and TsLint stage 33% and 32% fewer files, input fetch falls 14% for each, and the pair's worker time falls 10% (6,749 s to 6,050 s). The table in the description is updated.

The flag. I kept --//ts:type_views as a build-wide switch for comparisons and rollback, and added your type_views attribute as the per-target escape hatch, with no transition. The off path is now tested both ways: //tests/npm/trees:app_chain_test (attribute) and //tests/npm:app_trees_flag_chain_test (flag) check that TsgoCheck and TsLint name only tree importers and stage no view.

@aherrmann-lov aherrmann-lov left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Both points are addressed. Approving.

Lint. On Lovable main, //packages/emails:emails's TsLint now stages the same 198 view artifacts as its TsgoCheck, plus 21 tree artifacts for @cloudflare/oxlint-config's closure through -runtime_link. Before, it staged 213 trees. The fresh off/on/off run on 1,058 programs replaces the stale numbers.

Flag and attribute. The build-wide flag for comparisons and rollback, the type_views attribute as the per-target escape hatch with no transition, and the two analysis tests for the off path settle my questions.

Checked. The four new and changed tsaction tests pass. The 38 other tsaction failures under a plain go test are the same set on main (25fb6adc), so they come from running outside Bazel. CI is green apart from the macOS unit-test lane, still running.

Nit, non-blocking. In ts/tools/tsaction/tsgo.go, linkAt's doc comment ("linkAt links the exec root's rel into root at rel, its directories real.") now opens typeViewHoles' doc comment, and linkAt has none. Moving that line back above func linkAt fixes it.

via Claude Code, Claude Opus 5.5

@aronstrandberg

Copy link
Copy Markdown
Member Author

@aherrmann-lov fixed the nit in 1f940dc: linkAt's doc comment is back above func linkAt, and typeViewHoles opens with its own.

@aronstrandberg
aronstrandberg merged commit 21f4290 into main Dec 2, 2026
17 checks passed
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.

2 participants