Skip to content

Commit 3b6401d

Browse files
authored
feat: read scalafmt version from scalafmt config if not provided (#2922)
2 parents 886ee99 + 016cc06 commit 3b6401d

10 files changed

Lines changed: 93 additions & 21 deletions

File tree

‎CHANGES.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111

1212
## [Unreleased]
1313
### Added
14+
- `scalafmt()` now reads the version from the `version` field in the scalafmt config file when no version is explicitly set in the plugin config, falling back to the built-in default only if neither is available. ([#2922](https://github.com/diffplug/spotless/pull/2922))
1415
- Add `versionCatalog` step for formatting and sorting Gradle version catalog (`.toml`) files. ([#2916](https://github.com/diffplug/spotless/issues/2916))
1516
- Add `javaparserVersion` option to the Cleanthat step, allowing callers to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903))
1617
### Fixed

‎lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2025 DiffPlug
2+
* Copyright 2016-2026 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.
@@ -20,7 +20,9 @@
2020
import java.io.Serial;
2121
import java.io.Serializable;
2222
import java.lang.reflect.Constructor;
23+
import java.nio.file.Files;
2324
import java.util.Collections;
25+
import java.util.Optional;
2426
import java.util.Set;
2527

2628
import javax.annotation.Nullable;
@@ -54,15 +56,16 @@ public static FormatterStep create(Provisioner provisioner) {
5456
return create(defaultVersion(), defaultScalaMajorVersion(), provisioner, null);
5557
}
5658

57-
public static FormatterStep create(String version, Provisioner provisioner, @Nullable File configFile) {
59+
public static FormatterStep create(@Nullable String version, Provisioner provisioner, @Nullable File configFile) {
5860
return create(version, defaultScalaMajorVersion(), provisioner, configFile);
5961
}
6062

61-
public static FormatterStep create(String version, @Nullable String scalaMajorVersion, Provisioner provisioner, @Nullable File configFile) {
63+
public static FormatterStep create(@Nullable String version, @Nullable String scalaMajorVersion, Provisioner provisioner, @Nullable File configFile) {
64+
String finalVersion = version != null ? version : versionFromConfig(configFile).orElse(defaultVersion());
6265
String finalScalaMajorVersion = scalaMajorVersion == null ? DEFAULT_SCALA_MAJOR_VERSION : scalaMajorVersion;
6366

6467
return FormatterStep.create(NAME,
65-
new ScalaFmtStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + finalScalaMajorVersion + ":" + version, provisioner)), configFile),
68+
new ScalaFmtStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + finalScalaMajorVersion + ":" + finalVersion, provisioner)), configFile),
6669
ScalaFmtStep::equalityState,
6770
State::createFormat);
6871
}
@@ -75,6 +78,22 @@ public static String defaultScalaMajorVersion() {
7578
return DEFAULT_SCALA_MAJOR_VERSION;
7679
}
7780

81+
private static Optional<String> versionFromConfig(@Nullable File configFile) {
82+
try {
83+
return configFile == null || !configFile.exists() ? Optional.empty()
84+
: Files.readAllLines(configFile.toPath())
85+
.stream()
86+
.filter(line -> line.trim().startsWith("version"))
87+
.findFirst()
88+
.flatMap(line -> {
89+
var parts = line.replaceAll("\\s", "").split("=");
90+
return parts.length < 2 ? Optional.empty() : Optional.of(parts[1]);
91+
});
92+
} catch (IOException e) {
93+
return Optional.empty();
94+
}
95+
}
96+
7897
private State equalityState() throws IOException {
7998
return new State(jarState.get(), configFile);
8099
}

‎plugin-gradle/CHANGES.md‎

