-
Notifications
You must be signed in to change notification settings - Fork 48
/
ModuleResolver.ts
381 lines (341 loc) · 11.5 KB
/
ModuleResolver.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { execSync } from "child_process"
import { TextDocument, Uri, window } from "coc.nvim"
import * as findUp from "find-up"
import * as fs from "fs"
import * as path from "path"
import type * as prettier from "prettier"
import * as resolve from "resolve"
import * as semver from "semver"
import { resolveGlobalNodePath, resolveGlobalYarnPath } from "./Files"
import { LoggingService } from "./LoggingService"
import {
FAILED_TO_LOAD_MODULE_MESSAGE,
INVALID_PRETTIER_CONFIG,
INVALID_PRETTIER_PATH_MESSAGE,
OUTDATED_PRETTIER_VERSION_MESSAGE,
USING_BUNDLED_PRETTIER
} from "./message"
import {
ModuleResolverInterface,
PackageManagers,
PrettierOptions,
PrettierResolveConfigOptions,
PrettierVSCodeConfig
} from "./types"
import { getConfig, getWorkspaceRelativePath } from "./util"
const minPrettierVersion = "1.13.0"
export type PrettierNodeModule = typeof prettier
const globalPaths: {
[key: string]: { cache: string | undefined; get(): string | undefined }
} = {
npm: {
cache: undefined,
get(): string | undefined {
return resolveGlobalNodePath()
},
},
pnpm: {
cache: undefined,
get(): string {
const pnpmPath = execSync("pnpm root -g").toString().trim()
return pnpmPath
},
},
yarn: {
cache: undefined,
get(): string | undefined {
return resolveGlobalYarnPath()
},
},
}
function globalPathGet(packageManager: PackageManagers): string | undefined {
const pm = globalPaths[packageManager]
if (pm) {
if (pm.cache === undefined) {
pm.cache = pm.get()
}
return pm.cache
}
return undefined
}
export class ModuleResolver implements ModuleResolverInterface {
private findPkgCache: Map<string, string>
private path2Module = new Map<string, PrettierNodeModule>();
constructor(private loggingService: LoggingService) {
this.findPkgCache = new Map()
}
public async getGlobalPrettierInstance(): Promise<PrettierNodeModule> {
return await import('prettier')
}
/**
* Returns an instance of the prettier module.
* @param fileName The path of the file to use as the starting point. If none provided, the bundled prettier will be used.
*/
public async getPrettierInstance(
fileName: string
): Promise<PrettierNodeModule | undefined> {
const { prettierPath, resolveGlobalModules, onlyUseLocalVersion } = getConfig(
Uri.file(fileName)
)
// Look for local module
let modulePath: string | undefined = undefined
try {
modulePath = prettierPath
? getWorkspaceRelativePath(fileName, prettierPath)
: this.findPkg(fileName, "prettier")
} catch (error) {
let moduleDirectory = ""
if (!modulePath && error instanceof Error) {
// If findPkg threw an error from `resolve.sync`, attempt to parse the
// directory it failed on to provide a better error message
const resolveSyncPathRegex = /Cannot find module '.*' from '(.*)'/
const resolveErrorMatches = resolveSyncPathRegex.exec(error.message)
if (resolveErrorMatches && resolveErrorMatches[1]) {
moduleDirectory = resolveErrorMatches[1]
}
}
this.loggingService.logInfo(
`Attempted to determine module path from ${modulePath || moduleDirectory || "package.json"
}`
)
this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error)
// Return here because there is a local module, but we can't resolve it.
// Must do NPM install for prettier to work.
return undefined
}
// If global modules allowed, look for global module
if (resolveGlobalModules && !modulePath) {
let items: PackageManagers[] = ['npm', 'pnpm', 'yarn']
const idx = await window.showMenuPicker(items, { title: 'Choose package manager' })
if (idx !== -1) {
const packageManager = items[idx]
const resolvedGlobalPackageManagerPath = globalPathGet(packageManager)
if (resolvedGlobalPackageManagerPath) {
const globalModulePath = path.join(
resolvedGlobalPackageManagerPath,
"prettier"
)
if (fs.existsSync(globalModulePath)) {
modulePath = globalModulePath
}
}
}
}
let moduleInstance: PrettierNodeModule | undefined = undefined
if (modulePath !== undefined) {
// First check module cache
moduleInstance = this.path2Module.get(modulePath)
if (moduleInstance) {
this.loggingService.logDebug(
`Local prettier module path: '${modulePath}'`
)
return moduleInstance
} else {
try {
moduleInstance = this.loadNodeModule<PrettierNodeModule>(modulePath)
if (moduleInstance) {
this.path2Module.set(modulePath, moduleInstance)
}
} catch (error) {
this.loggingService.logInfo(
`Attempted to load Prettier module from ${modulePath || "package.json"
}`
)
this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error)
// Returning here because module didn't load.
return undefined
}
}
}
if (moduleInstance) {
// If the instance is missing `format`, it's probably
// not an instance of Prettier
const isPrettierInstance = !!moduleInstance.format
const isValidVersion =
moduleInstance.version &&
!!moduleInstance.getSupportInfo &&
!!moduleInstance.getFileInfo &&
!!moduleInstance.resolveConfig &&
semver.gte(moduleInstance.version, minPrettierVersion)
if (!isPrettierInstance && prettierPath) {
this.loggingService.logError(INVALID_PRETTIER_PATH_MESSAGE)
return undefined
}
if (!isValidVersion) {
this.loggingService.logInfo(
`Attempted to load Prettier module from ${modulePath}`
)
this.loggingService.logError(OUTDATED_PRETTIER_VERSION_MESSAGE)
return undefined
}
return moduleInstance
} else {
if (onlyUseLocalVersion) {
this.loggingService.logInfo('Ignored bundled prettier by onlyUseLocalVersion configuration.')
return undefined
}
this.loggingService.logDebug(USING_BUNDLED_PRETTIER)
return await import('prettier')
}
}
public async getResolvedConfig(
textDocument: TextDocument,
vscodeConfig: PrettierVSCodeConfig
): Promise<"error" | "disabled" | PrettierOptions | null> {
const uri = Uri.parse(textDocument.uri)
const fileName = uri.fsPath
const isVirtual = uri.scheme !== "file"
let configPath: string | undefined
try {
if (!isVirtual) {
configPath = (await (await import('prettier')).resolveConfigFile(fileName)) ?? undefined
}
} catch (error) {
this.loggingService.logError(
`Error resolving prettier configuration for ${fileName}`,
error
)
return "error"
}
const resolveConfigOptions: PrettierResolveConfigOptions = {
config: isVirtual
? undefined
: vscodeConfig.configPath
? getWorkspaceRelativePath(fileName, vscodeConfig.configPath)
: configPath,
editorconfig: isVirtual ? undefined : vscodeConfig.useEditorConfig,
}
let resolvedConfig: PrettierOptions | null
try {
resolvedConfig = isVirtual
? null
: await (await import('prettier')).resolveConfig(fileName, resolveConfigOptions)
} catch (error) {
this.loggingService.logError(
"Invalid prettier configuration file detected.",
error
)
this.loggingService.logError(INVALID_PRETTIER_CONFIG)
return "error"
}
if (resolveConfigOptions.config) {
this.loggingService.logInfo(
`Using config file at '${resolveConfigOptions.config}'`
)
}
if (!isVirtual && !resolvedConfig && vscodeConfig.requireConfig) {
this.loggingService.logInfo(
"Require config set to true and no config present. Skipping file."
)
return "disabled"
}
return resolvedConfig
}
/**
* Clears the module and config cache
*/
public async dispose() {
(await import('prettier')).clearConfigCache()
this.path2Module.forEach((module) => {
try {
module.clearConfigCache()
} catch (error) {
this.loggingService.logError("Error clearing module cache.", error)
}
})
this.path2Module.clear()
}
// Source: https://github.com/microsoft/vscode-eslint/blob/master/server/src/eslintServer.ts
private loadNodeModule<T>(moduleName: string): T | undefined {
try {
return require(moduleName)
} catch (error) {
this.loggingService.logError(
`Error loading node module '${moduleName}'`,
error
)
}
return undefined
}
private isInternalTestRoot(dir: string): boolean {
if (process.env.NODE_ENV !== "production") {
// This is for testing purposes only. This code is removed in the
// shipped version of this extension so do not use this in your
// project. It won't work.
return fs.existsSync(path.join(dir, ".do-not-use-prettier-vscode-root"))
} else {
return false
}
}
/**
* Recursively search upwards for a given module definition based on
* package.json or node_modules existence
* @param {string} fsPath file system path to start searching from
* @param {string} pkgName package's name to search for
* @returns {string} resolved path to module
*/
private findPkg(fsPath: string, pkgName: string): string | undefined {
const cacheKey = `${fsPath}:${pkgName}`
const packagePathState = this.findPkgCache.get(cacheKey)
if (packagePathState) {
return packagePathState
}
// Only look for a module definition outside of any `node_modules` directories
const splitPath = fsPath.split("/")
let finalPath = fsPath
const nodeModulesIndex = splitPath.indexOf("node_modules")
if (nodeModulesIndex > 1) {
finalPath = splitPath.slice(0, nodeModulesIndex).join("/")
}
// First look for an explicit package.json dep
const packageJsonResDir = findUp.sync(
(dir) => {
if (fs.existsSync(path.join(dir, "package.json"))) {
let packageJson
try {
packageJson = JSON.parse(
fs.readFileSync(path.join(dir, "package.json"), "utf8")
)
} catch (e) {
// Swallow, if we can't read it we don't want to resolve based on it
}
if (
packageJson &&
((packageJson.dependencies && packageJson.dependencies[pkgName]) ||
(packageJson.devDependencies &&
packageJson.devDependencies[pkgName]))
) {
return dir
}
}
if (this.isInternalTestRoot(dir)) {
return findUp.stop
}
},
{ cwd: finalPath, type: "directory" }
)
if (packageJsonResDir) {
const packagePath = resolve.sync(pkgName, { basedir: packageJsonResDir })
this.findPkgCache.set(cacheKey, packagePath)
return packagePath
}
// If no explicit package.json dep found, instead look for implicit dep
const nodeModulesResDir = findUp.sync(
(dir) => {
if (fs.existsSync(path.join(dir, "node_modules", pkgName))) {
return dir
}
if (this.isInternalTestRoot(dir)) {
return findUp.stop
}
},
{ cwd: finalPath, type: "directory" }
)
if (nodeModulesResDir) {
const packagePath = resolve.sync(pkgName, { basedir: nodeModulesResDir })
this.findPkgCache.set(cacheKey, packagePath)
return packagePath
}
return
}
}