From 2a9631d7edf9eb2846bf19b477e7af6882c3a73e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 15:55:15 +0000 Subject: [PATCH 1/2] Run compatibility adapter tests on the JUnit Platform The test tasks registered by the version-compatibility plugin (testCompatKtLint1Dot0Dot0, testCompatCleanthat2Dot1, ...) never called useJUnitPlatform(), so they discovered none of the JUnit 5 adapter tests. Together with failOnNoDiscoveredTests = false this went unnoticed and the adapter tests have silently been skipped. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MTr23vEiR4QymHV1bKhe6h --- lib/build.gradle.kts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 1e7f44de8c..9b8f9956a0 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -201,6 +201,12 @@ tasks.withType().configureEach { failOnNoDiscoveredTests = false } +// the adapter test tasks registered by the version-compatibility plugin default to JUnit 4, +// which silently discovers none of our JUnit 5 tests +tasks.withType().matching { it.name.startsWith("testCompat") }.configureEach { + useJUnitPlatform() +} + tasks.jar { for (glue in needsGlue) { from(sourceSets.getByName(glue).output.classesDirs) From c1abba580fa4e73bee48256371fdd9f448e69105 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 15:55:15 +0000 Subject: [PATCH 2/2] Support ktlint 2.x (io.github.ktlint.core) ktlint 2.x is published under the new Maven group io.github.ktlint.core and moved its packages from com.pinterest.ktlint to io.github.ktlint.core, so changing the coordinate alone is not enough (#3156): - KtLintStep resolves io.github.ktlint.core:ktlint-cli for versions >= 2. - New KtLintCompat2Dot0Dot0Adapter built against the 2.x API (RuleSetV2Provider/RuleV2Provider, new KtLintRuleEngine constructor and format(...) signature). KtlintFormatterFunc dispatches to it for major versions >= 2. - ktlint 2.x asks for the autocorrect decision while a rule is executing and wraps exceptions thrown there, so uncorrectable violations are collected and reported as a Spotless lint after formatting finished. - Like for 1.x, ktlint_code_style defaults to intellij_idea when overrides are given but no code style is configured. The caller's override map is no longer mutated. The default ktlint version stays 1.8.0 as 2.x is still in alpha. Custom rule sets written for ktlint 1.x are not supported on 2.x: since 2.0.0-ALPHA-5 ktlint uses the Kotlin Analysis API (com.intellij.*) instead of kotlin-compiler-embeddable (org.jetbrains.kotlin.com.intellij.*), so 1.x rules no longer override the visitor methods of the backward compatibility module. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MTr23vEiR4QymHV1bKhe6h --- CHANGES.md | 2 + gradle/libs.versions.toml | 3 + lib/build.gradle.kts | 8 +- .../compat/KtLintCompat2Dot0Dot0Adapter.java | 168 ++++++++++++++++++ .../glue/ktlint/KtlintFormatterFunc.java | 7 +- .../diffplug/spotless/kotlin/KtLintStep.java | 15 +- .../KtLintCompat2Dot0Dot0AdapterTest.java | 80 +++++++++ .../resources/EmptyClassBody.kt | 3 + .../resources/FailsNoSemicolons.kt | 3 + .../resources/FailsWildcardImport.kt | 5 + plugin-gradle/CHANGES.md | 2 + plugin-maven/CHANGES.md | 2 + .../spotless/kotlin/KtLintStepTest.java | 10 +- 13 files changed, 301 insertions(+), 7 deletions(-) create mode 100644 lib/src/compatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0Adapter.java create mode 100644 lib/src/testCompatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0AdapterTest.java create mode 100644 lib/src/testCompatKtLint2Dot0Dot0/resources/EmptyClassBody.kt create mode 100644 lib/src/testCompatKtLint2Dot0Dot0/resources/FailsNoSemicolons.kt create mode 100644 lib/src/testCompatKtLint2Dot0Dot0/resources/FailsWildcardImport.kt diff --git a/CHANGES.md b/CHANGES.md index 5a3c98ad78..2a86435c11 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +- Support ktlint `2.x` (Maven group `io.github.ktlint.core`), e.g. `ktlint("2.0.0-ALPHA-5")`. The default version stays `1.8.0`. Note that ktlint `2.x` renamed some rules (e.g. `no-semi` is now `semicolon`) and only runs custom rule sets built for ktlint `2.x`. ([#3156](https://github.com/diffplug/spotless/issues/3156)) ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1823b87cf1..dfe51c0ef3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,6 +4,7 @@ durian = "1.2.0" selfie = "3.1.2" jackson = "2.23.0" ktlint = "1.8.0" +ktlint2 = "2.0.0-ALPHA-5" javaparser = "3.28.2" [libraries] @@ -22,6 +23,8 @@ jackson-dataformat-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-d ktlint-rule-engine = { module = "com.pinterest.ktlint:ktlint-rule-engine", version.ref = "ktlint" } ktlint-ruleset-standard = { module = "com.pinterest.ktlint:ktlint-ruleset-standard", version.ref = "ktlint" } +ktlint2-rule-engine = { module = "io.github.ktlint.core:ktlint-rule-engine", version.ref = "ktlint2" } +ktlint2-ruleset-standard = { module = "io.github.ktlint.core:ktlint-ruleset-standard", version.ref = "ktlint2" } javaparser-core = { module = "com.github.javaparser:javaparser-core", version.ref = "javaparser" } javaparser-symbol-solver-core = { module = "com.github.javaparser:javaparser-symbol-solver-core", version.ref = "javaparser" } diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 9b8f9956a0..6ebd30101a 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -111,7 +111,7 @@ versionCompatibility { // we will support no more than 2 breaking changes at a time = 3 incompatible versions // we will try to drop down to only one version if a stable API can be maintained for a full // year - versions = listOf("1.0.0") + versions = listOf("1.0.0", "2.0.0") targetSourceSetName = "ktlint" } namespaces.register("Diktat") { @@ -165,10 +165,14 @@ dependencies { "ktfmtCompileOnly"(libs.ktfmt) "ktfmtCompileOnly"(pinnedLibs.google.java.format) // for JDK 8 compatibility "ktfmtCompileOnly"(libs.jsr305) - // ktlint latest supported version + // ktlint 1.x (com.pinterest.ktlint) "compatKtLint1Dot0Dot0CompileAndTestOnly"(libs.ktlint.rule.engine) "compatKtLint1Dot0Dot0CompileAndTestOnly"(libs.ktlint.ruleset.standard) "compatKtLint1Dot0Dot0CompileAndTestOnly"(libs.slf4j.api) + // ktlint 2.x (io.github.ktlint.core) + "compatKtLint2Dot0Dot0CompileAndTestOnly"(libs.ktlint2.rule.engine) + "compatKtLint2Dot0Dot0CompileAndTestOnly"(libs.ktlint2.ruleset.standard) + "compatKtLint2Dot0Dot0CompileAndTestOnly"(libs.slf4j.api) // palantirJavaFormat "palantirJavaFormatCompileOnly"( pinnedLibs.palantir.java.format, diff --git a/lib/src/compatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0Adapter.java b/lib/src/compatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0Adapter.java new file mode 100644 index 0000000000..1dffd92d81 --- /dev/null +++ b/lib/src/compatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0Adapter.java @@ -0,0 +1,168 @@ +/* + * Copyright 2026 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.glue.ktlint.compat; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.ServiceLoader; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import io.github.ktlint.core.cli.ruleset.core.api.RuleSetV2Provider; +import io.github.ktlint.core.rule.engine.api.Code; +import io.github.ktlint.core.rule.engine.api.EditorConfigDefaults; +import io.github.ktlint.core.rule.engine.api.EditorConfigOverride; +import io.github.ktlint.core.rule.engine.api.KtLintRuleEngine; +import io.github.ktlint.core.rule.engine.api.LintError; +import io.github.ktlint.core.rule.engine.core.api.AutocorrectDecision; +import io.github.ktlint.core.rule.engine.core.api.RuleId; +import io.github.ktlint.core.rule.engine.core.api.RuleSetId; +import io.github.ktlint.core.rule.engine.core.api.RuleV2Provider; +import io.github.ktlint.core.rule.engine.core.api.RuleV2ProviderKt; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.CodeStyleEditorConfigPropertyKtKt; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.EditorConfigProperty; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.EndOfLinePropertyKt; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.IndentSizeEditorConfigPropertyKt; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.IndentStyleEditorConfigPropertyKt; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.InsertFinalNewLineEditorConfigPropertyKt; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.MaxLineLengthEditorConfigPropertyKt; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.RuleExecution; +import io.github.ktlint.core.rule.engine.core.api.editorconfig.RuleExecutionEditorConfigPropertyKtKt; +import kotlin.Pair; + +/** + * Adapter for ktlint 2.x, which moved from {@code com.pinterest.ktlint} to {@code io.github.ktlint.core}. + */ +public class KtLintCompat2Dot0Dot0Adapter implements KtLintCompatAdapter { + + private static final List> DEFAULT_EDITOR_CONFIG_PROPERTIES = List.of( + CodeStyleEditorConfigPropertyKtKt.getCODE_STYLE_PROPERTY(), + EndOfLinePropertyKt.getEND_OF_LINE_PROPERTY(), + IndentSizeEditorConfigPropertyKt.getINDENT_SIZE_PROPERTY(), + IndentStyleEditorConfigPropertyKt.getINDENT_STYLE_PROPERTY(), + InsertFinalNewLineEditorConfigPropertyKt.getINSERT_FINAL_NEWLINE_PROPERTY(), + MaxLineLengthEditorConfigPropertyKt.getMAX_LINE_LENGTH_PROPERTY(), + RuleExecutionEditorConfigPropertyKtKt.getEXPERIMENTAL_RULES_EXECUTION_PROPERTY()); + + @Override + public String format( + String unix, + Path path, + Path editorConfigPath, + Map editorConfigOverrideMap) { + Set allRuleProviders = loadRuleProviders(); + + EditorConfigDefaults editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleV2ProviderKt.propertyTypes(allRuleProviders)); + EditorConfigOverride editorConfigOverride; + if (editorConfigOverrideMap.isEmpty()) { + editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE(); + } else { + editorConfigOverride = createEditorConfigOverride(editorConfig, allRuleProviders, editorConfigOverrideMap); + } + + // create Code and then set the content to match previous steps in the Spotless pipeline + Code code = Code.Companion.fromPath(path); + KtLintCompatAdapter.setCodeContent(code, unix); + + // ktlint 2.x asks for the autocorrect decision while a rule is still executing, and wraps any exception thrown there, + // so violations are collected and only reported once formatting has finished + List uncorrected = new ArrayList<>(); + String formatted = new KtLintRuleEngine( + allRuleProviders, + editorConfig, + editorConfigOverride, + path.getFileSystem()) + .format( + code, + true, + lint -> { + uncorrected.add(lint); + return kotlin.Unit.INSTANCE; + }, + lint -> { + if (!lint.getCanBeAutoCorrected()) { + uncorrected.add(lint); + } + return AutocorrectDecision.ALLOW_AUTOCORRECT; + }); + if (!uncorrected.isEmpty()) { + LintError lint = uncorrected.get(0); + KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId().getValue(), lint.getDetail()); + } + return formatted; + } + + private static Set loadRuleProviders() { + return ServiceLoader.load(RuleSetV2Provider.class, RuleSetV2Provider.class.getClassLoader()) + .stream() + .flatMap(loader -> loader.get().getRuleProviders().stream()) + .collect(Collectors.toUnmodifiableSet()); + } + + /** + * Create EditorConfigOverride from user provided parameters. + */ + private static EditorConfigOverride createEditorConfigOverride(final EditorConfigDefaults editorConfig, final Set ruleProviders, Map editorConfigOverrideMap) { + // Create a mapping of properties to their names based on rule properties and default properties + Map> supportedProperties = Stream + .concat(ruleProviders.stream().flatMap(provider -> provider.getUsesEditorConfigProperties().stream()), DEFAULT_EDITOR_CONFIG_PROPERTIES.stream()) + .distinct() + .collect(Collectors.toMap(EditorConfigProperty::getName, property -> property, (first, second) -> first)); + + Map overrides = new LinkedHashMap<>(editorConfigOverrideMap); + // Spotless keeps intellij_idea as default code style, as it did for ktlint 1.x + boolean isCodeStyleDefinedInEditorConfig = editorConfig.getValue().getSections().stream() + .anyMatch(section -> section.getProperties().containsKey("ktlint_code_style")); + if (!isCodeStyleDefinedInEditorConfig && !overrides.containsKey("ktlint_code_style")) { + overrides.put("ktlint_code_style", "intellij_idea"); + } + + // Create config properties based on provided property names and values + @SuppressWarnings("unchecked") + Pair, ?>[] properties = overrides.entrySet().stream() + .map(entry -> { + EditorConfigProperty property = supportedProperties.get(entry.getKey()); + + if (property == null && entry.getKey().startsWith("ktlint_")) { + String[] parts = entry.getKey().substring(7).split("_", 2); + if (parts.length == 1) { + // convert ktlint_{ruleset} to RuleSetId + RuleSetId id = new RuleSetId(parts[0]); + property = RuleExecutionEditorConfigPropertyKtKt.createRuleSetExecutionEditorConfigProperty(id, RuleExecution.ENABLED); + } else { + // convert ktlint_{ruleset}_{rulename} to RuleId + RuleId id = new RuleId(parts[0] + ":" + parts[1]); + property = RuleExecutionEditorConfigPropertyKtKt.createRuleExecutionEditorConfigProperty(id, RuleExecution.ENABLED); + } + } + + if (property == null) { + return null; + } else { + return new Pair<>(property, entry.getValue()); + } + }) + .filter(Objects::nonNull) + .toArray(Pair[]::new); + + return EditorConfigOverride.Companion.from(properties); + } +} diff --git a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java index eb3a175fc4..e09b4e3968 100644 --- a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java +++ b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2025 DiffPlug + * Copyright 2021-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.Lint; import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat1Dot0Dot0Adapter; +import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat2Dot0Dot0Adapter; import com.diffplug.spotless.glue.ktlint.compat.KtLintCompatAdapter; import com.diffplug.spotless.glue.ktlint.compat.KtLintCompatReporting; @@ -37,7 +38,9 @@ public KtlintFormatterFunc( Map editorConfigOverrideMap) { String[] versions = version.split("\\."); int majorVersion = Integer.parseInt(versions[0]); - if (majorVersion == 1) { + if (majorVersion >= 2) { + this.adapter = new KtLintCompat2Dot0Dot0Adapter(); + } else if (majorVersion == 1) { this.adapter = new KtLintCompat1Dot0Dot0Adapter(); } else { throw new IllegalStateException("Ktlint versions < 1.0.0 not supported!"); diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 558b20e1e7..16bf0af3b3 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -42,6 +42,7 @@ public final class KtLintStep implements Serializable { private static final String NAME = "ktlint"; private static final String MAVEN_COORDINATE_0_DOT = "com.pinterest:ktlint:"; private static final String MAVEN_COORDINATE_1_DOT = "com.pinterest.ktlint:ktlint-cli:"; + private static final String MAVEN_COORDINATE_2_DOT = "io.github.ktlint.core:ktlint-cli:"; private final JarState.Promised jarState; @Nullable private final FileSignature.Promised config; @@ -73,15 +74,25 @@ public static FormatterStep create(String version, List customRuleSets) { Objects.requireNonNull(version, "version"); Objects.requireNonNull(provisioner, "provisioner"); - String ktlintCoordinate = (version.startsWith("0.") ? MAVEN_COORDINATE_0_DOT : MAVEN_COORDINATE_1_DOT) + version; Set mavenCoordinates = new HashSet<>(customRuleSets); - mavenCoordinates.add(ktlintCoordinate); + mavenCoordinates.add(ktlintCoordinate(version)); return FormatterStep.create(NAME, new KtLintStep(version, JarState.promise(() -> JarState.from(mavenCoordinates, provisioner)), editorConfig, editorConfigOverride), KtLintStep::equalityState, State::createFormat); } + private static String ktlintCoordinate(String version) { + if (version.startsWith("0.")) { + return MAVEN_COORDINATE_0_DOT + version; + } else if (version.startsWith("1.")) { + return MAVEN_COORDINATE_1_DOT + version; + } else { + // ktlint 2.x moved from the com.pinterest.ktlint group to io.github.ktlint.core + return MAVEN_COORDINATE_2_DOT + version; + } + } + public static String defaultVersion() { return KotlinBuildConfig.VERSION_KTLINT; } diff --git a/lib/src/testCompatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0AdapterTest.java b/lib/src/testCompatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0AdapterTest.java new file mode 100644 index 0000000000..7989c44902 --- /dev/null +++ b/lib/src/testCompatKtLint2Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat2Dot0Dot0AdapterTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2026 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.glue.ktlint.compat; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class KtLintCompat2Dot0Dot0AdapterTest { + @Test + public void testDefaults(@TempDir Path path) throws IOException { + KtLintCompat2Dot0Dot0Adapter adapter = new KtLintCompat2Dot0Dot0Adapter(); + var content = loadAndWriteText(path, "EmptyClassBody.kt"); + final Path filePath = Path.of(path.toString(), "EmptyClassBody.kt"); + + Map editorConfigOverrideMap = new HashMap<>(); + + String formatted = adapter.format(content, filePath, null, editorConfigOverrideMap); + assertEquals("class EmptyClassBody\n", formatted); + } + + @Test + public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { + KtLintCompat2Dot0Dot0Adapter adapter = new KtLintCompat2Dot0Dot0Adapter(); + var content = loadAndWriteText(path, "FailsNoSemicolons.kt"); + final Path filePath = Path.of(path.toString(), "FailsNoSemicolons.kt"); + + Map editorConfigOverrideMap = new HashMap<>(); + editorConfigOverrideMap.put("indent_style", "tab"); + // ktlint 2.x renamed the 'no-semi' rule to 'semicolon' + editorConfigOverrideMap.put("ktlint_standard_semicolon", "disabled"); + + String formatted = adapter.format(content, filePath, null, editorConfigOverrideMap); + assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted); + } + + @Test + public void testUnfixableViolationIsReportedWithLineAndRuleId(@TempDir Path path) throws IOException { + KtLintCompat2Dot0Dot0Adapter adapter = new KtLintCompat2Dot0Dot0Adapter(); + var content = loadAndWriteText(path, "FailsWildcardImport.kt"); + final Path filePath = Path.of(path.toString(), "FailsWildcardImport.kt"); + + Map editorConfigOverrideMap = new HashMap<>(); + + KtLintCompatReporting.KtlintSpotlessException e = assertThrows(KtLintCompatReporting.KtlintSpotlessException.class, + () -> adapter.format(content, filePath, null, editorConfigOverrideMap)); + assertEquals(3, e.line); + assertEquals("standard:no-wildcard-imports", e.ruleId); + assertEquals("Wildcard import", e.detail); + } + + private static String loadAndWriteText(Path path, String name) throws IOException { + try (InputStream is = KtLintCompat2Dot0Dot0AdapterTest.class.getResourceAsStream("/" + name)) { + Files.copy(is, path.resolve(name)); + } + return Files.readString(path.resolve(name)); + } +} diff --git a/lib/src/testCompatKtLint2Dot0Dot0/resources/EmptyClassBody.kt b/lib/src/testCompatKtLint2Dot0Dot0/resources/EmptyClassBody.kt new file mode 100644 index 0000000000..7da53fb78d --- /dev/null +++ b/lib/src/testCompatKtLint2Dot0Dot0/resources/EmptyClassBody.kt @@ -0,0 +1,3 @@ +class EmptyClassBody { + +} diff --git a/lib/src/testCompatKtLint2Dot0Dot0/resources/FailsNoSemicolons.kt b/lib/src/testCompatKtLint2Dot0Dot0/resources/FailsNoSemicolons.kt new file mode 100644 index 0000000000..4cf05ceacf --- /dev/null +++ b/lib/src/testCompatKtLint2Dot0Dot0/resources/FailsNoSemicolons.kt @@ -0,0 +1,3 @@ +class FailsNoSemicolons { + val i = 0; +} diff --git a/lib/src/testCompatKtLint2Dot0Dot0/resources/FailsWildcardImport.kt b/lib/src/testCompatKtLint2Dot0Dot0/resources/FailsWildcardImport.kt new file mode 100644 index 0000000000..04fc2a93d4 --- /dev/null +++ b/lib/src/testCompatKtLint2Dot0Dot0/resources/FailsWildcardImport.kt @@ -0,0 +1,5 @@ +package demo + +import java.io.* + +val file = File("x") diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 03164b7e52..2d01595a56 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +- Support ktlint `2.x` (Maven group `io.github.ktlint.core`), e.g. `ktlint("2.0.0-ALPHA-5")`. The default version stays `1.8.0`. Note that ktlint `2.x` renamed some rules (e.g. `no-semi` is now `semicolon`) and only runs custom rule sets built for ktlint `2.x`. ([#3156](https://github.com/diffplug/spotless/issues/3156)) ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) - Fix `Cannot set the value of task ... property 'taskService'` when Spotless is applied to sibling projects but not to their parent, a regression since `8.3.0`. Applying it to the root project with `apply false` is no longer necessary. ([#2877](https://github.com/diffplug/spotless/issues/2877)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 626f743508..63a0ede9db 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +- Support ktlint `2.x` (Maven group `io.github.ktlint.core`), e.g. `ktlint("2.0.0-ALPHA-5")`. The default version stays `1.8.0`. Note that ktlint `2.x` renamed some rules (e.g. `no-semi` is now `semicolon`) and only runs custom rule sets built for ktlint `2.x`. ([#3156](https://github.com/diffplug/spotless/issues/3156)) ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) diff --git a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java index 6fbef25a76..bedc8b029b 100644 --- a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2025 DiffPlug + * Copyright 2016-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,14 @@ void works1_0_0() { .expectLintsOfResource("kotlin/ktlint/unsolvable.dirty").toBe("L1 ktlint(standard:no-empty-file) File 'unsolvable.dirty' should not be empty"); } + @Test + void works2_0_0() { + FormatterStep step = KtLintStep.create("2.0.0-ALPHA-5", TestProvisioner.mavenCentral()); + StepHarnessWithFile.forStep(this, step) + .testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean") + .expectLintsOfResource("kotlin/ktlint/unsolvable.dirty").toBe("L1 ktlint(standard:no-empty-file) File 'unsolvable.dirty' should not be empty"); + } + @Test void behavior() { FormatterStep step = KtLintStep.create(TestProvisioner.mavenCentral());