Lines changed: 1 addition & 0 deletions
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
### Added
7+
- `scalafmt()` now reads the version from the `version` field in the scalafmt config file when no version is explicitly set in the plugin config, falling back to the built-in default only if neither is available. ([#2922](https://github.com/diffplug/spotless/pull/2922))
78
- Add `toml` format type with `versionCatalog()` step for formatting and sorting Gradle version catalog files. ([#2916](https://github.com/diffplug/spotless/issues/2916))
89
- Add `withIndentStyle` and `withIndentSize` configuration to `tableTestFormatter` for setting the fallback indent when no `.editorconfig` is found. ([#2893](https://github.com/diffplug/spotless/pull/2893))
910
- Add `javaparserVersion(...)` to `cleanthat`, allowing users to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903))

‎plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2026 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.
@@ -35,22 +35,20 @@ public ScalaExtension(SpotlessExtension spotless) {
3535
}
3636

3737
public ScalaFmtConfig scalafmt() {
38-
return scalafmt(ScalaFmtStep.defaultVersion());
38+
return new ScalaFmtConfig(null);
3939
}
4040

4141
public ScalaFmtConfig scalafmt(String version) {
42-
return new ScalaFmtConfig(version);
42+
return new ScalaFmtConfig(Objects.requireNonNull(version));
4343
}
4444

4545
public class ScalaFmtConfig {
46-
final String version;
47-
@Nullable
48-
String scalaMajorVersion;
49-
@Nullable
50-
Object configFile;
46+
@Nullable final String version;
47+
@Nullable String scalaMajorVersion;
48+
@Nullable Object configFile;
5149

52-
ScalaFmtConfig(String version) {
53-
this.version = Objects.requireNonNull(version);
50+
ScalaFmtConfig(@Nullable String version) {
51+
this.version = version;
5452
addStep(createStep());
5553
}
5654

‎plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ScalaExtensionTest.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2026 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.
@@ -20,6 +20,25 @@
2020
import org.junit.jupiter.api.Test;
2121

2222
class ScalaExtensionTest extends GradleIntegrationHarness {
23+
@Test
24+
void versionFromConfig() throws IOException {
25+
setFile("build.gradle").toLines(
26+
"plugins {",
27+
" id 'com.diffplug.spotless'",
28+
"}",
29+
"repositories { mavenCentral() }",
30+
"apply plugin: 'scala'",
31+
"spotless {",
32+
" scala {",
33+
" scalafmt().configFile('scalafmt.conf')",
34+
" }",
35+
"}");
36+
setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt_newer.conf");
37+
setFile("src/main/scala/basic.scala").toResource("scala/scalafmt/basic.dirty");
38+
gradleRunner().withArguments("spotlessApply").build();
39+
assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
40+
}
41+
2342
@Test
2443
void integration() throws IOException {
2544
setFile("build.gradle").toLines(

‎plugin-maven/CHANGES.md‎

Lines changed: 1 addition & 0 deletions
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
### Added
7+
- `<scalafmt>` now reads the version from the `version` field in the scalafmt config file when no `<version>` is explicitly set, falling back to the built-in default only if neither is available. ([#2922](https://github.com/diffplug/spotless/pull/2922))
78
- Add `<toml>` format type with `<versionCatalog>` step for formatting and sorting Gradle version catalog files. ([#2916](https://github.com/diffplug/spotless/issues/2916))
89
- Add `<javaparserVersion>` option to `<cleanthat>`, allowing users to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903))
910
### Fixed

‎plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scalafmt.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2026 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.
@@ -37,9 +37,7 @@ public class Scalafmt implements FormatterStepFactory {
3737

3838
@Override
3939
public FormatterStep newFormatterStep(FormatterStepConfig config) {
40-
String scalafmtVersion = version != null ? version : ScalaFmtStep.defaultVersion();
41-
String scalafmtScalaMajorVersion = scalaMajorVersion != null ? scalaMajorVersion : ScalaFmtStep.defaultScalaMajorVersion();
4240
File configFile = config.getFileLocator().locateFile(file);
43-
return ScalaFmtStep.create(scalafmtVersion, scalafmtScalaMajorVersion, config.getProvisioner(), configFile);
41+
return ScalaFmtStep.create(version, scalaMajorVersion, config.getProvisioner(), configFile);
4442
}
4543
}

‎plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2026 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.
@@ -39,6 +39,18 @@ void testScalafmtWithCustomConfig() throws Exception {
3939
runTest("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
4040
}
4141

42+
@Test
43+
void testScalafmtVersionFromConfig() throws Exception {
44+
setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt_newer.conf");
45+
46+
writePomWithScalaSteps(
47+
"<scalafmt>",
48+
" <file>${project.basedir}/scalafmt.conf</file>",
49+
"</scalafmt>");
50+
51+
runTest("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
52+
}
53+
4254
private void runTest(String s) throws Exception {
4355
String path = "src/main/scala/test.scala";
4456
setFile(path).toResource("scala/scalafmt/basic.dirty");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version = 3.11.0
2+
runner.dialect = scala213
3+
runner.dialectFeatures = [relaxedLambdaSyntax]
4+
style = defaultWithAlign
5+
maxColumn = 20

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2025 DiffPlug
2+
* Copyright 2016-2026 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.
@@ -88,6 +88,24 @@ protected FormatterStep create() {
8888
}.testEquals();
8989
}
9090

91+
@Test
92+
void behaviorVersionFromConfigFile() {
93+
StepHarness.forStep(ScalaFmtStep.create((String) null, TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf")))
94+
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
95+
}
96+
97+
@Test
98+
void behaviorVersionFromConfigFileNewerVersion() {
99+
StepHarness.forStep(ScalaFmtStep.create((String) null, TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt_newer.conf")))
100+
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
101+
}
102+
103+
@Test
104+
void behaviorDefaultVersionWithoutConfigFile() {
105+
StepHarness.forStep(ScalaFmtStep.create((String) null, TestProvisioner.mavenCentral(), null))
106+
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_3.0.0");
107+
}
108+
91109
@Test
92110
void invalidConfiguration() {
93111
File invalidConfFile = createTestFile("scala/scalafmt/scalafmt.invalid.conf");

0 commit comments

Comments
 (0)