jhipster / prettier-java

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

No parser could be inferred for file when using Prettier Java with pnpm #508

Closed NatoBoram closed 2 years ago

NatoBoram commented 3 years ago

The installation instructions don't seem to work on my end.

prettier-plugin-java@1.4.0

# Options (if any):

Input:

# code snippet
pnpm install -g prettier prettier-plugin-java
prettier --write "**/*.java"

Output:

# code snippet
[error] No parser could be inferred for file: src/main/java/com/natoboram/switcheroo/Main.java

Expected behavior:

src/main/java/com/natoboram/switcheroo/Main.java 241ms
clementdessoude commented 3 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 :)

clementdessoude commented 2 years ago

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.

sarnobat commented 1 year ago

I've given up and gone back to google's code formatter

jmayday commented 1 year ago

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?

clementdessoude commented 1 year ago

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 ?

jmayday commented 1 year ago

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.

TyIsI commented 1 year ago

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

Then went over to the svelte issue and tried the options

Final solutions are:

Hope this helps!

TyIsI commented 1 year ago

I can confirm that I have this now running successfully in VSC with the standard Prettier VSC extension!

murdos commented 1 year ago

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.

Ch-301 commented 1 year ago

Hey @TyIsI Where I can add "the plugin as a plugin definition:"?

TyIsI commented 1 year ago

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
jeremy-brooks commented 1 year ago

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:

loweffort-alt commented 1 year ago

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 😿

codejedi365 commented 11 months ago

@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.

KaziNizamul commented 11 months ago

I faced the same issue in CI pipeline of jhonline repo. Steps i followed to resolve it is:

jameshearttech commented 5 months ago

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;
borgogelli commented 3 weeks ago

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"
      }
    }     
  ]
}