Skip to content

Commit 0016044

Browse files
authored
Fixes resolveConfigFile usage for 3.1.1 (#3335)
1 parent 22e15b2 commit 0016044

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

src/ModuleResolver.ts

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type PrettierNodeModule = typeof prettier;
3636
const origFsStatSync = fs.statSync;
3737
const fsStatSyncWorkaround = (
3838
path: fs.PathLike,
39-
options: fs.StatSyncOptions
39+
options: fs.StatSyncOptions,
4040
) => {
4141
if (
4242
options?.throwIfNoEntry === true ||
@@ -114,7 +114,7 @@ export class ModuleResolver implements ModuleResolverInterface {
114114
return pkgFilePath;
115115
}
116116
},
117-
{ cwd: modulePath }
117+
{ cwd: modulePath },
118118
);
119119

120120
if (!packageJsonPath) {
@@ -148,15 +148,15 @@ export class ModuleResolver implements ModuleResolverInterface {
148148
* @param fileName The path of the file to use as the starting point. If none provided, the bundled prettier will be used.
149149
*/
150150
public async getPrettierInstance(
151-
fileName: string
151+
fileName: string,
152152
): Promise<PrettierNodeModule | PrettierInstance | undefined> {
153153
if (!workspace.isTrusted) {
154154
this.loggingService.logDebug(UNTRUSTED_WORKSPACE_USING_BUNDLED_PRETTIER);
155155
return prettier;
156156
}
157157

158158
const { prettierPath, resolveGlobalModules } = getConfig(
159-
Uri.file(fileName)
159+
Uri.file(fileName),
160160
);
161161

162162
// Look for local module
@@ -181,7 +181,7 @@ export class ModuleResolver implements ModuleResolverInterface {
181181
this.loggingService.logInfo(
182182
`Attempted to determine module path from ${
183183
modulePath || moduleDirectory || "package.json"
184-
}`
184+
}`,
185185
);
186186
this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error);
187187

@@ -204,7 +204,7 @@ export class ModuleResolver implements ModuleResolverInterface {
204204
if (resolvedGlobalPackageManagerPath) {
205205
const globalModulePath = path.join(
206206
resolvedGlobalPackageManagerPath,
207-
"prettier"
207+
"prettier",
208208
);
209209
if (fs.existsSync(globalModulePath)) {
210210
modulePath = globalModulePath;
@@ -239,7 +239,7 @@ export class ModuleResolver implements ModuleResolverInterface {
239239
this.loggingService.logInfo(
240240
`Attempted to load Prettier module from ${
241241
modulePath || "package.json"
242-
}`
242+
}`,
243243
);
244244
this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error);
245245

@@ -261,7 +261,7 @@ export class ModuleResolver implements ModuleResolverInterface {
261261

262262
if (!isValidVersion) {
263263
this.loggingService.logInfo(
264-
`Attempted to load Prettier module from ${modulePath}`
264+
`Attempted to load Prettier module from ${modulePath}`,
265265
);
266266
this.loggingService.logError(OUTDATED_PRETTIER_VERSION_MESSAGE);
267267
return undefined;
@@ -277,7 +277,7 @@ export class ModuleResolver implements ModuleResolverInterface {
277277

278278
public async getResolvedIgnorePath(
279279
fileName: string,
280-
ignorePath: string
280+
ignorePath: string,
281281
): Promise<string | undefined> {
282282
const cacheKey = `${fileName}:${ignorePath}`;
283283
// cache resolvedIgnorePath because resolving it checks the file system
@@ -307,7 +307,7 @@ export class ModuleResolver implements ModuleResolverInterface {
307307
// https://stackoverflow.com/questions/17699599/node-js-check-if-file-exists#comment121041700_57708635
308308
await fs.promises.stat(p).then(
309309
() => true,
310-
() => false
310+
() => false,
311311
)
312312
) {
313313
resolvedIgnorePath = p;
@@ -322,30 +322,51 @@ export class ModuleResolver implements ModuleResolverInterface {
322322
return resolvedIgnorePath;
323323
}
324324

325+
private adjustFileNameForPrettierVersion3_1_1(
326+
prettierInstance: { version: string | null },
327+
fileName: string,
328+
) {
329+
if (!prettierInstance.version) {
330+
return fileName;
331+
}
332+
// Avoid https://github.com/prettier/prettier/pull/15363
333+
const isGte3_1_1 = semver.gte(prettierInstance.version, "3.1.1");
334+
if (isGte3_1_1) {
335+
return path.join(fileName, "noop.js");
336+
}
337+
return fileName;
338+
}
339+
325340
public async resolveConfig(
326341
prettierInstance: {
342+
version: string | null;
327343
resolveConfigFile(filePath?: string): Promise<string | null>;
328344
resolveConfig(
329345
fileName: string,
330-
options?: prettier.ResolveConfigOptions
346+
options?: prettier.ResolveConfigOptions,
331347
): Promise<PrettierOptions | null>;
332348
},
333349
uri: Uri,
334350
fileName: string,
335-
vscodeConfig: PrettierVSCodeConfig
351+
vscodeConfig: PrettierVSCodeConfig,
336352
): Promise<"error" | "disabled" | PrettierOptions | null> {
337353
const isVirtual = uri.scheme !== "file" && uri.scheme !== "vscode-userdata";
338354

339355
let configPath: string | undefined;
340356
try {
341357
if (!isVirtual) {
342358
configPath =
343-
(await prettierInstance.resolveConfigFile(fileName)) ?? undefined;
359+
(await prettierInstance.resolveConfigFile(
360+
this.adjustFileNameForPrettierVersion3_1_1(
361+
prettierInstance,
362+
fileName,
363+
),
364+
)) ?? undefined;
344365
}
345366
} catch (error) {
346367
this.loggingService.logError(
347368
`Error resolving prettier configuration for ${fileName}`,
348-
error
369+
error,
349370
);
350371
return "error";
351372
}
@@ -367,15 +388,15 @@ export class ModuleResolver implements ModuleResolverInterface {
367388
} catch (error) {
368389
this.loggingService.logError(
369390
"Invalid prettier configuration file detected.",
370-
error
391+
error,
371392
);
372393
this.loggingService.logError(INVALID_PRETTIER_CONFIG);
373394

374395
return "error";
375396
}
376397
if (resolveConfigOptions.config) {
377398
this.loggingService.logInfo(
378-
`Using config file at ${resolveConfigOptions.config}`
399+
`Using config file at ${resolveConfigOptions.config}`,
379400
);
380401
}
381402

@@ -385,7 +406,7 @@ export class ModuleResolver implements ModuleResolverInterface {
385406

386407
if (!isVirtual && !resolvedConfig && vscodeConfig.requireConfig) {
387408
this.loggingService.logInfo(
388-
"Require config set to true and no config present. Skipping file."
409+
"Require config set to true and no config present. Skipping file.",
389410
);
390411
return "disabled";
391412
}
@@ -395,7 +416,7 @@ export class ModuleResolver implements ModuleResolverInterface {
395416

396417
public async getResolvedConfig(
397418
{ fileName, uri }: TextDocument,
398-
vscodeConfig: PrettierVSCodeConfig
419+
vscodeConfig: PrettierVSCodeConfig,
399420
): Promise<"error" | "disabled" | PrettierOptions | null> {
400421
const prettierInstance: typeof prettier | PrettierInstance =
401422
(await this.getPrettierInstance(fileName)) || prettier;
@@ -404,7 +425,7 @@ export class ModuleResolver implements ModuleResolverInterface {
404425
prettierInstance,
405426
uri,
406427
fileName,
407-
vscodeConfig
428+
vscodeConfig,
408429
);
409430

410431
return resolvedConfig;
@@ -467,7 +488,7 @@ export class ModuleResolver implements ModuleResolverInterface {
467488
let packageJson;
468489
try {
469490
packageJson = JSON.parse(
470-
fs.readFileSync(path.join(dir, "package.json"), "utf8")
491+
fs.readFileSync(path.join(dir, "package.json"), "utf8"),
471492
);
472493
} catch (e) {
473494
// Swallow, if we can't read it we don't want to resolve based on it
@@ -487,7 +508,7 @@ export class ModuleResolver implements ModuleResolverInterface {
487508
return findUp.stop;
488509
}
489510
},
490-
{ cwd: finalPath, type: "directory" }
511+
{ cwd: finalPath, type: "directory" },
491512
);
492513

493514
if (packageJsonResDir) {
@@ -507,7 +528,7 @@ export class ModuleResolver implements ModuleResolverInterface {
507528
return findUp.stop;
508529
}
509530
},
510-
{ cwd: finalPath, type: "directory" }
531+
{ cwd: finalPath, type: "directory" },
511532
);
512533

513534
if (nodeModulesResDir) {

0 commit comments

Comments
 (0)