Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes resolveConfigFile usage for 3.1.1 #3335

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down Expand Up @@ -114,7 +114,7 @@ export class ModuleResolver implements ModuleResolverInterface {
return pkgFilePath;
}
},
{ cwd: modulePath }
{ cwd: modulePath },
);

if (!packageJsonPath) {
Expand Down Expand Up @@ -148,15 +148,15 @@ 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<PrettierNodeModule | PrettierInstance | undefined> {
if (!workspace.isTrusted) {
this.loggingService.logDebug(UNTRUSTED_WORKSPACE_USING_BUNDLED_PRETTIER);
return prettier;
}

const { prettierPath, resolveGlobalModules } = getConfig(
Uri.file(fileName)
Uri.file(fileName),
);

// Look for local module
Expand All @@ -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);

Expand All @@ -204,7 +204,7 @@ export class ModuleResolver implements ModuleResolverInterface {
if (resolvedGlobalPackageManagerPath) {
const globalModulePath = path.join(
resolvedGlobalPackageManagerPath,
"prettier"
"prettier",
);
if (fs.existsSync(globalModulePath)) {
modulePath = globalModulePath;
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -277,7 +277,7 @@ export class ModuleResolver implements ModuleResolverInterface {

public async getResolvedIgnorePath(
fileName: string,
ignorePath: string
ignorePath: string,
): Promise<string | undefined> {
const cacheKey = `${fileName}:${ignorePath}`;
// cache resolvedIgnorePath because resolving it checks the file system
Expand Down Expand Up @@ -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;
Expand All @@ -322,30 +322,51 @@ 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<string | null>;
resolveConfig(
fileName: string,
options?: prettier.ResolveConfigOptions
options?: prettier.ResolveConfigOptions,
): Promise<PrettierOptions | null>;
},
uri: Uri,
fileName: string,
vscodeConfig: PrettierVSCodeConfig
vscodeConfig: PrettierVSCodeConfig,
): Promise<"error" | "disabled" | PrettierOptions | null> {
const isVirtual = uri.scheme !== "file" && uri.scheme !== "vscode-userdata";

let configPath: string | undefined;
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";
}
Expand All @@ -367,15 +388,15 @@ export class ModuleResolver implements ModuleResolverInterface {
} catch (error) {
this.loggingService.logError(
"Invalid prettier configuration file detected.",
error
error,
);
this.loggingService.logError(INVALID_PRETTIER_CONFIG);

return "error";
}
if (resolveConfigOptions.config) {
this.loggingService.logInfo(
`Using config file at ${resolveConfigOptions.config}`
`Using config file at ${resolveConfigOptions.config}`,
);
}

Expand All @@ -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";
}
Expand All @@ -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;
Expand All @@ -404,7 +425,7 @@ export class ModuleResolver implements ModuleResolverInterface {
prettierInstance,
uri,
fileName,
vscodeConfig
vscodeConfig,
);

return resolvedConfig;
Expand Down Expand Up @@ -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
Expand All @@ -487,7 +508,7 @@ export class ModuleResolver implements ModuleResolverInterface {
return findUp.stop;
}
},
{ cwd: finalPath, type: "directory" }
{ cwd: finalPath, type: "directory" },
);

if (packageJsonResDir) {
Expand All @@ -507,7 +528,7 @@ export class ModuleResolver implements ModuleResolverInterface {
return findUp.stop;
}
},
{ cwd: finalPath, type: "directory" }
{ cwd: finalPath, type: "directory" },
);

if (nodeModulesResDir) {
Expand Down
Loading