Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if ktlint_code_style is set in .editorconfig before overriding it #2143

Conversation

OlivierGenez
Copy link
Contributor

@OlivierGenez OlivierGenez commented May 28, 2024

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.

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.

@Goooler
Copy link
Member

Goooler commented May 28, 2024

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

@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();
}

@OlivierGenez
Copy link
Contributor Author

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

@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?

@Goooler
Copy link
Member

Goooler commented May 28, 2024

Ooooops, will take a look later.

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")) {
Copy link
Member

@Goooler Goooler May 29, 2024

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.

"spotless {",
" kotlin {",
" ktlint().editorConfigOverride([",
" ktlint_test_key: true,",
Copy link
Member

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?

Copy link
Contributor Author

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.

The default path was never set, which means that .editorconfig was never
picked up if not explicitly set.
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.
CHANGES.md Outdated Show resolved Hide resolved
plugin-gradle/CHANGES.md Outdated Show resolved Hide resolved
plugin-maven/CHANGES.md Outdated Show resolved Hide resolved
Comment on lines 47 to 52
if (editorConfigPath == null) {
File defaultEditorConfig = new File(".editorconfig");
if (defaultEditorConfig.exists()) {
editorConfigPath = defaultEditorConfig.getPath();
}
}
Copy link
Member

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

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.

Copy link
Contributor Author

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:

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

OlivierGenez and others added 2 commits May 31, 2024 23:11
Co-authored-by: Zongle Wang <wangzongler@gmail.com>
Comment on lines 47 to 52
if (editorConfigPath == null) {
File defaultEditorConfig = new File(".editorconfig");
if (defaultEditorConfig.exists()) {
editorConfigPath = defaultEditorConfig.getPath();
}
}
Copy link
Member

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();
}

Copy link
Member

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");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

Quick shot!

@nedtwigg nedtwigg merged commit 3308d33 into diffplug:main Jun 4, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants