Automatic analysis of libraries by default: report everything found, with its kind #52

Merged
sionzee merged 3 commits from evobug/auto-library-knowledge into main 2026-11-16 17:55:24 +01:00
Owner

What

Automatic analysis of libraries is now the default way SAFE-TS knows what library calls throw or reject. Every package the program imports (resolving into node_modules, listed in package.json or not) is analyzed from its JavaScript once per installed version, and everything found is reported, with its kind in the message:

error[unhandled-failure]: `User.parse()` (zod, found in its code: contract) can throw `$ZodAsyncError | ZodError`, but the failure is not handled
error[unhandled-failure]: `new WebSocket(url).send()` (ws, found in its code: builtin) can throw `Error`, but the failure is not handled
error[unhandled-failure]: `z.string()` (zod, found in its code: argument check) can throw `Error`, but the failure is not handled
  • Levels: "infer": "all" (default), "real" (everything except argument checks), "documented" (the old behaviour: found and @throws), false. { "level": "real", "exclude": [...] } combines both. Anything else warns.
  • Kinds: contract / internal / builtin / dynamic, plus a new argument check: the throw's guard only tests the function's own parameters (typeof, instanceof, === undefined, comparisons, .length, Array.isArray, Number.isInteger, ...). A guard that calls something on the argument (isValid(date), re.test(s)) is validation, not an argument check. Through calls: still an argument check when the caller passes its own arguments, builtin when it passes a value it computed, dropped when it passes only constants.
  • Follows more code: functions a factory returns (zod's parse = _parse(ZodRealError) → ZodError), export * / export * as ns, re-exports from and calls into other packages (vue → runtime-dom → runtime-core), CommonJS members and compiled Object.defineProperty(exports, ...), prototype methods, bundlers' class expressions, exported instances. Two precision fixes: closures stored for later no longer count as running in their creator, and catch (e) { throw e } passes the try block's own failures through.
  • Lookup: methods by the receiver's class where visible (new WebSocket(url).send()); on a value returned by a call (z.string().min(1).parse()) by name, only when every method of that name in the package fails. What it can't follow is counted by safe-ts infer, never guessed.
  • Which packages: any import resolving into node_modules. Workspace packages that resolve to project source are project code (as in the project pass) and aren't analyzed.
  • Cache: one file per package, version and level in node_modules/.cache/safe-ts/ (or SAFE_TS_CACHE_DIR; SAFE_TS_CACHE=0 off). Key holds the analyzer version; an entry is re-analyzed when a dependency it followed into changes version; a package linked from outside node_modules is also keyed by a fingerprint of its files (sizes, mtimes).
  • Packs win per member, as before. New manifest field "complete": true on a module entry: automatic mode adds nothing for its specifiers. The zod pack sets it, so with the zod pack nothing changes for zod.
  • safe-ts infer / coverage show the kinds; infer names methods Class#method, takes subpaths, lists followed packages, counts data exports apart.

Why

Before, a failure was only reported when it was both found and documented with @throws, so automatic mode found nothing for most packages (zod, ws, pixi.js, vue).

This changes what existing projects see. CHANGELOG lists it under Changed, with the way back ("infer": "documented").

Measurements (functions/methods analyzed; failures by kind c/i/b/d/arg; exports not analyzable)

Package Before After
zod 4.6.7 148; 1/0/87/2/–; 110 837 (incl. core namespace, schema methods); 43/0/29/0/290; 22
ws 8.23 82; 0/0/19/0/–; 2 57 (aliases deduplicated); 0/0/18/0/3; 0
pixi.js 8.23 1952; 7/0/145/2/–; 208 (+49 data counted as unanalyzable) 2225; 3/0/187/0/55; 24 (+119 data exports counted apart)
vue 3.5 0; –; 1 209; 0/0/0/3/0; 6
axios 1.21 0; –; 18 64; 7/0/10/6/0; 3 (axios.get etc. are made in a loop: not guessed)
date-fns 4.4 289; 15 contract (documented); 2 same; 1

pixi.js's 4 lost "contract" items were getters/setters, which aren't calls.

  • zod: parse, parseAsync, schema.parse() found (ZodError, contract); schema builders (z.object, z.string, z.email, ...) are argument checks. safeParse reports $ZodAsyncError (sync parse of an async schema), real but noisy; the zod pack (complete) hides it.
  • ws vs RTSS's hand-written pack: automatic mode reports the same members with the same class: WebSocket#send/ping/pong → Error, WebSocketServer#handleUpgrade → Error (builtin). On the same program, the RTSS pack itself only catches handleUpgrade with import WebSocket from "ws": it lacks "default": "WebSocket".
  • Timing (release): the first check of a file calling into pixi.js takes ~140 ms (65 ms reading pixi's 737 files), then ~25 ms from the cache. All 8 scratch packages cold: ~330 ms, warm: ~22 ms.
  • RTSS client (read-only copy in /tmp, cache in /tmp): 2532 → 2533 problems; the new one is app.init() (pixi.js, builtin), which rejects when no renderer is available.

Verification

  • cargo nextest run --workspace: 187 passed. New fixture packages in tests/infer/node_modules (factories, namespaces, a dependency, argument checks, stored closures, rethrows, CommonJS prototypes, class expressions, data exports), level fixtures auto/real, auto/documented. The auto fixture now imports its config subdirectories: before, off/ and excluded/ were never checked.
  • cargo clippy -q --workspace --all-targets: nothing new in changed code.
  • npm run build, test:std, typecheck, typecheck:emit, node scripts/docs-typecheck.mjs, test:docs, test:vite: pass.
  • Repo examples: same results as before (demo 22 intended problems, react/vue none).

Docs changed

README.md (section renamed to "Libraries: automatic analysis and feature packs", leading with automatic analysis), docs/guide/libraries.md (intro, automatic mode rewritten: levels, kinds, packages, cache, timings; coverage; safe-ts infer with measurements; complete in the manifest), docs/index.md, docs/README.md, docs/rules/unhandled-failure.md, docs/guide/editor.md, docs/guide/adoption.md (cache), docs/guide/security.md, CHANGELOG.md.

🤖 Generated with Claude Code

## What Automatic analysis of libraries is now the default way SAFE-TS knows what library calls throw or reject. Every package the program imports (resolving into `node_modules`, listed in package.json or not) is analyzed from its JavaScript once per installed version, and **everything found is reported**, with its kind in the message: ``` error[unhandled-failure]: `User.parse()` (zod, found in its code: contract) can throw `$ZodAsyncError | ZodError`, but the failure is not handled error[unhandled-failure]: `new WebSocket(url).send()` (ws, found in its code: builtin) can throw `Error`, but the failure is not handled error[unhandled-failure]: `z.string()` (zod, found in its code: argument check) can throw `Error`, but the failure is not handled ``` - **Levels**: `"infer": "all"` (default), `"real"` (everything except argument checks), `"documented"` (the old behaviour: found and `@throws`), `false`. `{ "level": "real", "exclude": [...] }` combines both. Anything else warns. - **Kinds**: contract / internal / builtin / dynamic, plus a new **argument check**: the throw's guard only tests the function's own parameters (`typeof`, `instanceof`, `=== undefined`, comparisons, `.length`, `Array.isArray`, `Number.isInteger`, ...). A guard that calls something on the argument (`isValid(date)`, `re.test(s)`) is validation, not an argument check. Through calls: still an argument check when the caller passes its own arguments, `builtin` when it passes a value it computed, dropped when it passes only constants. - **Follows more code**: functions a factory returns (zod's `parse = _parse(ZodRealError)` → `ZodError`), `export *` / `export * as ns`, re-exports from and calls into other packages (vue → runtime-dom → runtime-core), CommonJS members and compiled `Object.defineProperty(exports, ...)`, prototype methods, bundlers' class expressions, exported instances. Two precision fixes: closures stored for later no longer count as running in their creator, and `catch (e) { throw e }` passes the try block's own failures through. - **Lookup**: methods by the receiver's class where visible (`new WebSocket(url).send()`); on a value returned by a call (`z.string().min(1).parse()`) by name, only when every method of that name in the package fails. What it can't follow is counted by `safe-ts infer`, never guessed. - **Which packages**: any import resolving into `node_modules`. Workspace packages that resolve to project source are project code (as in the project pass) and aren't analyzed. - **Cache**: one file per package, version and level in `node_modules/.cache/safe-ts/` (or `SAFE_TS_CACHE_DIR`; `SAFE_TS_CACHE=0` off). Key holds the analyzer version; an entry is re-analyzed when a dependency it followed into changes version; a package linked from outside `node_modules` is also keyed by a fingerprint of its files (sizes, mtimes). - **Packs win** per member, as before. New manifest field `"complete": true` on a module entry: automatic mode adds nothing for its specifiers. The zod pack sets it, so with the zod pack nothing changes for zod. - `safe-ts infer` / `coverage` show the kinds; `infer` names methods `Class#method`, takes subpaths, lists followed packages, counts data exports apart. ## Why Before, a failure was only reported when it was both found and documented with `@throws`, so automatic mode found nothing for most packages (zod, ws, pixi.js, vue). This changes what existing projects see. CHANGELOG lists it under **Changed**, with the way back (`"infer": "documented"`). ## Measurements (functions/methods analyzed; failures by kind c/i/b/d/arg; exports not analyzable) | Package | Before | After | |---|---|---| | zod 4.6.7 | 148; 1/0/87/2/–; 110 | 837 (incl. `core` namespace, schema methods); 43/0/29/0/290; 22 | | ws 8.23 | 82; 0/0/19/0/–; 2 | 57 (aliases deduplicated); 0/0/18/0/3; 0 | | pixi.js 8.23 | 1952; 7/0/145/2/–; 208 (+49 data counted as unanalyzable) | 2225; 3/0/187/0/55; 24 (+119 data exports counted apart) | | vue 3.5 | 0; –; 1 | 209; 0/0/0/3/0; 6 | | axios 1.21 | 0; –; 18 | 64; 7/0/10/6/0; 3 (`axios.get` etc. are made in a loop: not guessed) | | date-fns 4.4 | 289; 15 contract (documented); 2 | same; 1 | pixi.js's 4 lost "contract" items were getters/setters, which aren't calls. - **zod**: `parse`, `parseAsync`, `schema.parse()` found (`ZodError`, contract); schema builders (`z.object`, `z.string`, `z.email`, ...) are argument checks. `safeParse` reports `$ZodAsyncError` (sync parse of an async schema), real but noisy; the zod pack (`complete`) hides it. - **ws vs RTSS's hand-written pack**: automatic mode reports the same members with the same class: `WebSocket#send/ping/pong` → `Error`, `WebSocketServer#handleUpgrade` → `Error` (builtin). On the same program, the RTSS pack itself only catches `handleUpgrade` with `import WebSocket from "ws"`: it lacks `"default": "WebSocket"`. - **Timing (release)**: the first `check` of a file calling into pixi.js takes ~140 ms (65 ms reading pixi's 737 files), then ~25 ms from the cache. All 8 scratch packages cold: ~330 ms, warm: ~22 ms. - **RTSS client** (read-only copy in /tmp, cache in /tmp): 2532 → 2533 problems; the new one is `app.init()` (pixi.js, builtin), which rejects when no renderer is available. ## Verification - `cargo nextest run --workspace`: 187 passed. New fixture packages in `tests/infer/node_modules` (factories, namespaces, a dependency, argument checks, stored closures, rethrows, CommonJS prototypes, class expressions, data exports), level fixtures `auto/real`, `auto/documented`. The `auto` fixture now imports its config subdirectories: before, `off/` and `excluded/` were never checked. - `cargo clippy -q --workspace --all-targets`: nothing new in changed code. - `npm run build`, `test:std`, `typecheck`, `typecheck:emit`, `node scripts/docs-typecheck.mjs`, `test:docs`, `test:vite`: pass. - Repo examples: same results as before (demo 22 intended problems, react/vue none). ## Docs changed README.md (section renamed to "Libraries: automatic analysis and feature packs", leading with automatic analysis), docs/guide/libraries.md (intro, automatic mode rewritten: levels, kinds, packages, cache, timings; coverage; `safe-ts infer` with measurements; `complete` in the manifest), docs/index.md, docs/README.md, docs/rules/unhandled-failure.md, docs/guide/editor.md, docs/guide/adoption.md (cache), docs/guide/security.md, CHANGELOG.md. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Automatic analysis of libraries by default: report everything found, with its kind
Some checks failed
Benchmarks / bench (pull_request) Failing after 2m43s
3e6be5735f
Every package the program imports (resolving into node_modules, listed in package.json or
not; workspace packages stay project code) is analyzed from its JavaScript, and what its
functions throw or reject is reported. Before, a failure had to be both found in the code
and documented with @throws, so most packages reported nothing.

- Levels: "infer": "all" (default), "real" (no argument checks), "documented" (the old
  behaviour), false; { "level", "exclude" } combined. Unknown values warn.
- Each finding names its kind in the message: contract, internal, builtin, dynamic, and the
  new argument check (the guard only tests the function's own parameters).
- The analysis follows factories' returned functions, export * / export * as ns, re-exports
  and calls into other packages, CommonJS members and compiled defineProperty exports,
  prototype methods, class expressions, exported instances; stored closures no longer count
  as running in their creator; catch-and-rethrow passes the try block's failures through.
- Method calls are matched by the receiver's class where visible, else by name only when
  every method of that name fails; nothing is guessed.
- Cache: one file per package, version and level in node_modules/.cache/safe-ts (or
  SAFE_TS_CACHE_DIR), keyed by the analyzer version, re-analyzed when a followed dependency
  changes version; linked packages also by a fingerprint of their files.
- Packs win per member; a pack module entry can say "complete": true (the zod pack does).
- safe-ts infer / coverage show the kinds; docs, README and CHANGELOG updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch evobug/auto-library-knowledge 2026-11-16 17:55:24 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!52
No description provided.