CLI: api-diff, breaking API changes since a git ref #7
Loading…
Reference in a new issue
No description provided.
Delete branch "api-diff"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Adds
safe-ts api-diff <git-ref> [entry files...] [--json]. It compares a package's public API at a git ref with the working tree and exits with 1 when a change breaks callers (0 otherwise, 2 for a bad ref or missing entries), in the spirit of cargo-semver-checks.What changed
crates/safe_ts_core/src/api.rs(new): reads a module's exports from syntax (functions with overloads, constants, classes, type aliases, interfaces,export *,export { a as b } from,export * as ns, default exports) into a normalised type model, then compares two versions.true | falseisboolean. Aliases callers can't name are expanded where they're declared, including through relative imports. Recursive aliases stop after one level.Option,Result,Enum,Mut,Readonly,ReadonlyArray,Array,Partial,Required, andPick/Omitwith literal keys are understood. An interface thatextendsan exported object type gets that type's fields merged in.Result(also insidePromise<Result>), a newPropagateErrorkind that can escape, a newEnumvariant in a returned type,T→Mut<T>, and sync → async when the return type isn't written. Compatible: the mirror images.Project::escaping_kinds(project.rs): exposes the existing whole-programfn_propagationfor a list of functions.resolveanddisplay_pathare nowpub(crate).crates/safe_ts_cli/src/api_diff.rs(new):git worktree(withcore.hooksPathpointed at nothing, so no hooks run) and removes it afterwards. This works from a package subdirectory of a monorepo.package.jsonexports(each subpath; preferssource/typesconditions and.tsfiles; uses the.tstwin of a missing.jstarget; skips*patterns), thenmain/module/types.--jsongives counts and per-exportstatus,location,usageandchanges.docs/guide/api-diff.mdcovers how to run it, what counts as breaking (tables by direction, callbacks, Enum errors) and a "What it can't see" section. Its sample outputs come from real runs.docs/README.md.One deliberate deviation from the brief: removing an
Enumvariant is breaking even in return position, including on the error side of aResult. I checked with tsc thatmatch(v, { ..., Gone: ... })fails with "Object literal may only specify known properties". Removing a member from a union of values stays compatible, as the brief says. The guide notes that TypeScript also rejects acasefor a literal that can no longer occur.How it was tested
Rebased onto main at
1fe2d1d: first onto04a5576, then again after #11 (Promise-returning functions, with #18's narrowing catches) and #6 were merged. The only conflict was the README paragraph next to #16's bench:check one; both are kept. Results on the rebased branch (head below):cargo nextest run --workspace: 110/110 pass.npm run test:std: 130/130 pass.npm run test:vite,npm run typecheck,npm run test:docs(docs typecheck, wasm, VitePress build): pass.bench:checknot run, because this touches no bench or transform code.cargo nextest run -p safe_ts_core --test api_diff: 15 fixture pairs (tests/api_diff/<case>/{old,new}plusexpected.txt), plus a check that an unchanged API reports nothing. They cover removed/added exports, parameters, returns,Resulterrors,PropagateErrorpropagation, enums in each direction, object keys/readonly/optional, local and recursive aliases,Partial, interfaceextends, callbacks, classes, re-exports, constants, and "can't classify".UPDATE_EXPECT=1regenerates the expected files. The 13 fixtures from before the rebase produce the same output under #5's and #11's analysis.cargo nextest run -p safe_ts_cli: 4 end-to-end tests on a temporary git repo with the package in a subdirectory: unchanged (exit 0), breaking (exit 1, JSON and text), explicit entries, compatible-only (exit 0), bad ref (exit 2), temporary worktree removed. New: a workspace sibling reached through anode_modulessymlink. This test fails without the resolver fallback below (1 spurious "can't classify").packages/against0e01fb3, the commit before #12 (details below).What changed in the rebase (second and third commits)
(file, name)to the whole-program pass, which still matches #5's keys. Calls throughns.f(), barrels andthis.#helper()are now followed. Two fixes:export default function load) was looked up asdefault, but the checker keys itload, so itsPropagateErrors were never reported. This bug was already in the PR.Repo (class) | static open | can now propagate ...), because the checker follows them now.propagate-callscovers both, plus a namespace import, an error class from anexport *barrel, and a method that stops propagating.packages/safe-ts-vue,api-diff HEADreported 8 "can't classify" changes with nothing changed: the old checkout (in the temp dir) has nonode_modules, so@evobug/safe-ts-std/enumstayed a name there but was expanded in the working tree.Project::with_packages_fromlets the old version resolve package imports from the working tree'snode_modules. That covers the type model and thePropagateErrorpass. Relative imports are unchanged.package.jsons in git still export./src/*.ts, so default discovery works as before. Against0e01fb3:index.d.tsunder atypescondition, and the old entry was the untypedindex.mjs. The result is 1 compatible change (SafeTsOptionsadded) and 9 "type now declared" can't-classify changes. That is accurate../recommended.jsonsubpath was parsed as TypeScript. Default discovery now skips non-code targets (in line with #2), and the result is no changes..release/<pkg>/manifests point atdist/*.d.ts/dist/*.js, which are gitignored, so no ref has an old version of them. Run there, api-diff used to list every export as "added" and exit 0. It now exits 2 and says to run it in the source directory.escaping_kindsreturned only kind names, so a kind could be listed twice, and a function whoseNotFoundmoved from a synchronous throw into a rejection (same signature) reported nothing. That is breaking, because callers'try { load(id) }withoutawaitno longer catches it. Kinds now carry their mode:can now reject with;tryor.catch(...)no longer catches.catch (e) { if (e instanceof NotFound) ...; throw e }lets through is reported.propagate-async.Left out / uncertain
PropagateErrorkinds: methods of exported object literals and inherited class members aren't tracked, and calls the checker can't tie to one function aren't followed.export const NAME = "x") count as their primitive type, so value changes aren't reported..vueentries aren't supported; JavaScript entries have no types, so only exports and parameter counts are compared.export *cycles, the module reached second may see an incomplete export list (memoised placeholder).safe-tsbinary, as for the other subcommands.🤖 Generated with Claude Code
7a1b6ed024toed859d0fc8ed859d0fc84fb2e54850