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

Format commands fail using Prettier v3 on NX 16.7.3 #18791

Closed
1 of 4 tasks
pvds opened this issue Aug 23, 2023 · 10 comments
Closed
1 of 4 tasks

Format commands fail using Prettier v3 on NX 16.7.3 #18791

pvds opened this issue Aug 23, 2023 · 10 comments
Labels
blocked: retry with latest Retry with latest release or head. outdated scope: misc Misc issues type: bug

Comments

@pvds
Copy link
Contributor

pvds commented Aug 23, 2023

Current Behavior

When running nx format:check or nx format:write the following error is thrown:
Cannot find module '/monorepo/node_modules/prettier/bin-prettier'

This is because Prettier 3 changed the node_module structure as documented in the Prettier 3 release notes.

So this is the fix to support Prettier 3, not taking in account backwards compatibility with older prettier versions:
-const PRETTIER_PATH = require.resolve('prettier/bin-prettier');
+const PRETTIER_PATH = require.resolve('prettier/bin/prettier.cjs');

Expected Behavior

Correct prettier 3 path is used, possibly with backwards compatibility for the prettier 2 path.

GitHub Repo

https://github.com/nrwl/nx-examples with nx 16.7.3 and prettier 3

Steps to Reproduce

  1. Clone https://github.com/nrwl/nx-examples and checkout test/nx-prettier-3 branch
  2. run nx format:check or nx format:write
  3. receive module not found error

Nx Report

Node   : 18.16.0
OS     : darwin-arm64
npm    : 9.8.1

nx                 : 16.7.3
@nx/js             : 16.7.3
@nx/jest           : 16.7.3
@nx/linter         : 16.7.3
@nx/workspace      : 16.7.3
@nx/angular        : 16.7.3
@nx/cypress        : 16.7.3
@nx/devkit         : 16.7.3
@nx/eslint-plugin  : 16.7.3
@nx/node           : 16.7.3
@nx/plugin         : 16.7.3
@nx/storybook      : 16.7.3
@nrwl/tao          : 16.7.3
@nx/web            : 16.7.3
@nx/webpack        : 16.7.3
nx-cloud           : 16.3.0
typescript         : 5.1.6
   ---------------------------------------
Community plugins:
@compodoc/compodoc   : 1.1.21
@ngrx/effects        : 16.2.0
@ngrx/router-store   : 16.2.0
@ngrx/store          : 16.2.0
@ngrx/store-devtools : 16.2.0
@storybook/angular   : 7.3.2
@twittwer/compodoc   : 1.10.0
ng-mocks             : 14.11.0
nx-stylelint         : 15.0.0

Failure Logs

