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

Fix issue that .prettierignore in monorepo sub folders is not read. (#3003, #3424) #3622

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.vscode-test/
out/
dist/
test-fixtures/
test-fixtures/*
!test-fixtures/plugin-tailwindcss
node_modules/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi

<!-- Check [Keep a Changelog](https://keepachangelog.com/) for recommendations on how to structure this file. -->

## [Unreleased]

- Fix issue that .prettierignore in monorepo sub folders is not read. (#3003, #3424)

## [11.0.0]

- [BREAKING CHANGE] Prevent `.editorconfig` from satisfying the `requireConfig` setting (#2708) - Thanks to [@redoPop](https://github.com/redoPop)
Expand Down
152 changes: 101 additions & 51 deletions src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class ModuleResolver implements ModuleResolverInterface {
return pkgFilePath;
}
},
{ cwd }
{ cwd },
);

if (!packageJsonPath) {
Expand Down Expand Up @@ -289,42 +289,52 @@ export class ModuleResolver implements ModuleResolverInterface {
const cacheKey = `${fileName}:${ignorePath}`;
// cache resolvedIgnorePath because resolving it checks the file system
let resolvedIgnorePath = this.ignorePathCache.get(cacheKey);

if (!resolvedIgnorePath) {
resolvedIgnorePath = getWorkspaceRelativePath(fileName, ignorePath);
// if multiple different workspace folders contain this same file, we
// may have chosen one that doesn't actually contain .prettierignore
if (workspace.workspaceFolders) {
// all workspace folders that contain the file
const folders = workspace.workspaceFolders
.map((folder) => folder.uri.fsPath)
.filter((folder) => {
// https://stackoverflow.com/a/45242825
const relative = path.relative(folder, fileName);
return (
relative &&
!relative.startsWith("..") &&
!path.isAbsolute(relative)
);
})
// sort folders innermost to outermost
.sort((a, b) => b.length - a.length);
for (const folder of folders) {
const p = path.join(folder, ignorePath);
if (
// https://stackoverflow.com/questions/17699599/node-js-check-if-file-exists#comment121041700_57708635
await fs.promises.stat(p).then(
() => true,
() => false,
)
) {
resolvedIgnorePath = p;
break;
// If no cache, check if a sibling .prettierignore file exists firstly
resolvedIgnorePath = await this.resolveSiblingIgnorePath(
fileName,
ignorePath,
);

// If no .prettierignore file is found with the config file, try resolving from the workspace
if (!resolvedIgnorePath) {
resolvedIgnorePath = getWorkspaceRelativePath(fileName, ignorePath);
// if multiple different workspace folders contain this same file, we
// may have chosen one that doesn't actually contain .prettierignore
if (workspace.workspaceFolders) {
// all workspace folders that contain the file
const folders = workspace.workspaceFolders
.map((folder) => folder.uri.fsPath)
.filter((folder) => {
// https://stackoverflow.com/a/45242825
const relative = path.relative(folder, fileName);
return (
relative &&
!relative.startsWith("..") &&
!path.isAbsolute(relative)
);
})
// sort folders innermost to outermost
.sort((a, b) => b.length - a.length);
for (const folder of folders) {
const p = path.join(folder, ignorePath);
if (
// https://stackoverflow.com/questions/17699599/node-js-check-if-file-exists#comment121041700_57708635
await fs.promises.stat(p).then(
() => true,
() => false,
)
) {
resolvedIgnorePath = p;
break;
}
}
}
}
}
if (resolvedIgnorePath) {
this.ignorePathCache.set(cacheKey, resolvedIgnorePath);
if (resolvedIgnorePath) {
this.ignorePathCache.set(cacheKey, resolvedIgnorePath);
}
}
return resolvedIgnorePath;
}
Expand Down Expand Up @@ -359,24 +369,9 @@ export class ModuleResolver implements ModuleResolverInterface {
): 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(
this.adjustFileNameForPrettierVersion3_1_1(
prettierInstance,
fileName,
),
)) ?? undefined;
}
} catch (error) {
this.loggingService.logError(
`Error resolving prettier configuration for ${fileName}`,
error,
);
return "error";
}
const configPath = isVirtual
? undefined
: await this.resolvedConfigPath(prettierInstance, fileName);

const resolveConfigOptions: PrettierResolveConfigOptions = {
config: isVirtual
Expand Down Expand Up @@ -459,6 +454,61 @@ export class ModuleResolver implements ModuleResolverInterface {
this.path2Module.clear();
}

private async resolveSiblingIgnorePath(fileName: string, ignorePath: string) {
let resolvedPath: string | undefined;

const prettierInstance: typeof prettier | PrettierInstance =
(await this.getPrettierInstance(fileName)) || prettier;

const configPath = await this.resolvedConfigPath(
prettierInstance,
fileName,
);
if (configPath) {
const siblingIgnorePath = path.join(path.dirname(configPath), ignorePath);
if (
await fs.promises.stat(siblingIgnorePath).then(
() => true,
() => false,
)
) {
resolvedPath = siblingIgnorePath;
}
}
return resolvedPath;
}

private async resolvedConfigPath(
prettierInstance: {
version: string | null;
resolveConfigFile(filePath?: string): Promise<string | null>;
resolveConfig(
fileName: string,
options?: prettier.ResolveConfigOptions,
): Promise<PrettierOptions | null>;
},
fileName: string,
) {
let configPath: string | undefined;
try {
configPath =
(await prettierInstance.resolveConfigFile(
this.adjustFileNameForPrettierVersion3_1_1(
prettierInstance,
fileName,
),
)) ?? undefined;
} catch (error) {
this.loggingService.logError(
`Error resolving prettier configuration for ${fileName}`,
error,
);
return "error";
}

return configPath;
}

private isInternalTestRoot(dir: string): boolean {
if (process.env.NODE_ENV !== "production") {
// This is for testing purposes only. This code is removed in the
Expand Down