ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

Wrong file path resolved when using --editorconfig

Open NyanSten opened this issue 2 years ago • 3 comments

When using --editorconfig option, a wrong path will be resolved.

Without --editorconfig:

12:13:49.290 [pool-1-thread-1] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for app/src/main/kotlin/com/example/Example1.kt file path:
12:13:49.290 [pool-1-thread-2] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for app/src/main/kotlin/com/example/Example2.kt file path:

With --editorconfig=/tmp/editorconfig:

12:15:04.346 [pool-1-thread-2] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for /tmp/editorconfig/Example1.kt file path:                                                                                                                                                                                                                                                   
12:15:04.348 [pool-1-thread-1] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for /tmp/editorconfig/Example2.kt file path:

Notice that the file's director name has been replaced with the editorconfig path. Path-specific rules for e.g. [**/example/*.kt] won't be applied.

NyanSten avatar Jul 19 '22 00:07 NyanSten

With --editorconfig=/tmp/editorconfig:

12:15:04.346 [pool-1-thread-2] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for /tmp/editorconfig/Example1.kt file path:                                                                                                                                                                                                                                                   
12:15:04.348 [pool-1-thread-1] TRACE com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for /tmp/editorconfig/Example2.kt file path:

The logging above is indeed inccorrect.

Path-specific rules for e.g. [**/example/*.kt] won't be applied.

Whenever the --editorconfig option is used, all .editorconfig files on the file path are ignored. I expect that this was a consicious design choice but it has not been documented (or at least I can not find it). Suppose that an .editorconfig file is found on the file path. Is the setting from the specified .editorconfig file more or less import than from the file found on the file path? I expect that this differs per user.

But the least that could be done, is to mention the current behavior in the CLI help.

paul-dingemans avatar Jul 21 '22 19:07 paul-dingemans

Whenever the --editorconfig option is used, all .editorconfig files on the file path are ignored. I expect that this was a consicious design choice but it has not been documented (or at least I can not find it). Suppose that an .editorconfig file is found on the file path. Is the setting from the specified .editorconfig file more or less import than from the file found on the file path? I expect that this differs per user.

That is fine, I expected that.

The problem is with path-based rules in the provided EditorConfig. If the EditorConfig looks like this:

[*.{kt,kts}]
max_line_length = 140

[app/**/example/*.kt]
disabled_rules = filename

Then when it is found by directory traversal, both max_line_length and disabled_rules rules are applied to app/src/main/kotlin/com/example/Example1.kt, but when it is specified via the --editorconfig option, only the max_line_length rule is applied because the rewritten file name does not match the latter section.

NyanSten avatar Jul 21 '22 22:07 NyanSten

Tnx for this clarification. That indeed sounds like a bug.

paul-dingemans avatar Jul 25 '22 18:07 paul-dingemans