Error: Cannot find module '/monorepo/node_modules/prettier/bin-prettier'
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:1096:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1089:15)
    at resolveExports (node:internal/modules/cjs/loader:565:14)
    at Module._findPath (node:internal/modules/cjs/loader:634:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1061:27)
    at Function.resolve (node:internal/modules/cjs/helpers:116:19)
    at Object.<anonymous> (/monorepo/node_modules/nx/src/command-line/format/format.js:18:31)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32) {
  code: 'MODULE_NOT_FOUND',

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

For reference this is the current patch i'm using:

diff --git a/node_modules/nx/src/command-line/format/format.js b/node_modules/nx/src/command-line/format/format.js
index 215fd55..87f0aa9 100644
--- a/node_modules/nx/src/command-line/format/format.js
+++ b/node_modules/nx/src/command-line/format/format.js
@@ -15,7 +15,7 @@ const affected_project_graph_1 = require("../../project-graph/affected/affected-
 const configuration_1 = require("../../config/configuration");
 const chunkify_1 = require("../../utils/chunkify");
 const all_file_data_1 = require("../../utils/all-file-data");
-const PRETTIER_PATH = require.resolve('prettier/bin-prettier');
+const PRETTIER_PATH = require.resolve('prettier/bin/prettier.cjs');
 function format(command, args) {
     return tslib_1.__awaiter(this, void 0, void 0, function* () {
         const { nxArgs } = (0, command_line_utils_1.splitArgsIntoNxArgsAndOverrides)(args, 'affected', { printWarnings: false }, (0, configuration_1.readNxJson)());
@pvds pvds added the type: bug label Aug 23, 2023
@pvds pvds changed the title All nx format requires incorrect file for Prettier v3 on NX 16.7.3 Format commands fail using Prettier v3 on NX 16.7.3 Aug 23, 2023
@pvds
Copy link
Contributor Author

pvds commented Aug 23, 2023

I see that #18664 was recently merged for prettier 3 compatibility, this implies nx supports prettier 3.

My issue already seems to be resolved by #18644 and improved by @Michsior14 in #18672 but according to #18721 errors occur.
I tested beta.2 and see the error but if I only apply the same fix on 16.7.3 it does work without errors:

diff --git a/node_modules/nx/src/command-line/format/format.js b/node_modules/nx/src/command-line/format/format.js
index 215fd55..3dcd756 100644
--- a/node_modules/nx/src/command-line/format/format.js
+++ b/node_modules/nx/src/command-line/format/format.js
@@ -15,7 +15,12 @@ const affected_project_graph_1 = require("../../project-graph/affected/affected-
 const configuration_1 = require("../../config/configuration");
 const chunkify_1 = require("../../utils/chunkify");
 const all_file_data_1 = require("../../utils/all-file-data");
-const PRETTIER_PATH = require.resolve('prettier/bin-prettier');
+const package_json_1 = require("../../utils/package-json");
+const PRETTIER_PATH = getPrettierPath();
+function getPrettierPath() {
+  const { bin } = (0, package_json_1.readModulePackageJson)('prettier').packageJson;
+  return require.resolve(path.join('prettier', bin));
+}
 function format(command, args) {
     return tslib_1.__awaiter(this, void 0, void 0, function* () {
         const { nxArgs } = (0, command_line_utils_1.splitArgsIntoNxArgsAndOverrides)(args, 'affected', { printWarnings: false }, (0, configuration_1.readNxJson)());

@AgentEnder maybe better to release the prettier fix in 16.7.4?

@Michsior14
Copy link
Contributor

This seems to be kind of duplicate of #17990.

@FrozenPandaz
Copy link
Collaborator

Can you please try again with 16.7.4? I released some prettier@3 fixes there.

@FrozenPandaz FrozenPandaz added blocked: retry with latest Retry with latest release or head. scope: misc Misc issues labels Aug 23, 2023
@pvds
Copy link
Contributor Author

pvds commented Aug 25, 2023

@Michsior14 you are right, didn't find that issue since it was closed already while the 16.7.3 and 16.8.0.beta.2 were not fully compatible with Prettier 3 yet (the beta.2 gives the error stated in #18721)

@FrozenPandaz I removed my patch and 16.7.4 is working perfectly!

Thanks and apologies in case I caused some noise by opening a new issue for this.

@pvds pvds closed this as completed Aug 25, 2023
@gcko
Copy link

gcko commented Sep 1, 2023

@pvds I don't believe this has been resolved. when running nx format:check i receive the following error:

var prettierPromise = import("./index.mjs");
                      ^

TypeError: Invalid host defined options
    at Object.<anonymous> (/<HIDDEN>/node_modules/.pnpm/prettier@3.0.3/node_modules/prettier/index.cjs:600:23)
    at Module._compile (/<HIDDEN>/node_modules/.pnpm/v8-compile-cache@2.3.0/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at Mod.require (/<HIDDEN>/node_modules/.pnpm/nx@16.7.4_@swc+core@1.3.81/node_modules/nx/bin/init-local.js:203:36)
    at require (/<HIDDEN>/node_modules/.pnpm/v8-compile-cache@2.3.0/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.<anonymous> (/<HIDDEN>/node_modules/.pnpm/nx@16.7.4_@swc+core@1.3.81/node_modules/nx/src/command-line/format/format.js:11:18)
    at Module._compile (/<HIDDEN>/node_modules/.pnpm/v8-compile-cache@2.3.0/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)

output of nx report:

 >  NX   Report complete - copy this into the issue template

   Node   : 20.5.1
   OS     : darwin-x64
   pnpm   : 8.7.0
   
   nx                 : 16.7.4
   @nx/js             : 16.7.4
   @nx/jest           : 16.7.4
   @nx/linter         : 16.7.4
   @nx/workspace      : 16.7.4
   @nx/cypress        : 16.7.4
   @nx/devkit         : 16.7.4
   @nx/eslint-plugin  : 16.7.4
   @nx/next           : 16.7.4
   @nx/react          : 16.7.4
   @nrwl/tao          : 16.7.4
   @nx/web            : 16.7.4
   nx-cloud           : 16.3.0
   typescript         : 5.2.2
   ---------------------------------------
   Community plugins:
   @koliveira15/nx-sonarqube : 3.1.1
   nx-stylelint              : 15.0.0

@evangalen
Copy link

evangalen commented Sep 5, 2023

@gcko
I found a workaround 🎉

After Googling on node import "Invalid host defined options", I found the following Tweet from Nicholas C. Zakas:
https://twitter.com/slicknet/status/1559619176230047744

As it turns out v8-compile-cache is in the "dependencies" of the NPM nx package.

Since I could not actually remove v8-compile-cache, I investigated the README of this package and it turns out you can simply use the DISABLE_V8_COMPILE_CACHE=1 environment variable to disable it.

So the following did the trick for me on the command-line:

DISABLE_V8_COMPILE_CACHE=1 nx format:check

@gcko
Copy link

gcko commented Sep 6, 2023

Nice sleuthing, @evangalen ! I'll give it a try 🙏

--- EDIT ---

And lo' and behold, IT WORKS! Thanks for this! 🥇

@MartinDavi
Copy link

Confirming both that the issue remains and that the DISABLE_V8_COMPILE_CACHE=1 fixes it. Should this be reopened @FrozenPandaz ?

@AgentEnder
Copy link
Member

There is a fix merged that will be in the next release for the v8 compile cache

@github-actions
Copy link

github-actions bot commented Oct 8, 2023

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: retry with latest Retry with latest release or head. outdated scope: misc Misc issues type: bug
Projects
None yet
Development

No branches or pull requests

7 participants