Skip to content

Commit

Permalink
fix(client): custom output mismatch (#18910)
Browse files Browse the repository at this point in the history
  • Loading branch information
millsp committed Apr 28, 2023
1 parent 2557106 commit 168771a
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 300 deletions.
3 changes: 1 addition & 2 deletions packages/client/src/generation/TSClient/TSClient.ts
Expand Up @@ -110,7 +110,6 @@ export class TSClient implements Generatable {

const code = `${commonCodeJS({ ...this.options, browser: false })}
${buildRequirePath(edge)}
${buildDirname(edge, relativeOutdir)}
/**
* Enums
Expand All @@ -132,8 +131,8 @@ ${buildDMMF(dataProxy && engineProtocol === 'graphql', this.options.document)}
* Create the Client
*/
const config = ${JSON.stringify(config, null, 2)}
config.dirname = dirname
config.document = dmmf
${buildDirname(edge, relativeOutdir)}
${await buildInlineSchema(dataProxy, schemaPath)}
${buildInlineDatasource(dataProxy, datasources)}
${buildInjectableEdgeEnv(edge, datasources)}
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/generation/TSClient/common.ts
Expand Up @@ -34,7 +34,6 @@ import {
objectEnumValues,
makeStrictEnum,
Extensions,
findSync
} from '${runtimeDir}/edge-esm.js'`
: browser
? `
Expand Down Expand Up @@ -63,7 +62,7 @@ const {
objectEnumValues,
makeStrictEnum,
Extensions,
findSync
warnOnce,
} = require('${runtimeDir}/${runtimeName}')
`
}
Expand Down
33 changes: 11 additions & 22 deletions packages/client/src/generation/utils/buildDirname.ts
@@ -1,5 +1,3 @@
import path from 'path'

/**
* Builds a `dirname` variable that holds the location of the generated client.
* @param edge
Expand All @@ -24,37 +22,28 @@ export function buildDirname(edge: boolean, relativeOutdir: string) {
* moved and copied out of its original spot. It all fails, it falls-back to
* `findSync`, when `__dirname` is not available (eg. bundle, electron) or
* nothing has been found around `__dirname`.
* @param defaultRelativeOutdir
* @param relativeOutdir
* @param runtimePath
* @returns
*/
function buildDirnameFind(defaultRelativeOutdir: string) {
// potential client location on serverless envs
const serverlessRelativeOutdir = defaultRelativeOutdir.split(path.sep).slice(1).join(path.sep)

function buildDirnameFind(relativeOutdir: string) {
return `
const fs = require('fs')
// some frameworks or bundlers replace or totally remove __dirname
const hasDirname = typeof __dirname !== 'undefined' && __dirname !== '/'
// will work in most cases, ie. if the client has not been bundled
const regularDirname = hasDirname && fs.existsSync(path.join(__dirname, 'schema.prisma')) && __dirname
// if the client has been bundled, we need to look for the folders
const foundDirname = !regularDirname && findSync(process.cwd(), [
${defaultRelativeOutdir ? `${JSON.stringify(defaultRelativeOutdir)},` : ''}
${serverlessRelativeOutdir ? `${JSON.stringify(serverlessRelativeOutdir)},` : ''}
], ['d'], ['d'], 1)[0]
const dirname = regularDirname || foundDirname || __dirname`
config.dirname = __dirname
if (!fs.existsSync(path.join(__dirname, 'schema.prisma'))) {
warnOnce('bundled-warning-1', 'Your generated Prisma Client could not immediately find its \`schema.prisma\`, falling back to finding it via the current working directory.')
warnOnce('bundled-warning-2', 'We are interested in learning about your project setup. We\\'d appreciate if you could take the time to share some information with us.')
warnOnce('bundled-warning-3', 'Please help us by answering a few questions: https://pris.ly/bundler-investigation')
config.dirname = path.join(process.cwd(), ${JSON.stringify(relativeOutdir)})
config.isBundled = true
}`
}
// TODO: 👆 all this complexity could fade away if we embed the schema

/**
* Builds a simple `dirname` for when it is not important to have one.
* @returns
*/
function buildDirnameDefault() {
return `const dirname = '/'`
return `config.dirname = '/'`
}
4 changes: 2 additions & 2 deletions packages/client/src/generation/utils/buildWarnEnvConflicts.ts
Expand Up @@ -13,7 +13,7 @@ export function buildWarnEnvConflicts(edge: boolean, runtimeDir: string, runtime
const { warnEnvConflicts } = require('${runtimeDir}/${runtimeName}')
warnEnvConflicts({
rootEnvPath: config.relativeEnvPaths.rootEnvPath && path.resolve(dirname, config.relativeEnvPaths.rootEnvPath),
schemaEnvPath: config.relativeEnvPaths.schemaEnvPath && path.resolve(dirname, config.relativeEnvPaths.schemaEnvPath)
rootEnvPath: config.relativeEnvPaths.rootEnvPath && path.resolve(config.dirname, config.relativeEnvPaths.rootEnvPath),
schemaEnvPath: config.relativeEnvPaths.schemaEnvPath && path.resolve(config.dirname, config.relativeEnvPaths.schemaEnvPath)
})`
}
8 changes: 8 additions & 0 deletions packages/client/src/runtime/getPrismaClient.ts
Expand Up @@ -282,6 +282,13 @@ export interface GetPrismaClientConfig {
* @remarks used to error for Vercel/Netlify for schema caching issues
*/
ciName?: string

/**
* Information about whether we have not found a schema.prisma file in the
* default location, and that we fell back to finding the schema.prisma file
* in the current working directory. This usually means it has been bundled.
*/
isBundled?: boolean
}

const TX_ID = Symbol.for('prisma.client.transaction.id')
Expand Down Expand Up @@ -440,6 +447,7 @@ export function getPrismaClient(config: GetPrismaClientConfig) {
tracingConfig: this._tracingConfig,
logEmitter: logEmitter,
engineProtocol,
isBundled: config.isBundled,
}

debug('clientVersion', config.clientVersion)
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/runtime/index.ts
Expand Up @@ -20,7 +20,6 @@ export { objectEnumValues } from './object-enums'
export { makeDocument, PrismaClientValidationError, transformDocument, unpack } from './query'
export { makeStrictEnum } from './strictEnum'
export type { DecimalJsLike } from './utils/decimalJsLike'
export { findSync } from './utils/find'
export { NotFoundError } from './utils/rejectOnNotFound'
export { warnEnvConflicts } from './warnEnvConflicts'
export { Debug } from '@prisma/debug'
Expand All @@ -40,6 +39,7 @@ export { decompressFromBase64 }

export { Types }
export { Extensions }
export { warnOnce } from '@prisma/internals'

/**
* Payload is already exported via Types but tsc will complain that it isn't reachable
Expand Down

0 comments on commit 168771a

Please sign in to comment.