diff --git a/CHANGES.md b/CHANGES.md index 920bef6631..e173afac8c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] +### Changed +* 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)) + ## [3.1.2] - 2025-05-27 ### Fixed * Fix `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487)) diff --git a/lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java b/lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java index 6217ab6186..ab7e56ba45 100644 --- a/lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java +++ b/lib/src/scalafmt/java/com/diffplug/spotless/glue/scalafmt/ScalafmtFormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 DiffPlug + * Copyright 2022-2025 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import java.nio.file.Files; import org.scalafmt.Scalafmt; +import org.scalafmt.Versions; import org.scalafmt.config.ScalafmtConfig; import org.scalafmt.config.ScalafmtConfig$; @@ -45,6 +46,16 @@ public ScalafmtFormatterFunc(FileSignature configSignature) throws Exception { String configStr = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); config = Scalafmt.parseHoconConfig(configStr).get(); } + + // This check is to raise awareness to the user that the version of the config file is currently not used. + // Context: https://github.com/diffplug/spotless/issues/2460 + // This check should be removed when Spotless dynamically loads the proper version of the Scalafmt library. + String scalafmtLibraryVersion = Versions.version(); + if (!config.version().equals(scalafmtLibraryVersion)) { + throw new IllegalArgumentException( + "Spotless is using " + scalafmtLibraryVersion + " but the config file declares " + config.version() + + ". Both must match. Update the version declared in the plugin's settings and/or the config file."); + } } @Override diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index e35da34bfc..9388934661 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] +### Changed +* 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)) + ## [7.0.4] - 2025-05-27 ### Fixed * Fix `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index d09ac6144f..35752fe6db 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] +### Changed +* 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)) + ## [2.44.5] - 2025-05-27 ### Changed * Bump default `eclipse` version to latest `4.34` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458)) diff --git a/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java b/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java index 48b277566e..1c534f5aef 100644 --- a/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/scala/ScalaFmtStepTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2024 DiffPlug + * Copyright 2016-2025 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,17 +48,18 @@ void behaviorFileOverride() { } @Test - void behaviorDefaultConfigVersion_3_0_0() { + void behaviorNoConfigFile() { FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null); StepHarness.forStep(step) .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.clean"); } @Test - void behaviorCustomConfigVersion_3_0_0() { + void behaviorConfigFileVersionDoesnotMatchLibrary() { FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf")); - StepHarness.forStep(step) - .testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.cleanWithCustomConf"); + Assertions.assertThatThrownBy(() -> { + step.format("", new File("")); + }).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."); } @Test