GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Check if ktlint_code_style is set in .editorconfig before overriding it by OlivierGenez · Pull Request #2143 · diffplug/spotless

Conversation

@OlivierGenez

@OlivierGenez OlivierGenez commented May 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

ktlint_code_style gets unconditionally overridden to its default value (intellij_idea) when the editorConfigOverride map is non-empty but does not define it. As a result, it gets overridden even if it's actually defined in the .editorconfig file.

This change checks if ktlint_code_style is already defined in .editorconfig before overriding it to its default value.

Fixes #2142.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@OlivierGenez
Check if ktlint_code_style is set in .editorconfig before overriding it
cf6c76f
ktlint_code_style gets unconditionally overridden to its default value
(intellij_idea) when the editorConfigOverride map is non-empty but does
not define it. As a result, it gets overridden even if it's actually
defined in the .editorconfig file.

This change checks if ktlint_code_style is already defined in
.editorconfig before overriding it to its default value.

Fixes diffplug#2142.
@Goooler

This comment was marked as outdated.

Sign in to view
@Goooler

Goooler commented May 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I see it! Could you add a test case for this? Or just update

spotless/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java

Lines 130 to 147 in 5f840aa

@Test
void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException {
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktlint().editorConfigOverride([",
" ktlint_code_style: \"ktlint_official\",",
" ])",
" }",
"}");
checkKtlintOfficialStyle();
}

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@OlivierGenez

OlivierGenez commented May 28, 2024

Copy link
Copy Markdown
Contributor Author

I see it! Could you add a test case for this? Or just update

spotless/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java

Lines 130 to 147 in 5f840aa

@Test
void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException {
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlin {",
" ktlint().editorConfigOverride([",
" ktlint_code_style: \"ktlint_official\",",
" ])",
" }",
"}");
checkKtlintOfficialStyle();
}

Thanks for the quick reply!

The PR already has this test: https://github.com/diffplug/spotless/pull/2143/files#diff-7bbe43d03149d8aad5b95564d57dc5bf59cba4ce539a44e01bbe51215dcdae71R131-R147. Is it sufficient, or is there a specific/additional scenario I am missing that you're after?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Goooler

Goooler commented May 28, 2024

Copy link
Copy Markdown
Member

Ooooops, will take a look later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Goooler
Goooler approved these changes May 29, 2024
View reviewed changes
Comment thread ...int1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java
Comment on lines -135 to +138
if (!editorConfigOverrideMap.containsKey("ktlint_code_style")) {
boolean isCodeStyleDefinedInEditorConfig = editorConfig.getValue().getSections().stream()
.anyMatch(section -> section.getProperties().containsKey("ktlint_code_style"));
if (!isCodeStyleDefinedInEditorConfig && !editorConfigOverrideMap.containsKey("ktlint_code_style")) {

@Goooler Goooler May 29, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key point is here, I missed checking EditorConfig file properties in #1808.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java
"spotless {",
" kotlin {",
" ktlint().editorConfigOverride([",
" ktlint_test_key: true,",

@Goooler Goooler May 29, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, can you add a test case like 87537c0 for maven as well?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@OlivierGenez OlivierGenez May 29, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

While adding the test I realised that plugin-maven never defaulted EditorConfig's path to .editorconfig, so I fixed that too.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
OlivierGenez added 3 commits May 29, 2024 22:36
@OlivierGenez
Default EditorConfig path to ".editorconfig" in plugin-maven ktlint step
0be12f5
The default path was never set, which means that .editorconfig was never
picked up if not explicitly set.
@OlivierGenez
Add ktlint test to plugin-maven
ccf141e
This verifies that the previous fix which checks if ktlint_code_style is
already defined in .editorconfig before overriding it works as expected
in plugin-maven.
@OlivierGenez
Update CHANGES.md files
1453214
Goooler
Goooler reviewed May 31, 2024
View reviewed changes
Comment thread CHANGES.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread plugin-gradle/CHANGES.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread plugin-maven/CHANGES.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java Outdated
Comment on lines +47 to +52
if (editorConfigPath == null) {
File defaultEditorConfig = new File(".editorconfig");
if (defaultEditorConfig.exists()) {
editorConfigPath = defaultEditorConfig.getPath();
}
}

@Goooler Goooler May 31, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will load EMPTY_EDITOR_CONFIG_DEFAULTS before

spotless/lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java

Lines 96 to 101 in cf6c76f

EditorConfigDefaults editorConfig;
if (editorConfigPath == null || !Files.exists(editorConfigPath)) {
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS();
} else {
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
}

But this should parse the root .editorconfig, I forget the exact position, let me check.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@OlivierGenez OlivierGenez Jun 1, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! KtLintRuleEngine will load the .editorconfig.

However I believe we still need to load it ourselves as well in order to check if it sets ktlint_code_style in KtLintCompat1Dot0Dot0Adapter. Otherwise we will run into the original issue: ktlint_code_style will be overridden to its default value (intellij_idea) when the editorConfigOverride map is non-empty but does not define it. This is also what plugin-gradle does:

spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/BaseKotlinExtension.java

Lines 159 to 160 in add4c1e

File defaultEditorConfig = getProject().getRootProject().file(".editorconfig");
FileSignature editorConfigPath = defaultEditorConfig.exists() ? FileSignature.signAsList(defaultEditorConfig) : null;

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Goooler reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
OlivierGenez and others added 2 commits May 31, 2024 23:11
@OlivierGenez @Goooler
Fix CHANGES.md files links
2effcbd
Co-authored-by: Zongle Wang <[email protected]>
@OlivierGenez
Revert unneeded changes to KtlintTest
f33f8be
Goooler
Goooler approved these changes Jun 3, 2024
View reviewed changes
Comment thread plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java Outdated
Comment on lines +47 to +52
if (editorConfigPath == null) {
File defaultEditorConfig = new File(".editorconfig");
if (defaultEditorConfig.exists()) {
editorConfigPath = defaultEditorConfig.getPath();
}
}

@Goooler Goooler Jun 3, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (editorConfigPath == null) {
File defaultEditorConfig = new File(".editorconfig");
if (defaultEditorConfig.exists()) {
editorConfigPath = defaultEditorConfig.getPath();
}
}
if (editorConfigPath == null && defaultEditorConfig.exists()) {
editorConfigPath = defaultEditorConfig.getPath();
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Goooler Goooler Jun 3, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And add

	private static final File defaultEditorConfig = new File(".editorconfig");

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@OlivierGenez OlivierGenez Jun 3, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Goooler Goooler Jun 3, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick shot!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

OlivierGenez and nedtwigg reacted with laugh emoji
All reactions
  • 😄 2 reactions
@OlivierGenez
Apply minor Ktlint implementation refactor
397a7fd
@nedtwigg
nedtwigg merged commit 3308d33 into diffplug:main Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@Goooler Goooler Goooler approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

ktlint - ktlint_code_style gets unconditionally overridden to its default value when not defined in editorConfigOverride

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@OlivierGenez @Goooler @nedtwigg
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.