jhipster / prettier-java

Prettier Java Plugin
http://www.jhipster.tech/prettier-java/
Apache License 2.0
1.06k stars 103 forks source link

Global formatter on macOS - cannot find package 'prettier-plugin-java' #630

Open Maxeh opened 6 months ago

Maxeh commented 6 months ago

Hello, I followed the readme and tried to configure the prettier plugin globally. For this I installed prettier and prettier-plugin-javaglobally with npm -g and added the file prettierrc.yaml to the root directory with the following content as described in the readme:

plugins:
  - prettier-plugin-java

When I run prettier --write "**/*.java" in the root directory I see the following error: [error] Cannot find package 'prettier-plugin-java' imported from /Users/currentuser/projectroot/noop.js

It seems that it tries to load the plugin from the project directory and not from the global node_modules which is quite annoying. As others do not seem to have this kind of issue, this might only occur on macOS ?

When changing the plugin path in the prettierrc.yaml to an absolute path it works as expected:

plugins:
  - "/Users/currentuser/.nvm/versions/node/v18.16.1/lib/node_modules/prettier-plugin-java/dist/index.js"

However, this means that the file cannot be checked into Git and every developer has to configure the file since everyone has different paths; I cannot even replace /Users/currentuser with ~ , this also throws the same error.

Do you know how I can make it work on macOS so that I can only use the short version and do not need the absolute path? If there is no way to make it work, the documentation of this project should be adjusted and this issue must be described in the readme. It took me way too long to figure out how to get rid of the error.

jtkiesel commented 5 months ago

This appears to be an issue with the way Prettier itself loads plugins: prettier/prettier#15141 A workaround is to install the plugin in your home directory instead of globally. That seems to allow Prettier to find the plugin without issue. For example:

npm -g install prettier
cd ~
npm install prettier-plugin-java

Thanks for bringing this to our attention! We should update Prettier Java's README to instruct users to follow the above steps, since apparently installing both Prettier and the plugin globally no longer works (I'm guessing this is an issue with Prettier 3.)