Closed NatoBoram closed 2 years ago
Hi @NatoBoram !
This seems to be an issue with how Prettier resolves plugin that is not compatible with pnpm (see this issue and this one).
A workaround seems to be explained in this issue, especially in this comment: https://github.com/sveltejs/prettier-plugin-svelte/issues/155#issuecomment-791935741
Can you try and see if it solves your issue ? If so, let's improve our documentation :)
I'm closing this as we don't have any update on this. If you have anything new to share with us, feel free to comment and we can reopen the issue.
I've given up and gone back to google's code formatter
I'm having same issue
prettier -v
3.0.0
prettier --write "**/*.java"
[error] No parser could be inferred for file "<path_to_my_java_file>....java".
@clementdessoude could we reopen this?
Hello @jmayday,
I'm not opposed to reopening this issue, but as explained in this previous comment, I am waiting for some information.
This seems to be an issue with how Prettier resolves plugin that is not compatible with pnpm (see this issue and this one).
A workaround seems to be explained in this issue, especially in this comment: sveltejs/prettier-plugin-svelte#155 (comment)
Can you try and see if it solves your issue ? If so, let's improve our documentation :)
Furthermore, could you provide a bit more information on how you installed prettier and prettier-plugin-java ? Do you also use pnpm ?
Hey @clementdessoude, I didn't really know how to apply hints from the other issue. I just downgraded Prettier to version 2.8.8 and it works again.
Hi!
Just ran into this issue with yarn
.
Installed prettier and prettier-plugin-java locally:
yarn add -D prettier prettier-plugin-java
.prettierrc.yml
:
overrides:
- files:
- '*.java'
options:
printWidth: 140
tabWidth: 4
useTabs: false
trailingComma: none
yarn run v1.22.19
$ /home/user/workspace/project/node_modules/.bin/prettier -c src/main/java/com/example/component/aspect/Class.java
Checking formatting...
[error] No parser could be inferred for file "/home/user/workspace/project/src/main/java/com/example/component/aspect/Class.java".
All matched files use Prettier code style!
Done in 0.28s.
Then went over to the svelte issue and tried the options
The --plugin-search-dir
is not currently a valid CLI argument for prettier
.
The inline requiring of the plugin would not work as I am using a YAML config file.
Adding the parser to the overrides options also did not work
...
overrides:
- files:
- '*.java'
options:
parser: 'java'
...
Final solutions are:
Directly import the plugin through CLI argument:
$ yarn prettier --plugin ./node_modules/prettier-plugin-java/dist/index.js -c src/main/java/com/example/component/aspect/Class.java
Checking formatting...
[warn] src/main/java/com/example/component/aspect/Class.java
[warn] Code style issues found in the above file. Run Prettier to fix.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Preferred solution; adding the plugin as a plugin definition:
...
plugins:
- prettier-plugin-java
Hope this helps!
I can confirm that I have this now running successfully in VSC with the standard Prettier VSC extension!
FYI prettier 3.0 indeed changed its behavior regarding plugins, and don't search and load them automatically anymore: Plugin search feature has been removed So the best way to use external plugins is effectively to declare them in configuration file.
Hey @TyIsI Where I can add "the plugin as a plugin definition:"?
Where I can add "the plugin as a plugin definition:"?
It's a top-level configuration item.
In my YAML file, I have it as so:
plugins:
- prettier-plugin-java
Hey @TyIsI Where I can add "the plugin as a plugin definition:"?
In my project root, I have created a file for prettier config called:
.prettierrc.yaml
In there I have added the plugins:
plugins:
- prettier-plugin-java
Taking special note of the formatting for yaml with indentation of plugins list.
This all works fine now, I am using:
Hello everyone 👋! I use null-ls to format my files (this repository was archived). Everything was fine until the .astro files don't work (same problem), even though .prettierrc.config.js exists:
plugins: ["prettier-plugin-astro"],
overrides: [
{
files: "*.astro",
options: {
parser: "astro",
},
},
],
I solved it by opening VSCode and installing the Astro plugin, I would like to solve it in NeoVim 😿
@jeremy-brooks, thank you for your answer! It led me to solving my problem.
I want to add my issue resolution here in case anyone else finds themselves on this thread because of the same random error message. I just updated my version of prettier
from 2.8.8
to 3.0.3
and found this projects plugin stopped working with [error] No parser could be inferred for file: src/main/java/com/organization/project/Main.java
.
Ends up the change from 2
to 3
moved from automatic detection of plugins to explicit plugin declarations instead. I figured this out after @jeremy-brooks's comment and just updated my prettier configuration to include plugins: ["prettier-plugin-java"]
and then everything started working again.
I faced the same issue in CI pipeline of jhonline repo. Steps i followed to resolve it is:
installing appropriate version of prettier and prettier-java-plugin as per package.json
i.e yarn add prettier-plugin-java@0.8.3
and yarn add prettier@1.19.1
and this command then :
yarn prettier --plugin ./node_modules/prettier-plugin-java/src/index.js --write <filename>
eg: yarn prettier --plugin ./node_modules/prettier-plugin-java/src/index.js --write src/main/java/io/github/jhipster/online/service/GitService.java
We have a shared Prettier configuration we use as a base configuration. We need to add support for Java in some cases. Here is an example of our base configuration with added support for Java using this plugin in ESM. Confirmed this works with the esbenp.prettier-vscode plugin.
import prettierBaseConfig from '@example/prettier';
const prettierConfig = {
...prettierBaseConfig,
plugins: ['prettier-plugin-java']
};
export default prettierConfig;
The only solution that works for me, on Windows, is to include the full path to the plugin :
{
"$schema": "http://json.schemastore.org/prettierrc",
"plugins": ["C:/Users/Borgo/AppData/Roaming/npm/node_modules/prettier-plugin-java/dist/index.js"],
"overrides": [
{
"files": "*.java",
"options": {
"printWidth": 140,
"useTabs": true,
"trailingComma": "all"
}
}
]
}
The installation instructions don't seem to work on my end.
prettier-plugin-java@1.4.0
Input:
Output:
Expected behavior: