Skip to content

Commit 03e89b8

Browse files
authored
feat(scalafmt): enforce version consistency between library and config file (#2499)
2 parents e803400 + 05fe1fe commit 03e89b8

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
2020
### Changed
2121
* Bump internal dependencies for npm-based formatters ([#2542](https://github.com/diffplug/spotless/pull/2542))
2222

23+
### Changed
24+
* scalafmt: enforce version consistency between the version configured in Spotless and the version declared in Scalafmt config file ([#2460](https://github.com/diffplug/spotless/issues/2460))
25+
2326
## [3.1.2] - 2025-05-27
2427
### Fixed
2528
* Fix `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487))

lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 DiffPlug
2+
* Copyright 2022-2025 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.
@@ -21,6 +21,7 @@
2121
import java.nio.file.Files;
2222

2323
import org.scalafmt.Scalafmt;
24+
import org.scalafmt.Versions;
2425
import org.scalafmt.config.ScalafmtConfig;
2526
import org.scalafmt.config.ScalafmtConfig$;
2627

@@ -45,6 +46,16 @@ public ScalafmtFormatterFunc(FileSignature configSignature) throws Exception {
4546
String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
4647
config = Scalafmt.parseHoconConfig(configStr).get();
4748
}
49+
50+
// This check is to raise awareness to the user that the version of the config file is currently not used.
51+
// Context: https://github.com/diffplug/spotless/issues/2460
52+
// This check should be removed when Spotless dynamically loads the proper version of the Scalafmt library.
53+
String scalafmtLibraryVersion = Versions.version();
54+
if (!config.version().equals(scalafmtLibraryVersion)) {
55+
throw new IllegalArgumentException(
56+
"Spotless is using " + scalafmtLibraryVersion + " but the config file declares " + config.version() +
57+
". Both must match. Update the version declared in the plugin's settings and/or the config file.");
58+
}
4859
}
4960

5061
@Override

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Changed
1414
* Bump internal dependencies for npm-based formatters ([#2542](https://github.com/diffplug/spotless/pull/2542))\
1515

16+
### Changed
17+
* scalafmt: enforce version consistency between the version configured in Spotless and the version declared in Scalafmt config file ([#2460](https://github.com/diffplug/spotless/issues/2460))
18+
1619
## [7.0.4] - 2025-05-27
1720
### Fixed
1821
* Fix `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487))

plugin-maven/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Changed
1414
* Bump internal dependencies for npm-based formatters ([#2542](https://github.com/diffplug/spotless/pull/2542))
1515

16+
### Changed
17+
* scalafmt: enforce version consistency between the version configured in Spotless and the version declared in Scalafmt config file ([#2460](https://github.com/diffplug/spotless/issues/2460))
18+
1619
## [2.44.5] - 2025-05-27
1720
### Changed
1821
* Bump default `eclipse` version to latest `4.34` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458))

testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 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.
@@ -48,17 +48,18 @@ void behaviorFileOverride() {
4848
}
4949

5050
@Test
51-
void behaviorDefaultConfigVersion_3_0_0() {
51+
void behaviorNoConfigFile() {
5252
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null);
5353
StepHarness.forStep(step)
5454
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.clean");
5555
}
5656

5757
@Test
58-
void behaviorCustomConfigVersion_3_0_0() {
58+
void behaviorConfigFileVersionDoesnotMatchLibrary() {
5959
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf"));
60-
StepHarness.forStep(step)
61-
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.cleanWithCustomConf");
60+
Assertions.assertThatThrownBy(() -> {
61+
step.format("", new File(""));
62+
}).cause().message().contains("Spotless is using 3.0.0 but the config file declares 3.8.1. Both must match. Update the version declared in the plugin's settings and/or the config file.");
6263
}
6364

6465
@Test

0 commit comments

Comments
 (0)