-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DefaultLibraryLoader.ts
184 lines (161 loc) · 7.22 KB
/
DefaultLibraryLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import Debug from '@prisma/debug'
import { getEnginesPath } from '@prisma/engines'
import type { Platform } from '@prisma/get-platform'
import { getNodeAPIName, getPlatform, getPlatformWithOSResult } from '@prisma/get-platform'
import chalk from 'chalk'
import fs from 'fs'
import path from 'path'
import { EngineConfig } from '../common/Engine'
import { PrismaClientInitializationError } from '../common/errors/PrismaClientInitializationError'
import { handleLibraryLoadingErrors } from '../common/errors/utils/handleEngineLoadingErrors'
import { printGeneratorConfig } from '../common/utils/printGeneratorConfig'
import { fixBinaryTargets } from '../common/utils/util'
import { runInChildSpan } from '../tracing'
import { Library, LibraryLoader } from './types/Library'
const debug = Debug('prisma:client:libraryEngine:loader')
export function load<T>(id: string): T {
// this require needs to be resolved at runtime, tell webpack to ignore it
return eval('require')(id) as T
}
export class DefaultLibraryLoader implements LibraryLoader {
private config: EngineConfig
private libQueryEnginePath: string | null = null
private platform: Platform | null = null
constructor(config: EngineConfig) {
this.config = config
}
async loadLibrary(): Promise<Library> {
const platformInfo = await getPlatformWithOSResult()
this.platform = platformInfo.binaryTarget
if (!this.libQueryEnginePath) {
this.libQueryEnginePath = await this.getLibQueryEnginePath()
}
debug(`loadEngine using ${this.libQueryEnginePath}`)
try {
const enginePath = this.libQueryEnginePath
return runInChildSpan({ name: 'loadLibrary', enabled: this.config.tracingConfig.enabled, internal: true }, () =>
load(enginePath),
)
} catch (e) {
const errorMessage = handleLibraryLoadingErrors({
e: e as Error,
platformInfo,
id: this.libQueryEnginePath,
})
throw new PrismaClientInitializationError(errorMessage, this.config.clientVersion!)
}
}
private async getLibQueryEnginePath(): Promise<string> {
const libPath = process.env.PRISMA_QUERY_ENGINE_LIBRARY ?? this.config.prismaPath
if (libPath && fs.existsSync(libPath) && libPath.endsWith('.node')) {
return libPath
}
this.platform = this.platform ?? (await getPlatform())
const { enginePath, searchedLocations } = await this.resolveEnginePath()
// If path to query engine doesn't exist, throw
if (!fs.existsSync(enginePath)) {
const incorrectPinnedPlatformErrorStr = this.platform
? `\nYou incorrectly pinned it to ${chalk.redBright.bold(`${this.platform}`)}\n`
: ''
// TODO Stop searching in many locations, have more deterministic logic.
let errorText = `Query engine library for current platform "${chalk.bold(
this.platform,
)}" could not be found.${incorrectPinnedPlatformErrorStr}
This probably happens, because you built Prisma Client on a different platform.
(Prisma Client looked in "${chalk.underline(enginePath)}")
Searched Locations:
${searchedLocations
.map((f) => {
let msg = ` ${f}`
if (process.env.DEBUG === 'node-engine-search-locations' && fs.existsSync(f)) {
const dir = fs.readdirSync(f)
msg += dir.map((d) => ` ${d}`).join('\n')
}
return msg
})
.join('\n' + (process.env.DEBUG === 'node-engine-search-locations' ? '\n' : ''))}\n`
// The generator should always be there during normal usage
if (this.config.generator) {
// The user already added it, but it still doesn't work 🤷♀️
// That means, that some build system just deleted the files 🤔
this.platform = this.platform ?? (await getPlatform())
if (
this.config.generator.binaryTargets.find((object) => object.value === this.platform!) ||
this.config.generator.binaryTargets.find((object) => object.value === 'native')
) {
errorText += `
You already added the platform${
this.config.generator.binaryTargets.length > 1 ? 's' : ''
} ${this.config.generator.binaryTargets
.map((t) => `"${chalk.bold(t.value)}"`)
.join(', ')} to the "${chalk.underline('generator')}" block
in the "schema.prisma" file as described in https://pris.ly/d/client-generator,
but something went wrong. That's suboptimal.
Please create an issue at https://github.com/prisma/prisma/issues/new`
errorText += ``
} else {
// If they didn't even have the current running platform in the schema.prisma file, it's easy
// Just add it
errorText += `\n\nTo solve this problem, add the platform "${this.platform}" to the "${chalk.underline(
'binaryTargets',
)}" attribute in the "${chalk.underline('generator')}" block in the "schema.prisma" file:
${chalk.greenBright(this.getFixedGenerator())}
Then run "${chalk.greenBright('prisma generate')}" for your changes to take effect.
Read more about deploying Prisma Client: https://pris.ly/d/client-generator`
}
} else {
errorText += `\n\nRead more about deploying Prisma Client: https://pris.ly/d/client-generator\n`
}
throw new PrismaClientInitializationError(errorText, this.config.clientVersion!)
}
this.platform = this.platform ?? (await getPlatform())
return enginePath
}
private async resolveEnginePath(): Promise<{
enginePath: string
searchedLocations: string[]
}> {
const searchedLocations: string[] = []
let enginePath: string
if (this.libQueryEnginePath) {
return { enginePath: this.libQueryEnginePath, searchedLocations }
}
this.platform = this.platform ?? (await getPlatform())
// TODO Why special case dependent on file name?
if (__filename.includes('DefaultLibraryLoader')) {
enginePath = path.join(getEnginesPath(), getNodeAPIName(this.platform, 'fs'))
return { enginePath, searchedLocations }
}
const dirname = eval('__dirname') as string
const searchLocations: string[] = [
// TODO: why hardcoded path? why not look for .prisma/client upwards?
path.resolve(dirname, '../../../.prisma/client'), // Dot Prisma Path
this.config.generator?.output?.value ?? dirname, // Custom Generator Path
path.resolve(dirname, '..'), // parentDirName
path.dirname(this.config.datamodelPath), // Datamodel Dir
this.config.cwd, //cwdPath
'/tmp/prisma-engines',
]
if (this.config.dirname) {
searchLocations.push(this.config.dirname)
}
for (const location of searchLocations) {
searchedLocations.push(location)
debug(`Searching for Query Engine Library in ${location}`)
enginePath = path.join(location, getNodeAPIName(this.platform, 'fs'))
if (fs.existsSync(enginePath)) {
return { enginePath, searchedLocations }
}
}
enginePath = path.join(__dirname, getNodeAPIName(this.platform, 'fs'))
return { enginePath, searchedLocations }
}
// TODO Fixed as in "not broken" or fixed as in "written down"? If any of these, why and how and where?
private getFixedGenerator(): string {
const fixedGenerator = {
...this.config.generator!,
binaryTargets: fixBinaryTargets(this.config.generator!.binaryTargets, this.platform!),
}
return printGeneratorConfig(fixedGenerator)
}
}