Skip to content

Commit

Permalink
fix: Ensure only selected types are exported from runime module
Browse files Browse the repository at this point in the history
In #20161, in order to solve type declaration/OOM/performance issues,
we added forced exports for every type within the runime.

However, our type bundler renames certain types if the naming conflicts.
For example, if there are 2 `Result` types, one of them will be renamed
to `Result_2`.

Making renamed type public gives `tsc` green light for dependenig on it
when creating declaration for third-party library. This is wrong,
because rename is not guaranteed to be stable. In fact, we have the
problem with `accelerate` extension on a different branch: latest
Accelerate depends on `Result_2`, but in a branch corresponding type got
renamed to `Result_3`.

Internal discussion: https://prisma-company.slack.com/archives/C04TW4A2H8C/p1694778678856899

In this PR we revert to manual re-exports and ensure every type from
`core/types` gets re-exported without rename. Tests, fixed in #20161 are still green,
but it also ensures that only types we want will leak to third party
project.

Couple of public types with conflicting names got renamed.
  • Loading branch information
SevInf committed Sep 18, 2023
1 parent f718439 commit 00cbe37
Show file tree
Hide file tree
Showing 27 changed files with 1,755 additions and 1,651 deletions.
10 changes: 1 addition & 9 deletions helpers/compile/plugins/tscPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,7 @@ export const tscPlugin: (emitTypes?: boolean) => esbuild.Plugin = (emitTypes?: b
// we get the types generated by tsc and bundle them near the output
bundleTypeDefinitions(typeOutPath, bundlePath)

// when types are already exported by us, it is not always guaranteed
// that the type bundler will also export them. It won't be able to
// trace them correctly back to runtime module and fail with either
// "... is using the type X but can not be named" or "The inferred
// type of this node exceeds the maximum length" error, or even worse
// the compiler could inline the types in the users' project causing
// very bad performance issues. To fix this, we export all the types.
let dtsContents = await fs.readFile(`${bundlePath}.d.ts`, 'utf-8')
dtsContents = dtsContents.replace(/(?<!export )declare (type|interface|const)/g, 'export declare $1')
const dtsContents = await fs.readFile(`${bundlePath}.d.ts`, 'utf-8')
await fs.outputFile(`${bundlePath}.d.ts`, dtsContents)
} else {
// in watch mode, it wouldn't be viable to bundle the types every time
Expand Down
6 changes: 3 additions & 3 deletions packages/client/scripts/default-index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export declare const PrismaClient: any
export declare type PrismaClient = any

export declare class PrismaClientExtends<
ExtArgs extends runtime.Types.Extensions.Args = runtime.Types.Extensions.DefaultArgs,
ExtArgs extends runtime.Types.Extensions.ExtensionArgs = runtime.Types.Extensions.DefaultArgs,
> {
$extends: { extArgs: ExtArgs } & (<
R extends runtime.Types.Extensions.UserArgs['result'] = {},
M extends runtime.Types.Extensions.UserArgs['model'] = {},
Q extends runtime.Types.Extensions.UserArgs['query'] = {},
C extends runtime.Types.Extensions.UserArgs['client'] = {},
Args extends runtime.Types.Extensions.Args = runtime.Types.Extensions.InternalArgs<R, M, {}, C>,
Args extends runtime.Types.Extensions.ExtensionArgs = runtime.Types.Extensions.InternalArgs<R, M, {}, C>,
>(
args:
| ((client: PrismaClientExtends<ExtArgs>) => { $extends: { extArgs: Args } })
Expand Down Expand Up @@ -84,7 +84,7 @@ export namespace Prisma {
M extends runtime.Types.Extensions.UserArgs['model'] = {},
Q extends runtime.Types.Extensions.UserArgs['query'] = {},
C extends runtime.Types.Extensions.UserArgs['client'] = {},
Args extends runtime.Types.Extensions.Args = runtime.Types.Extensions.InternalArgs<R, M, {}, C>,
Args extends runtime.Types.Extensions.ExtensionArgs = runtime.Types.Extensions.InternalArgs<R, M, {}, C>,
>(
args:
| ((client: PrismaClientExtends) => { $extends: { extArgs: Args } })
Expand Down

0 comments on commit 00cbe37

Please sign in to comment.