Skip to content

Commit dbd7e42

Browse files
authored
fix(codegen-ts): drop dprint-node from the CLI install (#579)
* fix(cli): stop installing dprint-node with every CLI install ts-poet requires dprint-node (about 25 MB of platform binaries) at module load. Vendor ts-poet's printer as codegen-ts/src/poet.ts (Apache-2.0, licence kept) and format with the same dprint TypeScript plugin, 0.87.1, through its WebAssembly build (@dprint/typescript + @dprint/formatter). Output is byte-identical: every codegen golden and conformance fixture is unchanged. poet.ts is an engine module, so owned and ejected templates take Code from @metaobjectsdev/codegen-ts/engine and class identity holds. The config loader still maps a legacy bare ts-poet import to that entry. check-peer-ranges now refuses dprint-node, ts-poet and @types/* in a published package's dependencies or required peers unless allowlisted. No published package declared @types/*; @types/node arrives only through the libSQL driver, which is out of scope. Fresh CLI install: 151 MB (134,525,165 bytes) -> 133 MB (115,177,311 bytes). * docs(changelog): note the standalone binary no longer crashes on dprint-node * fix(codegen-ts): address review of the vendored printer - A formatter that fails to load now throws instead of silently emitting unformatted code; only a format error falls back. - The CLI pins @metaobjectsdev/codegen-ts/engine to the same build (dist vs src) as the package root, so both reach one poet.ts. - The engine entry re-exports only code/imp/joinCode/Import/Code from the printer, as it did from ts-poet. - Restore ts-poet's toString() call for non-plain placeholders. - Refresh the index.ts comment and narrow the CHANGELOG claims about legacy ts-poet imports and the standalone binary. * refactor: drop unused printer helpers; reuse the workspace walk in the peer guard Remove asOneline/oneline, Import.fromMaybeString and arrayOf from the vendored printer (no callers). check-peer-ranges enumerates packages through publish-set.mjs's enumerateWorkspacePackages instead of its own readdir walk. * fix(codegen-ts): import-path.ts takes the vendored printer; refresh install numbers after #567 * no-mistakes(review): guard: refuse install-only deps via optionalDependencies; add real-check tests * no-mistakes(review): apply reviewer inline fixes * no-mistakes(review): wire peer-ranges guard test into gate_peer_ranges lane * no-mistakes(test): apply changes made during test validation * no-mistakes(document): Doc sync: ts-poet printer, guard, binary
1 parent c3455eb commit dbd7e42

53 files changed

Lines changed: 1140 additions & 160 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ import { EntityFetcherProvider, EntityGrid } from "@metaobjectsdev/tanstack";
260260
- **TS runtime**: Bun-first for development (zero-config TS, native test runner). Node-compatible for distribution; users install via npm/pnpm/bun without lock-in to Bun's runtime.
261261
- **Module system**: ESM only. No CommonJS, no transpile step required.
262262
- **Storage format**: JSON files in `metaobjects/meta.<concept>.json` at project root. `.metaobjects/.gen-state/` holds the codegen merge base: the snapshot **bodies are gitignored** (a second full copy of all generated output), but **`.hashes.json` is COMMITTED** — one hash per generated path, and the only thing that lets `meta gen` tell "this file is exactly what I wrote" from "somebody edited this" on a machine that did not generate it. Ignore it and a fresh clone or CI runner silently overwrites hand edits; commit it and an edited file is refused by name instead.
263-
- **Codegen substrate**: ts-poet for greenfield emit, ts-morph for in-place edits, Biome for format pass, `git merge-file --diff3` for hand-edit-preserving regen.
263+
- **Codegen substrate**: the vendored ts-poet printer (`codegen-ts/src/poet.ts`) for greenfield emit, ts-morph for in-place edits, Biome for format pass, `git merge-file --diff3` for hand-edit-preserving regen.
264264
- **Runtime substrate**: Kysely for TS (user-provided connection, async-only).
265265
- **Migration substrate**: Postgres + SQLite for TS v0.3.
266266
- **Metadata location**: resolved via `resolveCollection()` (`@metaobjectsdev/sdk`) — the single authority. `metaobjects/` is the **default value of `sources`** and nothing else: no other module, command or user-facing message may assert that a directory of that name exists or is where metadata lives. Exactly five sites may name it — `sdk/src/metadata-files.ts` (`DEFAULT_METADATA_DIR`, its single definition), `sdk/src/sources.ts` (`DEFAULT_SOURCES`, **the** default), `sdk/src/collection.ts` (inside `resolveCollection`, *applying* that default), `sdk/src/index.ts` (the barrel re-export of the constant, no use), and `cli/src/commands/init.ts` (the scaffolder **writing** the layout). Enforced by `sdk/test/no-hardcoded-metadata-dir.test.ts`, whose allowlist demands a written reason per entry. See [docs/features/metadata-sources.md](docs/features/metadata-sources.md).

‎CHANGELOG.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,31 @@ loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersi
126126
lint gate), and a fresh npm install of the CLI drops from 245 MB to 151 MB of `node_modules`.
127127
`scripts/check-peer-ranges.ts` now fails a publishable package that requires `@biomejs/biome`
128128
through `dependencies` or a non-optional peer.
129+
- **`@metaobjectsdev/cli` no longer installs `dprint-node`.** The TypeScript generators printed
130+
code with `ts-poet`, which loads `dprint-node` on import, and dprint-node ships all seven of
131+
its platform binaries in one package (24 MB, of which a machine runs one). `codegen-ts` now
132+
vendors the part of ts-poet the generators use (`src/poet.ts`, Apache-2.0, licence in
133+
`LICENSE.ts-poet`) and runs the same dprint TypeScript plugin as WebAssembly
134+
(`@dprint/typescript`, pinned to 0.87.1, the version dprint-node 1.0.8 compiles in, 5 MB) with
135+
ts-poet's configuration, so generated code is byte-identical: every codegen golden and
136+
conformance fixture is unchanged. `codegen-ts-react`, `codegen-ts-tanstack` and
137+
`codegen-ts-angular` drop their `ts-poet` dependency and import the printer from
138+
`@metaobjectsdev/codegen-ts/engine`, which an ejected template also uses. A bare `ts-poet`
139+
import in an older owned generator is still pointed at that entry by `meta gen`, which
140+
carries `code`, `imp`, `joinCode`, `Import` and the `Code` type; ts-poet's `saveFiles`,
141+
`conditionalOutput` and its `toString` import options are not carried over. A fresh npm
142+
install of the CLI drops from 151 MB (134,641,989 bytes) to 133 MB (115,302,944 bytes) of
143+
`node_modules`. `scripts/check-peer-ranges.ts` now also fails a publishable package that
144+
requires `dprint-node`, `ts-poet` or any `@types/*` package through `dependencies`,
145+
`optionalDependencies` or a non-optional peer; a type package a published `.d.ts` imports goes
146+
in its `ALLOWED_TYPES_DEPS`
147+
with the reason. No `@metaobjectsdev/*` package declares `@types/node`: the 3 MB copy in the
148+
install comes through the libSQL driver (`@libsql/isomorphic-ws` → `@types/ws`), which is
149+
unchanged here. This also repairs the standalone binary (`bun run build:binary`): `--compile`
150+
cannot embed dprint-node's native `.node` file, so a binary built from 1.2.0 crashed with
151+
`Cannot find module './dprint-node.linux-x64-gnu.node'` on every command that loads the
152+
codegen engine, `meta migrate` and `meta verify --db` included. Those schema commands, the
153+
binary's documented scope, now run; `meta gen` is still not supported inside the binary.
129154

130155
### Deprecated
131156

‎bun.lock‎

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/features/own-your-codegen.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ holds), the naming tables and the type-mapping tables (`names.ts`, `naming.ts`,
108108
`column-mapper.ts`), the primary-key and TPH walks (`pk-resolver.ts`, `tph.ts`), and the
109109
projection and report lowering that `meta migrate` and `meta verify` run. An owned file
110110
imports the engine from the package's engine entry, `@metaobjectsdev/codegen-ts/engine`,
111-
which also re-exports `ts-poet`'s `code`, `imp` and `joinCode`. The list is
112-
`ENGINE_MODULES`, exported from that entry; a module
111+
which also exports the code printer's `code`, `imp` and `joinCode` (`poet.ts`, the package's
112+
vendored copy of `ts-poet`). The list is `ENGINE_MODULES`, exported from that entry; a module
113113
that is not on it and that a generator reaches is copied. An owned generator therefore
114114
imports no packaged module that returns emitted text, and a test ejects each generator alone
115115
into an empty project to prove it: it compiles as your `tsconfig.codegen.json` checks it and
116116
writes what the packaged generator writes, byte for byte.
117117

118118
A copy differs from the packaged module in its **import lines only**. A relative import of
119119
an engine module becomes an import of `@metaobjectsdev/codegen-ts/engine`, a relative import of
120-
another template module becomes the path to your copy of it, and `ts-poet` becomes the
121-
engine's own (a copy must build `Code` with the same `ts-poet` the engine uses). Nothing else
120+
another template module becomes the path to your copy of it, and the printer stays the
121+
engine's (a copy must build `Code` with the same printer the engine uses). Nothing else
122122
is touched, so "what did upstream change?" is a diff of two files whose body is the same.
123123

124124
- **Two generators that call one module share one copy.** `entity` and `queries` both call

‎fixtures/generator-registry-conformance/registry.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
"codegen-ts/src/naming/name-claims.ts",
378378
"codegen-ts/src/owned-runtime.ts",
379379
"codegen-ts/src/pk-resolver.ts",
380+
"codegen-ts/src/poet.ts",
380381
"codegen-ts/src/projection/build-projection-views.ts",
381382
"codegen-ts/src/projection/extract-view-spec.ts",
382383
"codegen-ts/src/projection/projection-detector.ts",

‎scripts/check-peer-ranges.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// The install-time guard (scripts/check-peer-ranges.ts, second rule): a publishable
2+
// package cannot land dprint-node, ts-poet or a run-time @types/* in an adopter's
3+
// tree through ANY manifest field npm installs by default. npm installs
4+
// `optionalDependencies` by default (it skips an entry only when that install
5+
// fails), so the guard must refuse them there exactly as it does under
6+
// `dependencies` — otherwise a published manifest reintroduces the ~25 MB
7+
// formatter while the gate stays green. The escape hatch stays an optional peer
8+
// (`peerDependenciesMeta`), which npm leaves to the adopter. All three cases are
9+
// pinned by driving `peerRangeReport` — the same walk the CLI runs — against a
10+
// synthetic workspace.
11+
import { afterAll, describe, expect, test } from "bun:test";
12+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
13+
import { tmpdir } from "node:os";
14+
import { join } from "node:path";
15+
import { peerRangeReport } from "./check-peer-ranges";
16+
17+
const created: string[] = [];
18+
afterAll(() => { for (const d of created) rmSync(d, { recursive: true, force: true }); });
19+
20+
/** A throwaway workspace whose single publishable package has this manifest body. */
21+
function workspaceWith(pkg: Record<string, unknown>): string {
22+
const root = mkdtempSync(join(tmpdir(), "check-peer-ranges-"));
23+
created.push(root);
24+
const dir = join(root, "server/typescript/packages/fixture");
25+
mkdirSync(dir, { recursive: true });
26+
writeFileSync(join(dir, "package.json"), JSON.stringify({ name: "@metaobjectsdev/fixture", version: "1.2.0", ...pkg }));
27+
return root;
28+
}
29+
30+
describe("install-time guard — optionalDependencies are installed by default", () => {
31+
test("dprint-node under optionalDependencies is refused, naming the field", () => {
32+
const report = peerRangeReport(workspaceWith({ optionalDependencies: { "dprint-node": "^1.0.8" } }));
33+
expect(report.installOnlyDeps).toEqual([
34+
expect.objectContaining({ pkg: "@metaobjectsdev/fixture", dep: "dprint-node", field: "optionalDependencies" }),
35+
]);
36+
});
37+
38+
test("a run-time @types/* under optionalDependencies is refused too", () => {
39+
const report = peerRangeReport(workspaceWith({ optionalDependencies: { "@types/node": "^22.0.0" } }));
40+
expect(report.installOnlyDeps).toEqual([
41+
expect.objectContaining({ dep: "@types/node", field: "optionalDependencies" }),
42+
]);
43+
});
44+
45+
test("a clean package passes: real dep, bounded peer counted", () => {
46+
const report = peerRangeReport(workspaceWith({
47+
dependencies: { "drizzle-orm": "^0.36.0" },
48+
peerDependencies: { react: "^18.0.0" },
49+
}));
50+
expect(report.installOnlyDeps).toEqual([]);
51+
expect(report.unboundedPeers).toEqual([]);
52+
expect(report.peerRanges).toBe(1);
53+
});
54+
55+
test("the documented escape hatch holds: an optional peer is not refused", () => {
56+
const report = peerRangeReport(workspaceWith({
57+
peerDependencies: { "dprint-node": "^1.0.8" },
58+
peerDependenciesMeta: { "dprint-node": { optional: true } },
59+
}));
60+
expect(report.installOnlyDeps).toEqual([]);
61+
});
62+
});

0 commit comments

Comments
 (0)