diff --git a/src/ModuleResolver.ts b/src/ModuleResolver.ts index 757d5c1b3..3b50c624f 100644 --- a/src/ModuleResolver.ts +++ b/src/ModuleResolver.ts @@ -36,7 +36,7 @@ export type PrettierNodeModule = typeof prettier; const origFsStatSync = fs.statSync; const fsStatSyncWorkaround = ( path: fs.PathLike, - options: fs.StatSyncOptions + options: fs.StatSyncOptions, ) => { if ( options?.throwIfNoEntry === true || @@ -114,7 +114,7 @@ export class ModuleResolver implements ModuleResolverInterface { return pkgFilePath; } }, - { cwd: modulePath } + { cwd: modulePath }, ); if (!packageJsonPath) { @@ -148,7 +148,7 @@ export class ModuleResolver implements ModuleResolverInterface { * @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 + fileName: string, ): Promise { if (!workspace.isTrusted) { this.loggingService.logDebug(UNTRUSTED_WORKSPACE_USING_BUNDLED_PRETTIER); @@ -156,7 +156,7 @@ export class ModuleResolver implements ModuleResolverInterface { } const { prettierPath, resolveGlobalModules } = getConfig( - Uri.file(fileName) + Uri.file(fileName), ); // Look for local module @@ -181,7 +181,7 @@ export class ModuleResolver implements ModuleResolverInterface { this.loggingService.logInfo( `Attempted to determine module path from ${ modulePath || moduleDirectory || "package.json" - }` + }`, ); this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error); @@ -204,7 +204,7 @@ export class ModuleResolver implements ModuleResolverInterface { if (resolvedGlobalPackageManagerPath) { const globalModulePath = path.join( resolvedGlobalPackageManagerPath, - "prettier" + "prettier", ); if (fs.existsSync(globalModulePath)) { modulePath = globalModulePath; @@ -239,7 +239,7 @@ export class ModuleResolver implements ModuleResolverInterface { this.loggingService.logInfo( `Attempted to load Prettier module from ${ modulePath || "package.json" - }` + }`, ); this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error); @@ -261,7 +261,7 @@ export class ModuleResolver implements ModuleResolverInterface { if (!isValidVersion) { this.loggingService.logInfo( - `Attempted to load Prettier module from ${modulePath}` + `Attempted to load Prettier module from ${modulePath}`, ); this.loggingService.logError(OUTDATED_PRETTIER_VERSION_MESSAGE); return undefined; @@ -277,7 +277,7 @@ export class ModuleResolver implements ModuleResolverInterface { public async getResolvedIgnorePath( fileName: string, - ignorePath: string + ignorePath: string, ): Promise { const cacheKey = `${fileName}:${ignorePath}`; // cache resolvedIgnorePath because resolving it checks the file system @@ -307,7 +307,7 @@ export class ModuleResolver implements ModuleResolverInterface { // https://stackoverflow.com/questions/17699599/node-js-check-if-file-exists#comment121041700_57708635 await fs.promises.stat(p).then( () => true, - () => false + () => false, ) ) { resolvedIgnorePath = p; @@ -322,17 +322,33 @@ export class ModuleResolver implements ModuleResolverInterface { return resolvedIgnorePath; } + private adjustFileNameForPrettierVersion3_1_1( + prettierInstance: { version: string | null }, + fileName: string, + ) { + if (!prettierInstance.version) { + return fileName; + } + // Avoid https://github.com/prettier/prettier/pull/15363 + const isGte3_1_1 = semver.gte(prettierInstance.version, "3.1.1"); + if (isGte3_1_1) { + return path.join(fileName, "noop.js"); + } + return fileName; + } + public async resolveConfig( prettierInstance: { + version: string | null; resolveConfigFile(filePath?: string): Promise; resolveConfig( fileName: string, - options?: prettier.ResolveConfigOptions + options?: prettier.ResolveConfigOptions, ): Promise; }, uri: Uri, fileName: string, - vscodeConfig: PrettierVSCodeConfig + vscodeConfig: PrettierVSCodeConfig, ): Promise<"error" | "disabled" | PrettierOptions | null> { const isVirtual = uri.scheme !== "file" && uri.scheme !== "vscode-userdata"; @@ -340,12 +356,17 @@ export class ModuleResolver implements ModuleResolverInterface { try { if (!isVirtual) { configPath = - (await prettierInstance.resolveConfigFile(fileName)) ?? undefined; + (await prettierInstance.resolveConfigFile( + this.adjustFileNameForPrettierVersion3_1_1( + prettierInstance, + fileName, + ), + )) ?? undefined; } } catch (error) { this.loggingService.logError( `Error resolving prettier configuration for ${fileName}`, - error + error, ); return "error"; } @@ -367,7 +388,7 @@ export class ModuleResolver implements ModuleResolverInterface { } catch (error) { this.loggingService.logError( "Invalid prettier configuration file detected.", - error + error, ); this.loggingService.logError(INVALID_PRETTIER_CONFIG); @@ -375,7 +396,7 @@ export class ModuleResolver implements ModuleResolverInterface { } if (resolveConfigOptions.config) { this.loggingService.logInfo( - `Using config file at ${resolveConfigOptions.config}` + `Using config file at ${resolveConfigOptions.config}`, ); } @@ -385,7 +406,7 @@ export class ModuleResolver implements ModuleResolverInterface { if (!isVirtual && !resolvedConfig && vscodeConfig.requireConfig) { this.loggingService.logInfo( - "Require config set to true and no config present. Skipping file." + "Require config set to true and no config present. Skipping file.", ); return "disabled"; } @@ -395,7 +416,7 @@ export class ModuleResolver implements ModuleResolverInterface { public async getResolvedConfig( { fileName, uri }: TextDocument, - vscodeConfig: PrettierVSCodeConfig + vscodeConfig: PrettierVSCodeConfig, ): Promise<"error" | "disabled" | PrettierOptions | null> { const prettierInstance: typeof prettier | PrettierInstance = (await this.getPrettierInstance(fileName)) || prettier; @@ -404,7 +425,7 @@ export class ModuleResolver implements ModuleResolverInterface { prettierInstance, uri, fileName, - vscodeConfig + vscodeConfig, ); return resolvedConfig; @@ -467,7 +488,7 @@ export class ModuleResolver implements ModuleResolverInterface { let packageJson; try { packageJson = JSON.parse( - fs.readFileSync(path.join(dir, "package.json"), "utf8") + 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 @@ -487,7 +508,7 @@ export class ModuleResolver implements ModuleResolverInterface { return findUp.stop; } }, - { cwd: finalPath, type: "directory" } + { cwd: finalPath, type: "directory" }, ); if (packageJsonResDir) { @@ -507,7 +528,7 @@ export class ModuleResolver implements ModuleResolverInterface { return findUp.stop; } }, - { cwd: finalPath, type: "directory" } + { cwd: finalPath, type: "directory" }, ); if (nodeModulesResDir) {