@samitouri / QOS-React / commits / ed1351c4fb

[compiler] improve zod v3 backwards compat (#34877)

## Summary When upgrading to `babel-plugin-react-compiler@1.0.0` in a project that uses `zod@3` we are running into TypeScript errors like: ``` node_modules/babel-plugin-react-compiler/dist/index.d.ts:435:10 - error TS2694: Namespace '"/REDACTED/node_modules/zod/v3/external"' has no exported member 'core'. 435 }, z.core.$strip>>>; ~~~~ ``` This problem seems to be related to d6eb735938bc67b41ad723206ea395ba4d761139, which introduced zod v3/v4 compatibility. Since `zod` is bundled into the compiler source this does not cause runtime issues and only manifests as TypeScript errors. My proposed solution is this PR is to use zod's [subpath versioning strategy](https://zod.dev/v4/versioning?id=versioning-in-zod-4) which allows you to support v3 and v4 APIs on both major versions. Changes in this PR include: - Updated `zod` import paths to `zod/v4` - Bumped min `zod` version to `^3.25.0` for zod which guarantees the `zod/v4` subpath is available. - Updated `zod-validation-error` import paths to `zod-validation-error/v4` - Bumped min `zod-validation-error ` version to `^3.5.0` - Updated `externals` tsup configuration where appropriate. Once the compiler drops zod v3 support we could optionally remove the `/v4` subpath from the imports. ## How did you test this change? Not totally sure the best way to test. I ran `NODE_ENV=production yarn workspace babel-plugin-react-compiler run build --dts` and diffed the `dist/` folder between my change and `v1.0.0` and it looks correct. We have a `patch-package` patch to workaround this for now and it works as expected. ```diff diff --git a/node_modules/babel-plugin-react-compiler/dist/index.d.ts b/node_modules/babel-plugin-react-compiler/dist/index.d.ts index 81c3f3d..daafc2c 100644 --- a/node_modules/babel-plugin-react-compiler/dist/index.d.ts +++ b/node_modules/babel-plugin-react-compiler/dist/index.d.ts @@ -1,7 +1,7 @@ import * as BabelCore from '@babel/core'; import { NodePath as NodePath$1 } from '@babel/core'; import * as t from '@babel/types'; -import { z } from 'zod'; +import { z } from 'zod/v4'; import { NodePath, Scope } from '@babel/traverse'; interface Result<T, E> { ``` Co-authored-by: Henry Q. Dineen <henryqdineen@gmail.com>

Henry Q. Dineen committed Oct 16, 2025 at 12:46 UTC ed1351c4fb92f84657a0c1a2af5ccef2484f7bd7
18 files changed +45 -32
compiler/packages/babel-plugin-react-compiler/package.json
+2 -2
@@ -52,8 +52,8 @@
52 "react-dom": "0.0.0-experimental-4beb1fd8-20241118",
53 "ts-jest": "^29.1.1",
54 "ts-node": "^10.9.2",
55 - "zod": "^3.22.4 || ^4.0.0",
56 - "zod-validation-error": "^3.0.3 || ^4.0.0"
55 + "zod": "^3.25.0 || ^4.0.0",
56 + "zod-validation-error": "^3.5.0 || ^4.0.0"
57 },
58 "resolutions": {
59 "./**/@babel/parser": "7.7.4",
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts
+2 -2
@@ -6,7 +6,7 @@
6 */
7
8 import * as t from '@babel/types';
9 -import {z} from 'zod';
9 +import {z} from 'zod/v4';
10 import {
11 CompilerDiagnostic,
12 CompilerError,
@@ -20,7 +20,7 @@ import {
20 tryParseExternalFunction,
21 } from '../HIR/Environment';
22 import {hasOwnProperty} from '../Utils/utils';
23 -import {fromZodError} from 'zod-validation-error';
23 +import {fromZodError} from 'zod-validation-error/v4';
24 import {CompilerPipelineValue} from './Pipeline';
25
26 const PanicThresholdOptionsSchema = z.enum([
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
+2 -2
@@ -6,8 +6,8 @@
6 */
7
8 import * as t from '@babel/types';
9 -import {ZodError, z} from 'zod';
10 -import {fromZodError} from 'zod-validation-error';
9 +import {ZodError, z} from 'zod/v4';
10 +import {fromZodError} from 'zod-validation-error/v4';
11 import {CompilerError} from '../CompilerError';
12 import {Logger, ProgramContext} from '../Entrypoint';
13 import {Err, Ok, Result} from '../Utils/Result';
compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
+1 -1
@@ -16,7 +16,7 @@ import {assertExhaustive} from '../Utils/utils';
16 import {Environment, ReactFunctionType} from './Environment';
17 import type {HookKind} from './ObjectShape';
18 import {Type, makeType} from './Types';
19 -import {z} from 'zod';
19 +import {z} from 'zod/v4';
20 import type {AliasingEffect} from '../Inference/AliasingEffects';
21 import {isReservedWord} from '../Utils/Keyword';
22 import {Err, Ok, Result} from '../Utils/Result';
compiler/packages/babel-plugin-react-compiler/src/HIR/TypeSchema.ts
+1 -1
@@ -6,7 +6,7 @@
6 */
7
8 import {isValidIdentifier} from '@babel/types';
9 -import {z} from 'zod';
9 +import {z} from 'zod/v4';
10 import {Effect, ValueKind} from '..';
11 import {
12 EffectSchema,
compiler/packages/babel-plugin-react-compiler/src/Utils/TestUtils.ts
+1 -1
@@ -5,7 +5,7 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 -import {fromZodError} from 'zod-validation-error';
8 +import {fromZodError} from 'zod-validation-error/v4';
9 import {CompilerError} from '../CompilerError';
10 import {
11 CompilationMode,
compiler/packages/eslint-plugin-react-compiler/package.json
+2 -2
@@ -15,8 +15,8 @@
15 "@babel/core": "^7.24.4",
16 "@babel/parser": "^7.24.4",
17 "hermes-parser": "^0.25.1",
18 - "zod": "^3.22.4 || ^4.0.0",
19 - "zod-validation-error": "^3.0.3 || ^4.0.0"
18 + "zod": "^3.25.0 || ^4.0.0",
19 + "zod-validation-error": "^3.5.0 || ^4.0.0"
20 },
21 "devDependencies": {
22 "@babel/preset-env": "^7.22.4",
compiler/packages/eslint-plugin-react-compiler/tsup.config.ts
+8 -1
@@ -10,7 +10,14 @@ import {defineConfig} from 'tsup';
10 export default defineConfig({
11 entry: ['./src/index.ts'],
12 outDir: './dist',
13 - external: ['@babel/core', 'hermes-parser', 'zod', 'zod-validation-error'],
13 + external: [
14 + '@babel/core',
15 + 'hermes-parser',
16 + 'zod',
17 + 'zod/v4',
18 + 'zod-validation-error',
19 + 'zod-validation-error/v4',
20 + ],
21 splitting: false,
22 sourcemap: false,
23 dts: false,
compiler/packages/react-compiler-healthcheck/package.json
+2 -2
@@ -17,8 +17,8 @@
17 "fast-glob": "^3.3.2",
18 "ora": "5.4.1",
19 "yargs": "^17.7.2",
20 - "zod": "^3.22.4 || ^4.0.0",
21 - "zod-validation-error": "^3.0.3 || ^4.0.0"
20 + "zod": "^3.25.0 || ^4.0.0",
21 + "zod-validation-error": "^3.5.0 || ^4.0.0"
22 },
23 "devDependencies": {},
24 "engines": {
compiler/packages/react-compiler-healthcheck/tsup.config.ts
+2
@@ -18,7 +18,9 @@ export default defineConfig({
18 'ora',
19 'yargs',
20 'zod',
21 + 'zod/v4',
22 'zod-validation-error',
23 + 'zod-validation-error/v4',
24 ],
25 splitting: false,
26 sourcemap: false,
compiler/packages/react-mcp-server/package.json
+1 -1
@@ -24,7 +24,7 @@
24 "html-to-text": "^9.0.5",
25 "prettier": "^3.3.3",
26 "puppeteer": "^24.7.2",
27 - "zod": "^3.22.4 || ^4.0.0"
27 + "zod": "^3.25.0 || ^4.0.0"
28 },
29 "devDependencies": {
30 "@types/html-to-text": "^9.0.4",
compiler/packages/react-mcp-server/src/index.ts
+1 -1
@@ -7,7 +7,7 @@
7
8 import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
9 import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js';
10 -import {z} from 'zod';
10 +import {z} from 'zod/v4';
11 import {compile, type PrintedCompilerPipelineValue} from './compiler';
12 import {
13 CompilerPipelineValue,
compiler/packages/snap/package.json
+3 -1
@@ -37,7 +37,9 @@
37 "react": "0.0.0-experimental-4beb1fd8-20241118",
38 "react-dom": "0.0.0-experimental-4beb1fd8-20241118",
39 "readline": "^1.3.0",
40 - "yargs": "^17.7.1"
40 + "yargs": "^17.7.1",
41 + "zod": "^3.25.0 || ^4.0.0",
42 + "zod-validation-error": "^3.5.0 || ^4.0.0"
43 },
44 "devDependencies": {
45 "@babel/core": "^7.19.1",
compiler/packages/snap/src/sprout/evaluator.ts
+2 -2
@@ -9,8 +9,8 @@ import {render} from '@testing-library/react';
9 import {JSDOM} from 'jsdom';
10 import React, {MutableRefObject} from 'react';
11 import util from 'util';
12 -import {z} from 'zod';
13 -import {fromZodError} from 'zod-validation-error';
12 +import {z} from 'zod/v4';
13 +import {fromZodError} from 'zod-validation-error/v4';
14 import {initFbt, toJSON} from './shared-runtime';
15
16 /**
compiler/yarn.lock
+6 -6
@@ -11505,17 +11505,17 @@ zod-to-json-schema@^3.24.1:
11505 resolved "https://registry.npmjs.org/zod-to-json-schema/-/zod-to-json-schema-3.24.5.tgz"
11506 integrity sha512-/AuWwMP+YqiPbsJx5D6TfgRTc4kTLjsh5SOcd4bLsfUg2RcEXrFMJl1DGgdHy2aCfsIA/cr/1JM0xcB2GZji8g==
11507
11508 -"zod-validation-error@^3.0.3 || ^4.0.0":
11508 +"zod-validation-error@^3.5.0 || ^4.0.0":
11509 version "4.0.2"
11510 resolved "https://registry.yarnpkg.com/zod-validation-error/-/zod-validation-error-4.0.2.tgz#bc605eba49ce0fcd598c127fee1c236be3f22918"
11511 integrity sha512-Q6/nZLe6jxuU80qb/4uJ4t5v2VEZ44lzQjPDhYJNztRQ4wyWc6VF3D3Kb/fAuPetZQnhS3hnajCf9CsWesghLQ==
11512
11513 -"zod@^3.22.4 || ^4.0.0":
11514 - version "4.1.11"
11515 - resolved "https://registry.yarnpkg.com/zod/-/zod-4.1.11.tgz#4aab62f76cfd45e6c6166519ba31b2ea019f75f5"
11516 - integrity sha512-WPsqwxITS2tzx1bzhIKsEs19ABD5vmCVa4xBo2tq/SrV4RNZtfws1EnCWQXM6yh8bD08a1idvkB5MZSBiZsjwg==
11517 -
11513 zod@^3.23.8, zod@^3.24.1:
11514 version "3.24.3"
11515 resolved "https://registry.npmjs.org/zod/-/zod-3.24.3.tgz"
11516 integrity sha512-HhY1oqzWCQWuUqvBFnsyrtZRhyPeR7SUGv+C4+MsisMuVfSPx8HpwWqH8tRahSlt6M3PiFAcoeFhZAqIXTxoSg==
11517 +
11518 +"zod@^3.25.0 || ^4.0.0":
11519 + version "4.1.12"
11520 + resolved "https://registry.yarnpkg.com/zod/-/zod-4.1.12.tgz#64f1ea53d00eab91853195653b5af9eee68970f0"
11521 + integrity sha512-JInaHOamG8pt5+Ey8kGmdcAcg3OL9reK8ltczgHTAwNhMys/6ThXHityHxVV2p3fkw/c+MAvBHFVYHFZDmjMCQ==
packages/eslint-plugin-react-hooks/package.json
+2 -2
@@ -42,8 +42,8 @@
42 "@babel/core": "^7.24.4",
43 "@babel/parser": "^7.24.4",
44 "hermes-parser": "^0.25.1",
45 - "zod": "^3.22.4 || ^4.0.0",
46 - "zod-validation-error": "^3.0.3 || ^4.0.0"
45 + "zod": "^3.25.0 || ^4.0.0",
46 + "zod-validation-error": "^3.5.0 || ^4.0.0"
47 },
48 "devDependencies": {
49 "@babel/eslint-parser": "^7.11.4",
scripts/rollup/bundles.js
+2
@@ -1255,7 +1255,9 @@ const bundles = [
1255 '@babel/core',
1256 'hermes-parser',
1257 'zod',
1258 + 'zod/v4',
1259 'zod-validation-error',
1260 + 'zod-validation-error/v4',
1261 'crypto',
1262 'util',
1263 ],
yarn.lock
+5 -5
@@ -18245,12 +18245,12 @@ zip-stream@^2.1.2:
18245 compress-commons "^2.1.1"
18246 readable-stream "^3.4.0"
18247
18248 -"zod-validation-error@^3.0.3 || ^4.0.0":
18248 +"zod-validation-error@^3.5.0 || ^4.0.0":
18249 version "4.0.2"
18250 resolved "https://registry.yarnpkg.com/zod-validation-error/-/zod-validation-error-4.0.2.tgz#bc605eba49ce0fcd598c127fee1c236be3f22918"
18251 integrity sha512-Q6/nZLe6jxuU80qb/4uJ4t5v2VEZ44lzQjPDhYJNztRQ4wyWc6VF3D3Kb/fAuPetZQnhS3hnajCf9CsWesghLQ==
18252
18253 -"zod@^3.22.4 || ^4.0.0":
18254 - version "4.1.11"
18255 - resolved "https://registry.yarnpkg.com/zod/-/zod-4.1.11.tgz#4aab62f76cfd45e6c6166519ba31b2ea019f75f5"
18256 - integrity sha512-WPsqwxITS2tzx1bzhIKsEs19ABD5vmCVa4xBo2tq/SrV4RNZtfws1EnCWQXM6yh8bD08a1idvkB5MZSBiZsjwg==
18253 +"zod@^3.25.0 || ^4.0.0":
18254 + version "4.1.12"
18255 + resolved "https://registry.yarnpkg.com/zod/-/zod-4.1.12.tgz#64f1ea53d00eab91853195653b5af9eee68970f0"
18256 + integrity sha512-JInaHOamG8pt5+Ey8kGmdcAcg3OL9reK8ltczgHTAwNhMys/6ThXHityHxVV2p3fkw/c+MAvBHFVYHFZDmjMCQ==