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

Facing "Could not resolve parser" when using multiple prettier formats with different plugins #1162

Closed
6 tasks done
julb opened this issue Apr 5, 2022 · 3 comments · Fixed by #1565
Closed
6 tasks done
Assignees
Labels

Comments

@julb
Copy link

julb commented Apr 5, 2022

If you are submitting a bug, please include the following:

  • summary of problem
  • gradle or maven version
  • spotless version
  • operating system and version
  • copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
  • copy-paste the full content of any console errors emitted by gradlew spotless[Apply/Check] --stacktrace

If you're just submitting a feature request or question, no need for the above.

Summary of the problem

I'm configuring a pom where I want to have different rules for prettier according to file extension.
I followed the documentation and configure specific format section, with for each a specific prettier plugin.

I suspect that NodeJS installs are competing each other, and may lead to have a missing plugin on a specific execution.
I'm able to fix the issue if I install all the prettier plugins I use in each format section.

Maven version

3.8.4

Spotless version

2.22.0 with Node v16.14.2

Operating system

Windows

Spotless configuration

<formats>
						<format>
							<includes>
								<include>**/*.json</include>
							</includes>
							<excludes>
								<exclude>target/**</exclude>
							</excludes>
							<prettier>
								<prettierVersion>2.6.2</prettierVersion>
								<config>
									<parser>json</parser>
									<tabWidth>4</tabWidth>
								</config>
							</prettier>
						</format>
						<format>
							<includes>
								<include>**/*.yaml</include>
								<include>**/*.yml</include>
							</includes>
							<excludes>
								<exclude>target/**</exclude>
							</excludes>
							<prettier>
								<prettierVersion>2.6.2</prettierVersion>
								<config>
									<parser>yaml</parser>
									<tabWidth>2</tabWidth>
								</config>
							</prettier>
						</format>
						<format>
							<includes>
								<include>**/*.xml</include>
							</includes>
							<excludes>
								<exclude>pom.xml</exclude>
								<exclude>target/**</exclude>
							</excludes>
							<prettier>
								<devDependencyProperties>
									<property>
										<name>prettier</name>
										<value>2.6.2</value>
									</property>
									<property>
										<name>@prettier/plugin-xml</name>
										<value>2.0.1</value>
									</property>
								</devDependencyProperties>
								<config>
									<parser>xml</parser>
									<tabWidth>4</tabWidth>
								</config>
							</prettier>
						</format>

						<format>
							<includes>
								<include>**/*.properties</include>
							</includes>
							<prettier>
								<devDependencies>
									<prettier>2.6.2</prettier>
									<prettier-plugin-properties>0.1.0</prettier-plugin-properties>
								</devDependencies>
								<config>
									<parser>dot-properties</parser>
									<keySeparator>=</keySeparator>
									<printWidth>120</printWidth>
								</config>
							</prettier>
						</format>
					</formats>

Stacktrace / Outputs

$ mvn clean spotless:apply
[...]
[INFO] --- spotless-maven-plugin:2.22.0:apply (default-cli) @ parent ---
added 52 packages in 3s

1 package is looking for funding
  run `npm fund` for details

added 4 packages in 4s

1 package is looking for funding
  run `npm fund` for details

up to date in 558ms

1 package is looking for funding
  run `npm fund` for details

added 3 packages, and removed 4 packages in 2s

1 package is looking for funding
  run `npm fund` for details

> [email protected] start
> node serve.js

Server running on port 53325

> [email protected] start
> node serve.js

Server running on port 53327

> [email protected] start
> node serve.js

Server running on port 53329
[ERROR] Step 'prettier-format' found problem in 'src\site\resources\eclipse\codetemplates.xml':
Unexpected response status code at /prettier/format [HTTP 501] -- (Error while formatting: Error: Couldn't resolve parser "xml")
com.diffplug.spotless.npm.SimpleRestClient$SimpleRestResponseException: Unexpected response status code at /prettier/format [HTTP 501] -- (Error while formatting: Error: Couldn't resolve parser "xml")
    at com.diffplug.spotless.npm.SimpleRestClient.postJson (SimpleRestClient.java:72)
    at com.diffplug.spotless.npm.SimpleRestClient.postJson (SimpleRestClient.java:46)
    at com.diffplug.spotless.npm.PrettierRestService.format (PrettierRestService.java:49)
    at com.diffplug.spotless.npm.PrettierFormatterStep$PrettierFilePathPassingFormatterFunc.applyWithFile (PrettierFormatterStep.java:120)
    at com.diffplug.spotless.FormatterFunc$NeedsFile.apply (FormatterFunc.java:154)
    at com.diffplug.spotless.FormatterFunc$Closeable$1.apply (FormatterFunc.java:64)
    at com.diffplug.spotless.FormatterStepImpl$Standard.format (FormatterStepImpl.java:78)
    at com.diffplug.spotless.FormatterStep$Strict.format (FormatterStep.java:88)
    at com.diffplug.spotless.Formatter.compute (Formatter.java:230)
    at com.diffplug.spotless.PaddedCell.calculateDirtyState (PaddedCell.java:203)
    at com.diffplug.spotless.PaddedCell.calculateDirtyState (PaddedCell.java:190)
    at com.diffplug.spotless.maven.SpotlessApplyMojo.process (SpotlessApplyMojo.java:45)
    at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute (AbstractSpotlessMojo.java:195)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:47)
    at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:156)
    at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:72)
