Skip to content

Commit 350cf2e

Browse files
authored
no empty files with biome when formatting ignored files (#1989 fixes #1987)
2 parents a3ef63b + 2b52f75 commit 350cf2e

File tree

13 files changed

+143
-15
lines changed

13 files changed

+143
-15
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Added
1414
* New static method to `DiffMessageFormatter` which allows to retrieve diffs with their line numbers ([#1960](https://github.com/diffplug/spotless/issues/1960))
1515
### Fixed
16+
* 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))
1617
* Fix a regression in BufStep where the same arguments were being provided to every `buf` invocation. ([#1976](https://github.com/diffplug/spotless/issues/1976))
1718
### Changes
1819
* Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948))

CONTRIBUTING.md

+20
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ The gist of it is that you will have to:
151151

152152
If you get something running, we'd love to host your plugin within this repo as a peer to `plugin-gradle` and `plugin-maven`.
153153

154+
## Run tests
155+
156+
To run all tests, simply do
157+
158+
> gradlew test
159+
160+
Since that takes some time, you might only want to run the tests
161+
concerning what you are working on:
162+
163+
```shell
164+
# Run only from test from the "lib" project
165+
gradlew :testlib:test --tests com.diffplug.spotless.generic.IndentStepTest
166+
167+
# Run only one test from the "plugin-maven" project
168+
gradlew :plugin-maven:test --tests com.diffplug.spotless.maven.pom.SortPomMavenTest
169+
170+
# Run only one test from the "plugin-gradle" project
171+
gradlew :plugin-gradle:test --tests com.diffplug.gradle.spotless.FreshMarkExtensionTest
172+
```
173+
154174
## Integration testing
155175

156176
### Gradle - locally

lib/src/main/java/com/diffplug/spotless/rome/RomeStep.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* 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
427427
var stdin = input.getBytes(StandardCharsets.UTF_8);
428428
var args = buildBiomeCommand(file);
429429
if (logger.isDebugEnabled()) {
430-
logger.debug("Running Biome comand to format code: '{}'", String.join(", ", args));
430+
logger.debug("Running Biome command to format code: '{}'", String.join(", ", args));
431+
}
432+
var runnerResult = runner.exec(stdin, args);
433+
var stdErr = runnerResult.stdErrUtf8();
434+
if (!stdErr.isEmpty()) {
435+
logger.warn("Biome stderr ouptut for file '{}'\n{}", file, stdErr.trim());
436+
}
437+
var formatted = runnerResult.assertExitZero(StandardCharsets.UTF_8);
438+
// When biome encounters an ignored file, it does not output any formatted code
439+
// Ignored files come from (a) the biome.json configuration file and (b) from
440+
// a list of hard-coded file names, such as package.json or tsconfig.json.
441+
if (formatted.isEmpty()) {
442+
return input;
443+
} else {
444+
return formatted;
431445
}
432-
return runner.exec(stdin, args).assertExitZero(StandardCharsets.UTF_8);
433446
}
434447

