diff --git a/CHANGES.md b/CHANGES.md index 69bbaa1fd2..ca1f965898 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] +### Fixed +* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599)) ## [3.0.0.BETA3] - 2024-10-15 ### Added diff --git a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java index 32ef497de8..78bd28f86b 100644 --- a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,6 +74,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format( + String unix, Path path, Path editorConfigPath, Map editorConfigOverrideMap) { @@ -105,7 +106,7 @@ public String format( editorConfig, editorConfigOverride, false) - .format(path, formatterCallback); + .format(unix, path, formatterCallback); } /** diff --git a/lib/src/compatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0Adapter.java b/lib/src/compatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0Adapter.java index 00c6744b4c..f4c1e04af7 100644 --- a/lib/src/compatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0Adapter.java @@ -121,6 +121,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format( + String unix, Path path, Path editorConfigPath, Map editorConfigOverrideMap) { @@ -146,13 +147,16 @@ public String format( editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders)); } + // 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); return new KtLintRuleEngine( allRuleProviders, editorConfig, editorConfigOverride, false, path.getFileSystem()) - .format(Code.Companion.fromPath(path), formatterCallback); + .format(code, formatterCallback); } /** diff --git a/lib/src/compatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0Adapter.java b/lib/src/compatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0Adapter.java index b683b84ca1..951b82704a 100644 --- a/lib/src/compatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0Adapter.java @@ -83,6 +83,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format( + String unix, Path path, Path editorConfigPath, Map editorConfigOverrideMap) { @@ -108,6 +109,9 @@ public String format( editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders)); } + // 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); return new KtLintRuleEngine( allRuleProviders, editorConfig, @@ -115,7 +119,7 @@ public String format( true, false, path.getFileSystem()) - .format(Code.Companion.fromPath(path), formatterCallback); + .format(code, formatterCallback); } /** diff --git a/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java b/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java index 0e97cdd12d..86387d0d6b 100644 --- a/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java +++ b/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java @@ -82,6 +82,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format( + String unix, Path path, Path editorConfigPath, Map editorConfigOverrideMap) { @@ -103,13 +104,16 @@ public String format( 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); return new KtLintRuleEngine( allRuleProviders, editorConfig, editorConfigOverride, false, path.getFileSystem()) - .format(Code.Companion.fromPath(path), formatterCallback); + .format(code, formatterCallback); } /** diff --git a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java index 1bfa0da6da..0fcecb3f6c 100644 --- a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java +++ b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2023 DiffPlug + * Copyright 2022-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +15,31 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.lang.reflect.Field; import java.nio.file.Path; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Map; public interface KtLintCompatAdapter { String format( + String content, Path path, Path editorConfigPath, - Map editorConfigOverrideMap); + Map editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException; + + static void setCodeContent(Object code, String content) { + AccessController.doPrivileged((PrivilegedAction) () -> { + try { + Field contentField = code.getClass().getDeclaredField("content"); + contentField.setAccessible(true); + contentField.set(code, content); + } catch (NoSuchFieldException | IllegalAccessException e) { + // Handle exceptions as needed + throw new RuntimeException("Failed to set content field", e); + } + return null; + }); + } } 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 b81e107609..a8e5bc32fc 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-2023 DiffPlug + * Copyright 2021-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,12 +60,13 @@ public KtlintFormatterFunc( } @Override - public String applyWithFile(String unix, File file) { + public String applyWithFile(String unix, File file) throws NoSuchFieldException, IllegalAccessException { Path absoluteEditorConfigPath = null; if (editorConfigPath != null) { absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath(); } return adapter.format( + unix, file.toPath(), absoluteEditorConfigPath, editorConfigOverrideMap); diff --git a/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java b/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java index 17fe40fb9c..f9c19d69a7 100644 --- a/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java +++ b/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,19 +33,19 @@ public class KtLintCompat0Dot48Dot0AdapterTest { @Test public void testDefaults(@TempDir Path path) throws IOException { KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter(); - loadAndWriteText(path, "empty_class_body.kt"); + var content = loadAndWriteText(path, "empty_class_body.kt"); final Path filePath = Paths.get(path.toString(), "empty_class_body.kt"); Map editorConfigOverrideMap = new HashMap<>(); - String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class empty_class_body\n", formatted); } @Test public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter(); - loadAndWriteText(path, "fails_no_semicolons.kt"); + var content = loadAndWriteText(path, "fails_no_semicolons.kt"); final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt"); Map editorConfigOverrideMap = new HashMap<>(); @@ -54,7 +54,7 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { // ktlint_filename is an invalid rule in ktlint 0.48.0 editorConfigOverrideMap.put("ktlint_filename", "disabled"); - String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted); } diff --git a/lib/src/testCompatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0AdapterTest.java b/lib/src/testCompatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0AdapterTest.java index b34a83ebc3..27d085f7fd 100644 --- a/lib/src/testCompatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0AdapterTest.java +++ b/lib/src/testCompatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0AdapterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,26 +33,26 @@ public class KtLintCompat0Dot49Dot0AdapterTest { @Test public void testDefaults(@TempDir Path path) throws IOException { KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter(); - loadAndWriteText(path, "EmptyClassBody.kt"); + var content = loadAndWriteText(path, "EmptyClassBody.kt"); final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt"); Map editorConfigOverrideMap = new HashMap<>(); - String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class EmptyClassBody\n", formatted); } @Test public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter(); - loadAndWriteText(path, "FailsNoSemicolons.kt"); + var content = loadAndWriteText(path, "FailsNoSemicolons.kt"); final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt"); Map editorConfigOverrideMap = new HashMap<>(); editorConfigOverrideMap.put("indent_style", "tab"); editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled"); - String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted); } diff --git a/lib/src/testCompatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0AdapterTest.java b/lib/src/testCompatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0AdapterTest.java index 0bb501a06e..27dce68f09 100644 --- a/lib/src/testCompatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0AdapterTest.java +++ b/lib/src/testCompatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0AdapterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,26 +33,26 @@ public class KtLintCompat0Dot50Dot0AdapterTest { @Test public void testDefaults(@TempDir Path path) throws IOException { KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter(); - loadAndWriteText(path, "EmptyClassBody.kt"); + var content = loadAndWriteText(path, "EmptyClassBody.kt"); final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt"); Map editorConfigOverrideMap = new HashMap<>(); - String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class EmptyClassBody\n", formatted); } @Test public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter(); - loadAndWriteText(path, "FailsNoSemicolons.kt"); + var content = loadAndWriteText(path, "FailsNoSemicolons.kt"); final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt"); Map editorConfigOverrideMap = new HashMap<>(); editorConfigOverrideMap.put("indent_style", "tab"); editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled"); - String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted); } diff --git a/lib/src/testCompatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0AdapterTest.java b/lib/src/testCompatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0AdapterTest.java index 0f5f9cf16c..760aa89ae5 100644 --- a/lib/src/testCompatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0AdapterTest.java +++ b/lib/src/testCompatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0AdapterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,26 +33,26 @@ public class KtLintCompat1Dot0Dot0AdapterTest { @Test public void testDefaults(@TempDir Path path) throws IOException { KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter(); - loadAndWriteText(path, "EmptyClassBody.kt"); + var content = loadAndWriteText(path, "EmptyClassBody.kt"); final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt"); Map editorConfigOverrideMap = new HashMap<>(); - String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class EmptyClassBody\n", formatted); } @Test public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter(); - loadAndWriteText(path, "FailsNoSemicolons.kt"); + var content = loadAndWriteText(path, "FailsNoSemicolons.kt"); final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt"); Map editorConfigOverrideMap = new HashMap<>(); editorConfigOverrideMap.put("indent_style", "tab"); editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled"); - String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap); + String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap); assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted); } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index fe3d1aa529..c5e2171005 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] +### Fixed +* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599)) ## [7.0.0.BETA3] - 2024-10-15 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 4f11ab296a..4eb157e7d0 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] +### Fixed +* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599)) ## [2.44.0.BETA3] - 2024-10-15 ### Added