diff --git a/CHANGES.md b/CHANGES.md index c35b69f97d..7e7c971e39 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * New static method to `DiffMessageFormatter` which allows to retrieve diffs with their line numbers ([#1960](https://github.com/diffplug/spotless/issues/1960)) ### Fixed +* Fix empty files with biome >= 1.5.0 when formatting files that are in the ignore list of the biome configuration file. ([#1989](https://github.com/diffplug/spotless/pull/1989) fixes [#1987](https://github.com/diffplug/spotless/issues/1987)) * Fix a regression in BufStep where the same arguments were being provided to every `buf` invocation. ([#1976](https://github.com/diffplug/spotless/issues/1976)) ### Changes * Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948)) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 287d835bca..6fb993f419 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -151,6 +151,26 @@ The gist of it is that you will have to: If you get something running, we'd love to host your plugin within this repo as a peer to `plugin-gradle` and `plugin-maven`. +## Run tests + +To run all tests, simply do + +> gradlew test + +Since that takes some time, you might only want to run the tests +concerning what you are working on: + +```shell +# Run only from test from the "lib" project +gradlew :testlib:test --tests com.diffplug.spotless.generic.IndentStepTest + +# Run only one test from the "plugin-maven" project +gradlew :plugin-maven:test --tests com.diffplug.spotless.maven.pom.SortPomMavenTest + +# Run only one test from the "plugin-gradle" project +gradlew :plugin-gradle:test --tests com.diffplug.gradle.spotless.FreshMarkExtensionTest +``` + ## Integration testing ### Gradle - locally diff --git a/lib/src/main/java/com/diffplug/spotless/rome/RomeStep.java b/lib/src/main/java/com/diffplug/spotless/rome/RomeStep.java index 5ba33d593d..de5b3e80ee 100644 --- a/lib/src/main/java/com/diffplug/spotless/rome/RomeStep.java +++ b/lib/src/main/java/com/diffplug/spotless/rome/RomeStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -427,9 +427,22 @@ private String format(ProcessRunner runner, String input, File file) throws IOEx var stdin = input.getBytes(StandardCharsets.UTF_8); var args = buildBiomeCommand(file); if (logger.isDebugEnabled()) { - logger.debug("Running Biome comand to format code: '{}'", String.join(", ", args)); + logger.debug("Running Biome command to format code: '{}'", String.join(", ", args)); + } + var runnerResult = runner.exec(stdin, args); + var stdErr = runnerResult.stdErrUtf8(); + if (!stdErr.isEmpty()) { + logger.warn("Biome stderr ouptut for file '{}'\n{}", file, stdErr.trim()); + } + var formatted = runnerResult.assertExitZero(StandardCharsets.UTF_8); + // When biome encounters an ignored file, it does not output any formatted code + // Ignored files come from (a) the biome.json configuration file and (b) from + // a list of hard-coded file names, such as package.json or tsconfig.json. + if (formatted.isEmpty()) { + return input; + } else { + return formatted; } - return runner.exec(stdin, args).assertExitZero(StandardCharsets.UTF_8); } /** diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 267927fe0f..b6bd7c419e 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -4,6 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed +* Fix empty files with biome >= 1.5.0 when formatting files that are in the ignore list of the biome configuration file. ([#1989](https://github.com/diffplug/spotless/pull/1989) fixes [#1987](https://github.com/diffplug/spotless/issues/1987))======= * Fix a regression in BufStep where the same arguments were being provided to every `buf` invocation. ([#1976](https://github.com/diffplug/spotless/issues/1976)) ### Changes * Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948)) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 7ad010567a..98416cdd05 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -1218,11 +1218,6 @@ spotless { } ``` -**Limitations:** - -- The auto-discovery of config files (up the file tree) will not work when using - Biome within spotless. - To apply Biome to more kinds of files with a different configuration, just add more formats: @@ -1243,6 +1238,24 @@ spotless { } ``` +**Limitations:** + +- The auto-discovery of config files (up the file tree) will not work when using + Biome within spotless. +- The `ignore` option of the `biome.json` configuration file will not be applied. + Include and exclude patterns are configured in the spotless configuration in the + Gradle settings file instead. + +Note: Due to a limitation of biome, if the name of a file matches a pattern in +the `ignore` option of the specified `biome.json` configuration file, it will not be +formatted, even if included in the biome configuration section of the Gradle settings +file. +You could specify a different `biome.json` configuration file without an `ignore` +pattern to circumvent this. + +Note 2: Biome is hard-coded to ignore certain special files, such as `package.json` +or `tsconfig.json`. These files will never be formatted. + ### Biome binary To format with Biome, spotless needs to find the Biome binary. By default, diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java index a2665a3bf1..db4ca4b027 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -340,4 +340,32 @@ void failureWhenNotParseable() throws Exception { assertThat(spotlessApply.getOutput()).contains("Format with errors is disabled."); assertThat(spotlessApply.getOutput()).contains("Step 'biome' found problem in 'biome_test.js'"); } + + /** + * Biome is hard-coded to ignore certain files, such as package.json. Since version 1.5.0, + * the biome CLI does not output any formatted code anymore, whereas previously it printed + * the input as-is. This tests checks that when the biome formatter outputs an empty string, + * the contents of the file to format are used instead. + * + * @throws Exception When a test failure occurs. + */ + @Test + void preservesIgnoredFiles() throws Exception { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " json {", + " target '**/*.json'", + " biome('1.5.0')", + " }", + "}"); + setFile("package.json").toResource("biome/json/packageBefore.json"); + + var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL"); + assertFile("package.json").sameAsResource("biome/json/packageAfter.json"); + } } diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 5da1cd384d..cf3ea63b2d 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Added * M2E support: Emit file specific errors during incremental build. ([#1960](https://github.com/diffplug/spotless/issues/1960)) +### Fixed +* Fix empty files with biome >= 1.5.0 when formatting files that are in the ignore list of the biome configuration file. ([#1989](https://github.com/diffplug/spotless/pull/1989) fixes [#1987](https://github.com/diffplug/spotless/issues/1987)) ### Changes * Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948)) * Bump default `ktlint` version to latest `1.0.1` -> `1.1.1`. ([#1973](https://github.com/diffplug/spotless/pull/1973)) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index f632d78ea7..593368c7ac 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -1300,10 +1300,6 @@ usually you will be creating a generic format. ``` -**Limitations:** -- The auto-discovery of config files (up the file tree) will not work when using - Biome within spotless. - To apply Biome to more kinds of files with a different configuration, just add more formats: @@ -1315,6 +1311,22 @@ more formats: ``` +**Limitations:** +- The auto-discovery of config files (up the file tree) will not work when using + Biome within spotless. +- The `ignore` option of the `biome.json` configuration file will not be applied. + Include and exclude patterns are configured in the spotless configuration in the + Maven pom instead. + +Note: Due to a limitation of biome, if the name of a file matches a pattern in +the `ignore` option of the specified `biome.json` configuration file, it will not be +formatted, even if included in the biome configuration section of the Maven pom. +You could specify a different `biome.json` configuration file without an `ignore` +pattern to circumvent this. + +Note 2: Biome is hard-coded to ignore certain special files, such as `package.json` +or `tsconfig.json`. These files will never be formatted. + ### Biome binary To format with Biome, spotless needs to find the Biome binary. By default, diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/biome/BiomeMavenTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/biome/BiomeMavenTest.java index e015b934f5..ff763c3cf8 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/biome/BiomeMavenTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/biome/BiomeMavenTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -199,4 +199,20 @@ void failureWhenNotParseable() throws Exception { assertThat(result.stdOutUtf8()).contains("Unable to format file"); assertThat(result.stdOutUtf8()).contains("Step 'biome' found problem in 'biome_test.js'"); } + + /** + * Biome is hard-coded to ignore certain files, such as package.json. Since version 1.5.0, + * the biome CLI does not output any formatted code anymore, whereas previously it printed + * the input as-is. This tests checks that when the biome formatter outputs an empty string, + * the contents of the file to format are used instead. + * + * @throws Exception When a test failure occurs. + */ + @Test + void preservesIgnoredFiles() throws Exception { + writePomWithJsonSteps("**/*.json", "1.5.0"); + setFile("package.json").toResource("biome/json/packageBefore.json"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile("package.json").sameAsResource("biome/json/packageAfter.json"); + } } diff --git a/testlib/src/main/resources/biome/json/package.json b/testlib/src/main/resources/biome/json/package.json new file mode 100644 index 0000000000..94e8f88de5 --- /dev/null +++ b/testlib/src/main/resources/biome/json/package.json @@ -0,0 +1,3 @@ +{ +"name": "test-package","version": "0.0.1" + } \ No newline at end of file diff --git a/testlib/src/main/resources/biome/json/packageAfter.json b/testlib/src/main/resources/biome/json/packageAfter.json new file mode 100644 index 0000000000..94e8f88de5 --- /dev/null +++ b/testlib/src/main/resources/biome/json/packageAfter.json @@ -0,0 +1,3 @@ +{ +"name": "test-package","version": "0.0.1" + } \ No newline at end of file diff --git a/testlib/src/main/resources/biome/json/packageBefore.json b/testlib/src/main/resources/biome/json/packageBefore.json new file mode 100644 index 0000000000..94e8f88de5 --- /dev/null +++ b/testlib/src/main/resources/biome/json/packageBefore.json @@ -0,0 +1,3 @@ +{ +"name": "test-package","version": "0.0.1" + } \ No newline at end of file diff --git a/testlib/src/test/java/com/diffplug/spotless/rome/RomeStepTest.java b/testlib/src/test/java/com/diffplug/spotless/rome/RomeStepTest.java index f723a561c2..8ab92e90e0 100644 --- a/testlib/src/test/java/com/diffplug/spotless/rome/RomeStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/rome/RomeStepTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -392,6 +392,19 @@ void testAutoDetectTsx() { var stepHarness = StepHarnessWithFile.forStep(RomeStepTest.this, step); stepHarness.testResource("biome/ts/fileBefore.tsx", "biome/ts/fileAfter.tsx"); } + + /** + * Biome is hard-coded to ignore certain files, such as package.json. Since version 1.5.0, + * the biome CLI does not output any formatted code anymore, whereas previously it printed + * the input as-is. This tests checks that when the biome formatter outputs an empty string, + * the contents of the file to format are used instead. + */ + @Test + void preservesIgnoredFiles() { + var step = RomeStep.withExeDownload(BiomeFlavor.BIOME, "1.5.0", downloadDir.toString()).create(); + var stepHarness = StepHarnessWithFile.forStep(RomeStepTest.this, step); + stepHarness.testResource("biome/json/package.json", "biome/json/packageAfter.json"); + } } @Nested