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

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

Closed
NatoBoram opened this issue Oct 1, 2021 · 17 comments · Fixed by #621
Closed

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

NatoBoram opened this issue Oct 1, 2021 · 17 comments · Fixed by #621

Comments

@NatoBoram
Copy link

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
Copy link
Contributor

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: sveltejs/prettier-plugin-svelte#155 (comment)

Can you try and see if it solves your issue ? If so, let's improve our documentation :)

@clementdessoude
Copy link
Contributor

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
Copy link

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

@jmayday
Copy link

jmayday commented Jul 6, 2023

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 clementdessoude changed the title No parser could be inferred for file No parser could be inferred for file when using Prettier Java with pnpm Jul 8, 2023
@clementdessoude
Copy link
Contributor

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
Copy link

jmayday commented Jul 8, 2023

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
Copy link

TyIsI commented Jul 10, 2023

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
  • Default execution:
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!

@TyIsI
Copy link

TyIsI commented Jul 10, 2023

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

@murdos
Copy link

murdos commented Jul 10, 2023

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
Copy link

Ch-301 commented Jul 16, 2023

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

@TyIsI
Copy link

TyIsI commented Jul 17, 2023

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
Copy link

jeremy-brooks commented Aug 17, 2023

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:

  • prettier version 3.0.1
  • prettier-plugin-java version 2.2.0
  • Java version 11 (for project reasons)
  • linux ubuntu version 20.04

@loweffort-alt
Copy link

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
Copy link

@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
Copy link

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

@jameshearttech
Copy link

jameshearttech commented May 1, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.