Skip to content

Commit a020954

Browse files
authored
fix(core): handle various directories when importing prettier (#33383)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior when trying to format with prettier while outside of the workspace (for example in a `tmp` nx installation like is created during `configure-ai-agents`), resolving prettier could fail even though it's present in the actual workspace. ## Expected Behavior resolving prettier works if it's available in either the proper workspace or the tmp one by specifying `paths` in `require.resolve`
1 parent cfcf109 commit a020954

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

packages/nx/src/generators/internal-utils/format-changed-files-with-prettier-if-available.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from 'path';
22
import type * as Prettier from 'prettier';
33
import { isUsingPrettier } from '../../utils/is-using-prettier';
44
import type { Tree } from '../tree';
5+
import { getNxRequirePaths } from '../../utils/installation-directory';
56

67
/**
78
* Formats all the created or updated files using Prettier
@@ -39,7 +40,10 @@ export async function formatFilesWithPrettierIfAvailable(
3940

4041
let prettier: typeof Prettier;
4142
try {
42-
prettier = await import('prettier');
43+
const prettierPath = require.resolve('prettier', {
44+
paths: [...getNxRequirePaths(root), __dirname],
45+
});
46+
prettier = require(prettierPath);
4347
/**
4448
* Even after we discovered prettier in node_modules, we need to be sure that the user is intentionally using prettier
4549
* before proceeding to format with it.

0 commit comments

Comments
 (0)