graceful shutdown finished.
graceful shutdown finished.
graceful shutdown finished.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  28.724 s
[INFO] Finished at: 2022-04-05T11:05:19+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.22.0:apply (default-cli) on project parent: Execution default-cli of goal com.diffplug.spotless:spotless-maven-plugin:2.22.0:apply failed: Unexpected response status code at /prettier/format [HTTP 501] -- (Error while formatting: Error: Couldn't resolve parser "xml") -> [Help 1]

Outputs of target/spotless-node-modules-prettier-format/package.json:

{
	"name": "spotless-prettier-formatter-step",
	"version": "2.0.0",
	"description": "Spotless formatter step for running prettier as a rest service.",
	"repository": "https://github.com/diffplug/spotless",
	"license": "Apache-2.0",
	"scripts": {
		"start": "node serve.js"
	},
	"devDependencies": {
		"prettier": "2.6.2",
		"prettier-plugin-properties": "0.1.0",
		"express": "4.17.1",
		"@moebius/http-graceful-shutdown": "1.1.0"
	},
	"dependencies": {},
	"engines": {
		"node": ">=6"
	}
}
@Magotchi
Copy link

Magotchi commented Aug 4, 2022

I also have this issue with the latest version of the Spotless Gradle plugin, using its Prettier formatter step in 3 different sections (one Java, one XML, on SQL), specifying on each only the latest version of Prettier and the latest versions of those respective plugins. It's intermittant, and therefore seems to me like some kind of race condition. I can make it more likely to occur with "gradle spotlessApply --rerun-tasks" (to avoid up-to-date checking). It is fixed for me the same as @julb fixed his (putting all Prettier plugin dependencies on all references to the Prettier formatter step).

Also note that it's not always that same error message. For example, if I'm having it format .sql files with the "sql" Spotless extension, and I do not specify the parser as "sql" in Prettier's config, when it complains, it will complain that it can't automatically discern the correct parser to use for that file extension (don't remember the exact wording).

Example sometimes-broken Spotless Gradle configuration (using Kotlin DSL):

import com.diffplug.spotless.LineEnding

plugins {
  id("application")
  id("com.diffplug.spotless") version "latest.release"
}

...

spotless {
  encoding("UTF-8")
  lineEndings = LineEnding.UNIX
  java {
    // This is included only for its ability to break up long strings and reformat JavaDoc comments.
    googleJavaFormat().reflowLongStrings()

    prettier(
      mapOf(
        "prettier" to "latest",
        "prettier-plugin-java" to "latest"
      )
    )
      .config(
        mapOf(
          "parser" to "java"
        )
      )
    importOrder("")
    removeUnusedImports()
    trimTrailingWhitespace()
    endWithNewline()
  }
  kotlinGradle {
    ktlint("0.46.1")
      .editorConfigOverride(mapOf("indent_size" to 2))
    trimTrailingWhitespace()
    endWithNewline()
  }
  format("xml") {
    target(
      "src/**/*.xml"
    )
    prettier(
      mapOf(
        "prettier" to "latest",
        "@prettier/plugin-xml" to "latest"
      )
    )
      .config(
        mapOf(
          "xmlSelfClosingSpace" to false,
          "xmlWhitespaceSensitivity" to "ignore"
        )
      )
    trimTrailingWhitespace()
    endWithNewline()
  }
  sql {
    target(
      "src/**/*.sql"
    )
    prettier(
      mapOf(
        "prettier" to "latest",
        "prettier-plugin-sql" to "latest"
      )
    )
      .config(
        mapOf(
          "language" to "tsql",
          "keywordCase" to "upper"
        )
      )
    trimTrailingWhitespace()
    endWithNewline()
  }
}

@sharedprophet
Copy link

Since it only happens when the different formats use different plugins, it seems the workaround is to pass the same map of prettier + plugin versions for every format, like so (based on @Magotchi 's config):

import com.diffplug.spotless.LineEnding

plugins {
  id("application")
  id("com.diffplug.spotless") version "latest.release"
}

...

spotless {
  encoding("UTF-8")
  lineEndings = LineEnding.UNIX
  val prettierPlugins = mapOf(
    "prettier" to "latest",
    "prettier-plugin-java" to "latest",
    "prettier-plugin-sql" to "latest",
    "@prettier/plugin-xml" to "latest",
  )
  java {
    // This is included only for its ability to break up long strings and reformat JavaDoc comments.
    googleJavaFormat().reflowLongStrings()

    prettier(prettierPlugins)
    importOrder("")
    removeUnusedImports()
    trimTrailingWhitespace()
    endWithNewline()
  }
  kotlinGradle {
    ktlint("0.46.1")
      .editorConfigOverride(mapOf("indent_size" to 2))
    trimTrailingWhitespace()
    endWithNewline()
  }
  format("xml") {
    target(
      "src/**/*.xml"
    )
    prettier(prettierPlugins)
      .config(
        mapOf(
          "xmlSelfClosingSpace" to false,
          "xmlWhitespaceSensitivity" to "ignore"
        )
      )
    trimTrailingWhitespace()
    endWithNewline()
  }
  sql {
    target(
      "src/**/*.sql"
    )
    prettier(prettierPlugins)
      .config(
        mapOf(
          "language" to "tsql",
          "keywordCase" to "upper"
        )
      )
    trimTrailingWhitespace()
    endWithNewline()
  }
}

This seems to be working for me.

@nedtwigg
Copy link
Member

Fixed in plugin-gradle 6.15.0 and plugin-maven 2.33.0 thanks to @simschla.

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

Successfully merging a pull request may close this issue.

5 participants