Skip to content

fix(dev): align dev server typegen defaults with sanity typegen generate#1426

Open
bjoerge wants to merge 2 commits into
mainfrom
fix/typegen-dev-server-defaults
Open

fix(dev): align dev server typegen defaults with sanity typegen generate#1426
bjoerge wants to merge 2 commits into
mainfrom
fix/typegen-dev-server-defaults

Conversation

@bjoerge

@bjoerge bjoerge commented Jul 2, 2026

Copy link
Copy Markdown
Member

Description

The typegen vite plugin used by sanity dev/sanity build hand-rolled its config defaults, defaulting formatGeneratedCode and overloadClientMethods to false (and using a narrower set of default query patterns). Meanwhile, sanity typegen generate applies defaults through configDefinition.parse() from @sanity/codegen, where both default to true. As a result, the same sanity.cli.ts config produced different output depending on whether types were generated by the dev server or by the manual command: the dev server emitted unformatted code (large git diffs) without the @sanity/client query typemap.

This PR replaces the hand-rolled defaults with configDefinition.parse(inputConfig) — the exact same zod schema the typegen generate command uses — so the two code paths share one source of truth and can't drift again. The schema strips the extra enabled key from the CLI config, and the plugin's now-redundant local default query patterns were removed.

Fixes #1425

What to review

  • packages/@sanity/cli/src/server/vite/plugin-typegen.ts: config resolution now goes through configDefinition.parse(). Note this intentionally changes the dev-server defaults: formatGeneratedCode: true, overloadClientMethods: true, generates: './sanity.types.ts', and the broader default query patterns from @sanity/codegen (including .astro/.vue/.svelte and ./sanity/**).
  • Explicitly-set config values are still respected (defaults only fill in unset fields).

Testing

  • Updated the plugin unit test that had baked in the old (wrong) defaults to assert the plugin passes configDefinition.parse({}) verbatim, so it stays in lockstep with @sanity/codegen.
  • Verified end-to-end in fixtures/basic-studio with the issue reporter's config shape (enabled: true, format/overload unset):
    • sanity typegen generate output vs. dev-server-generated output is now byte-identical (prettier-formatted, with the declare module "@sanity/client" typemap).
    • Watch-mode regeneration after editing a query file remains identical to manual generation.
    • Explicit opt-out (formatGeneratedCode: false, overloadClientMethods: false) still produces unformatted output without the typemap.

Note

Medium Risk
Default generated sanity.types.ts output changes for projects that relied on implicit dev-server defaults (formatting and client typemap), though explicit config opt-outs still apply.

Overview
The Vite typegen plugin used by sanity dev / sanity build no longer applies its own default config. It now resolves options with configDefinition.parse() from @sanity/codegen, the same path as sanity typegen generate, so watch/build and manual runs share one source of truth.

Behavior change when fields are left unset: dev-server generation now defaults formatGeneratedCode and overloadClientMethods to true, uses @sanity/codegen’s output path and query globs (broader than the removed local patterns), and strips CLI-only keys like enabled via the schema. Explicit sanity.cli.ts values are unchanged.

The plugin unit test now expects runTypegenGenerate to receive configDefinition.parse({}) instead of the old hand-rolled defaults.

Reviewed by Cursor Bugbot for commit 70333a0. Bugbot is set up for automated code reviews on this repo. Configure here.

@bjoerge bjoerge requested a review from a team as a code owner July 2, 2026 16:23
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

📦 Bundle Stats — @sanity/cli

Compared against main (bb18d264)

@sanity/cli

Metric Value vs main (bb18d26)
Internal (raw) 2.7 KB -
Internal (gzip) 1.0 KB -
Bundled (raw) 11.16 MB -
Bundled (gzip) 2.10 MB -
Import time 686ms +3ms, +0.4%

bin:sanity

Metric Value vs main (bb18d26)
Internal (raw) 782 B -
Internal (gzip) 423 B -
Bundled (raw) 9.87 MB -
Bundled (gzip) 1.78 MB -
Import time 1.78s +3ms, +0.2%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — @sanity/cli-core

Compared against main (bb18d264)

Metric Value vs main (bb18d26)
Internal (raw) 106.7 KB -
Internal (gzip) 26.7 KB -
Bundled (raw) 21.72 MB -
Bundled (gzip) 3.46 MB -
Import time 608ms +15ms, +2.6%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — create-sanity

Compared against main (bb18d264)

Metric Value vs main (bb18d26)
Internal (raw) 908 B -
Internal (gzip) 483 B -
Bundled (raw) 931 B -
Bundled (gzip) 491 B -
Import time ❌ ChildProcess denied: node -
Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage Delta

File Statements
packages/@sanity/cli/src/server/vite/plugin-typegen.ts 86.3% (- 0.2%)

Comparing 1 changed file against main @ bb18d2647e149f8e545bf139f28c1cbb6dd8937d

Overall Coverage

Metric Coverage
Statements 74.4% (- 0.0%)
Branches 64.3% (- 0.1%)
Functions 69.0% (+ 0.0%)
Lines 75.0% (- 0.0%)

@runeb runeb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Assuming this doesn't break users types, only reduces diffs and adds more core types

@bjoerge

bjoerge commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

Assuming this doesn't break users types, only reduces diffs and adds more core types

I don't have a clear picture of the downstream impact of these changes, so not in a position to give a confident answer to that. Would love for someone from @sanity-io/typegen to weigh in/review.

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.

Automatic typegen (enabled: true) has different defaults

2 participants