Skip to content

Commit

Permalink
fix(core): formatter should not fail when absolute paths are provided…
Browse files Browse the repository at this point in the history
… as "--files" (#20331)
  • Loading branch information
dmitry-stepanenko authored Jan 10, 2024
1 parent 7da53c0 commit a42d9ab
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions packages/nx/src/command-line/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { readNxJson } from '../../config/configuration';
import { ProjectGraph } from '../../config/project-graph';
import { chunkify } from '../../utils/chunkify';
import { allFileData } from '../../utils/all-file-data';
import { workspaceRoot } from '../../utils/workspace-root';
import { output } from '../../utils/output';

const PRETTIER_PATH = getPrettierPath();

Expand Down Expand Up @@ -83,20 +85,22 @@ async function getPatterns(
const p = parseFiles(args);

// In prettier v3 the getSupportInfo result is a promise
const supportedExtensions = (
await (prettier.getSupportInfo() as Promise<SupportInfo> | SupportInfo)
).languages
.flatMap((language) => language.extensions)
.filter((extension) => !!extension)
// Prettier supports ".swcrc" as a file instead of an extension
// So we add ".swcrc" as a supported extension manually
// which allows it to be considered for calculating "patterns"
.concat('.swcrc');

const patterns = p.files.filter(
(f) => fileExists(f) && supportedExtensions.includes(path.extname(f))
const supportedExtensions = new Set(
(
await (prettier.getSupportInfo() as Promise<SupportInfo> | SupportInfo)
).languages
.flatMap((language) => language.extensions)
.filter((extension) => !!extension)
// Prettier supports ".swcrc" as a file instead of an extension
// So we add ".swcrc" as a supported extension manually
// which allows it to be considered for calculating "patterns"
.concat('.swcrc')
);

const patterns = p.files
.map((f) => path.relative(workspaceRoot, f))
.filter((f) => fileExists(f) && supportedExtensions.has(path.extname(f)));

// exclude patterns in .nxignore or .gitignore
const nonIgnoredPatterns = getIgnoreObject().filter(patterns);

Expand All @@ -107,7 +111,13 @@ async function getPatterns(
graph
)
: nonIgnoredPatterns;
} catch {
} catch (err) {
output.error({
title:
err?.message ||
'Something went wrong when resolving the list of files for the formatter',
bodyLines: [`Defaulting to all files pattern: "${allFilesPattern}"`],
});
return allFilesPattern;
}
}
Expand Down

0 comments on commit a42d9ab

Please sign in to comment.