435448
/**

plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66
### Fixed
7+
* 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))=======
78
* Fix a regression in BufStep where the same arguments were being provided to every `buf` invocation. ([#1976](https://github.com/diffplug/spotless/issues/1976))
89
### Changes
910
* Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948))

plugin-gradle/README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -1218,11 +1218,6 @@ spotless {
12181218
}
12191219
```
12201220
1221-
**Limitations:**
1222-
1223-
- The auto-discovery of config files (up the file tree) will not work when using
1224-
Biome within spotless.
1225-
12261221
To apply Biome to more kinds of files with a different configuration, just add
12271222
more formats:
12281223
@@ -1243,6 +1238,24 @@ spotless {
12431238
}
12441239
```
12451240
1241+
**Limitations:**
1242+
1243+
- The auto-discovery of config files (up the file tree) will not work when using
1244+
Biome within spotless.
1245+
- The `ignore` option of the `biome.json` configuration file will not be applied.
1246+
Include and exclude patterns are configured in the spotless configuration in the
1247+
Gradle settings file instead.
1248+
1249+
Note: Due to a limitation of biome, if the name of a file matches a pattern in
1250+
the `ignore` option of the specified `biome.json` configuration file, it will not be
1251+
formatted, even if included in the biome configuration section of the Gradle settings
1252+
file.
1253+
You could specify a different `biome.json` configuration file without an `ignore`
1254+
pattern to circumvent this.
1255+
1256+
Note 2: Biome is hard-coded to ignore certain special files, such as `package.json`
1257+
or `tsconfig.json`. These files will never be formatted.
1258+
12461259
### Biome binary
12471260
12481261
To format with Biome, spotless needs to find the Biome binary. By default,

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BiomeIntegrationTest.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -340,4 +340,32 @@ void failureWhenNotParseable() throws Exception {
340340
assertThat(spotlessApply.getOutput()).contains("Format with errors is disabled.");
341341
assertThat(spotlessApply.getOutput()).contains("Step 'biome' found problem in 'biome_test.js'");
342342
}
343+
344+
/**
345+
* Biome is hard-coded to ignore certain files, such as package.json. Since version 1.5.0,
346+
* the biome CLI does not output any formatted code anymore, whereas previously it printed
347+
* the input as-is. This tests checks that when the biome formatter outputs an empty string,
348+
* the contents of the file to format are used instead.
349+
*
350+
* @throws Exception When a test failure occurs.
351+
*/
352+
@Test
353+
void preservesIgnoredFiles() throws Exception {
354+
setFile("build.gradle").toLines(
355+
"plugins {",
356+
" id 'com.diffplug.spotless'",
357+
"}",
358+
"repositories { mavenCentral() }",
359+
"spotless {",
360+
" json {",
361+
" target '**/*.json'",
362+
" biome('1.5.0')",
363+
" }",
364+
"}");
365+
setFile("package.json").toResource("biome/json/packageBefore.json");
366+
367+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
368+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
369+
assertFile("package.json").sameAsResource("biome/json/packageAfter.json");
370+
}
343371
}

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* M2E support: Emit file specific errors during incremental build. ([#1960](https://github.com/diffplug/spotless/issues/1960))
8+
### Fixed
9+
* 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))
810
### Changes
911
* Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948))
1012
* Bump default `ktlint` version to latest `1.0.1` -> `1.1.1`. ([#1973](https://github.com/diffplug/spotless/pull/1973))

plugin-maven/README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -1300,10 +1300,6 @@ usually you will be creating a generic format.
13001300
</configuration>
13011301
```
13021302

1303-
**Limitations:**
1304-
- The auto-discovery of config files (up the file tree) will not work when using
1305-
Biome within spotless.
1306-
13071303
To apply Biome to more kinds of files with a different configuration, just add
13081304
more formats:
13091305

@@ -1315,6 +1311,22 @@ more formats:
13151311
</configuration>
13161312
```
13171313

1314+
**Limitations:**
1315+
- The auto-discovery of config files (up the file tree) will not work when using
1316+
Biome within spotless.
1317+
- The `ignore` option of the `biome.json` configuration file will not be applied.
1318+
Include and exclude patterns are configured in the spotless configuration in the
1319+
Maven pom instead.
1320+
1321+
Note: Due to a limitation of biome, if the name of a file matches a pattern in
1322+
the `ignore` option of the specified `biome.json` configuration file, it will not be
1323+
formatted, even if included in the biome configuration section of the Maven pom.
1324+
You could specify a different `biome.json` configuration file without an `ignore`
1325+
pattern to circumvent this.
1326+
1327+
Note 2: Biome is hard-coded to ignore certain special files, such as `package.json`
1328+
or `tsconfig.json`. These files will never be formatted.
1329+
13181330
### Biome binary
13191331

13201332
To format with Biome, spotless needs to find the Biome binary. By default,

plugin-maven/src/test/java/com/diffplug/spotless/maven/biome/BiomeMavenTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -199,4 +199,20 @@ void failureWhenNotParseable() throws Exception {
199199
assertThat(result.stdOutUtf8()).contains("Unable to format file");
200200
assertThat(result.stdOutUtf8()).contains("Step 'biome' found problem in 'biome_test.js'");
201201
}
202+
203+
/**
204+
* Biome is hard-coded to ignore certain files, such as package.json. Since version 1.5.0,
205+
* the biome CLI does not output any formatted code anymore, whereas previously it printed
206+
* the input as-is. This tests checks that when the biome formatter outputs an empty string,
207+
* the contents of the file to format are used instead.
208+
*
209+
* @throws Exception When a test failure occurs.
210+
*/
211+
@Test
212+
void preservesIgnoredFiles() throws Exception {
213+
writePomWithJsonSteps("**/*.json", "<biome><version>1.5.0</version></biome>");
214+
setFile("package.json").toResource("biome/json/packageBefore.json");
215+
mavenRunner().withArguments("spotless:apply").runNoError();
216+
assertFile("package.json").sameAsResource("biome/json/packageAfter.json");
217+
}
202218
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "test-package","version": "0.0.1"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "test-package","version": "0.0.1"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "test-package","version": "0.0.1"
3+
}

testlib/src/test/java/com/diffplug/spotless/rome/RomeStepTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -392,6 +392,19 @@ void testAutoDetectTsx() {
392392
var stepHarness = StepHarnessWithFile.forStep(RomeStepTest.this, step);
393393
stepHarness.testResource("biome/ts/fileBefore.tsx", "biome/ts/fileAfter.tsx");
394394
}
395+
396+
/**
397+
* Biome is hard-coded to ignore certain files, such as package.json. Since version 1.5.0,
398+
* the biome CLI does not output any formatted code anymore, whereas previously it printed
399+
* the input as-is. This tests checks that when the biome formatter outputs an empty string,
400+
* the contents of the file to format are used instead.
401+
*/
402+
@Test
403+
void preservesIgnoredFiles() {
404+
var step = RomeStep.withExeDownload(BiomeFlavor.BIOME, "1.5.0", downloadDir.toString()).create();
405+
var stepHarness = StepHarnessWithFile.forStep(RomeStepTest.this, step);
406+
stepHarness.testResource("biome/json/package.json", "biome/json/packageAfter.json");
407+
}
395408
}
396409

397410
@Nested

0 commit comments

Comments
 (0)