Skip to content

Support ktlint 2.x (io.github.ktlint.core) - #3207

Open
Pankraz76 wants to merge 2 commits into
diffplug:mainfrom
Pankraz76:feat/ktlint-2-support
Open

Pankraz76 wants to merge 2 commits into
diffplug:mainfrom
Pankraz76:feat/ktlint-2-support

Conversation

@Pankraz76

@Pankraz76 Pankraz76 commented Nov 23, 2026 •

Copy link
Copy Markdown

Resolves #3156.

Note

Stacked on #3206 (first commit), so the new adapter tests actually run. Kept as a draft while ktlint 2.x is in alpha; see "Open points".

ktlint 2.x is published under io.github.ktlint.core and moved its packages from com.pinterest.ktlint to io.github.ktlint.core. Changing the Maven coordinate alone is therefore not enough: the glue code is compiled against the 1.x API.

Changes

  • KtLintStep resolves io.github.ktlint.core:ktlint-cli for versions >= 2. The 0.x and 1.x paths are unchanged.
  • New KtLintCompat2Dot0Dot0Adapter against the 2.x API: RuleSetV2Provider/RuleV2Provider, the new KtLintRuleEngine constructor and the new format(...) signature. KtlintFormatterFunc dispatches to it for major >= 2, following the same pattern as the diktat glue.
  • ktlint 2.x requests the autocorrect decision while a rule executes and wraps exceptions thrown there in RuleExecutionException. Uncorrectable violations are therefore collected and reported as a Spotless Lint once formatting has finished.
  • As for 1.x, ktlint_code_style defaults to intellij_idea when overrides are set but no code style is configured. The caller's override map is no longer mutated.
  • The default version stays 1.8.0.

Tests

  • KtLintCompat2Dot0Dot0AdapterTest: defaults, an editorconfig override disabling a rule, and an unfixable violation reported with line and rule id.
  • KtLintStepTest.works2_0_0: end-to-end format and lint with 2.0.0-ALPHA-5.
  • Ran locally: compat adapter tests, :testlib:test --tests 'com.diffplug.spotless.kotlin.*' (24 tests), spotlessCheck, changelogCheck, and SpotBugs for main, ktlint and both compat source sets.
  • Not run locally: the Gradle and Maven plugin integration tests, because Maven Central rate-limited the environment. CI will be the first full run.

Open points

  1. The API is unstable between alphas. 2.0.0-ALPHA-4 → ALPHA-5 changed exactly the KtLintRuleEngine constructor and format(...) signatures this adapter uses, so it only works with ALPHA-5+. It may be better to merge once 2.0.0 final is out.
  2. Custom rule sets built for ktlint 1.x do not work on 2.x. Since ALPHA-5, ktlint uses the Kotlin Analysis API (com.intellij.*) instead of kotlin-compiler-embeddable (org.jetbrains.kotlin.com.intellij.*). Rules like io.nlopez.compose.rules:ktlint:0.4.25 load through ktlint-com-pinterest-backward-compatibility, but their beforeVisitChildNodes(org.jetbrains.kotlin.com.intellij.lang.ASTNode, …) no longer overrides the compat Rule, so they silently never fire. I verified this with a spike and left it out; it's documented in CHANGES. Should Spotless warn when only a 1.x rule set (RuleSetProviderV3) is found on ktlint 2.x?
  3. ktlint 2.x renamed rules, e.g. no-semi → semicolon, so existing editorConfigOverride keys can silently stop matching. This is noted in CHANGES.
  4. Related: Support using projects as step dependency (ktlint rule sets only at this time) #2999 also touches ktlint custom rule sets.
  • PR allows edits from maintainers
  • Summary of the change
  • CHANGES.md (lib, plugin-gradle, plugin-maven) with a link to the issue

🤖 Generated with Claude Code

https://claude.ai/code/session_01MTr23vEiR4QymHV1bKhe6h

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTr23vEiR4QymHV1bKhe6h
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 (diffplug#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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTr23vEiR4QymHV1bKhe6h
@Pankraz76
Pankraz76 marked this pull request as ready for review November 23, 2026 19:04

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Ktlint 2.0

2 participants