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

Rules from Compose ruleset are not (properly) executed #425

Closed
chrimaeon opened this issue Dec 25, 2023 · 16 comments
Closed

Rules from Compose ruleset are not (properly) executed #425

chrimaeon opened this issue Dec 25, 2023 · 16 comments

Comments

@chrimaeon
Copy link

When applying a custom lint rule via a jar from the settings, the plugin does not apply custom config from .editconfig.

Use case:
Applying the custom Compose rules from https://github.com/mrmans0n/compose-rules and then enabling ktlint_function_naming_ignore_when_annotated_with=Composable from the local .editorconfig does not disable the "ktlint:standard:function-naming" "error" in the IDE.

@paul-dingemans
Copy link
Collaborator

Rule id ktlint_function_naming_ignore_when_annotated_with is not complete. It should consist of 3 parts. First part is ktlint. Third part is the id of the rule within its ruleset. In your case that is most likely function_naming_ignore_when_annotated_with, but normally this is written in kebab case (function-naming-ignore-when-annotated-with). You are missing the second part, which is the id of the ruleset. Based on https://github.com/mrmans0n/compose-rules/blob/5edba09c8718a6a0dc387371d7e276ac9115b8dd/rules/ktlint/src/main/kotlin/io/nlopez/compose/rules/ktlint/ComposeRuleSetProvider.kt#L38, I would say that you need to use compose.

So try ktlint_compose_function-naming-ignore-when-annotated-with. I could however not locate any rule in the compose ruleset with id function-naming-ignore-when-annotated-with. So please at the compose project for the rule-id if it does not work.

@chrimaeon
Copy link
Author

chrimaeon commented Dec 26, 2023

it's actually a config from ktlint

https://github.com/pinterest/ktlint/blob/master/CHANGELOG.md#-features-1

and it works when running ktlint from the command line.

it was your PR 🙂 pinterest/ktlint#2275

@paul-dingemans
Copy link
Collaborator

Lol, I totally forgot about that already. Could it be that you still are running version '0.13.0' of the plugin? That one uses an old ktlint version (0.48).

New version of plugin is on its way.

@chrimaeon
Copy link
Author

no, I downloaded the 0.20.0-beta-6 from the beta channel.

@paul-dingemans
Copy link
Collaborator

Most likely you are seeing a violation of a default inspection of IntelliJ. Ktlint can not disable those inspections.

In 'Problems' you can see that the violations do not contain a reference to a ktlint rule.
Screenshot 2023-12-27 at 12 18 55

Also when you choose a quick fix for the violation, you can go to the inspections page and disable the inspection.
Screenshot 2023-12-27 at 12 19 14

@chrimaeon
Copy link
Author

no its a standard:function-naming from ktlint

image

@chrimaeon
Copy link
Author

chrimaeon commented Dec 27, 2023

And after updating to 0.20.0 I don't see any "warnings"/"errors" for a custom rule set in the IDE. formatting works fine but it does not show any ktlint related issues that are shown using the CLI.

Also the idea.log does not show any outputs anymore - are they disabled in this release?

@chrimaeon
Copy link
Author

Screenshot 2023-12-27 at 13 22 39

@paul-dingemans
Copy link
Collaborator

As user of the beta version you might need to re-enable the plugin in the preferences page of ktlint. Logging has indeed been disabled, but can be enabled again by setting environment variable KTLINT_PLUGIN_LOG_TO_STDOUT=TRUE.

Please also post the code snippet as code (jnstead of image) as well as your .editorconfig.

@chrimaeon
Copy link
Author

chrimaeon commented Dec 27, 2023

Post.kt

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

@Composable
fun Post() {
    Text("foo")
}

.editorconfig

root = true
[*.{kt,kts}]
ij_kotlin_imports_layout = *,java.**,javax.**,kotlin.**,^
ij_kotlin_allow_trailing_comma = false
ij_kotlin_allow_trailing_comma_on_call_site = false
ktlint_function_naming_ignore_when_annotated_with=Composable

CLI error

/opt/homebrew/bin/ktlint -F -R <path>/scripts/ktlint-compose-0.3.8-all.jar .../Post.kt
<path>/Post.kt:11:5: This @Composable function emits content but doesn't have a modifier parameter.

See https://mrmans0n.github.io/compose-rules/rules/#when-should-i-expose-modifier-parameters for more information. (compose:modifier-missing-check)

Summary error count (descending) by rule:
  compose:modifier-missing-check: 1

Process finished with exit code 1

ktlint-plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="KtLint plugin">
    <ktlintMode>DISTRACT_FREE</ktlintMode>
    <externalJarPaths>
      <list>
        <option value="$PROJECT_DIR$/scripts/ktlint-compose-0.3.8-all.jar" />
      </list>
    </externalJarPaths>
  </component>
</project>

@paul-dingemans
Copy link
Collaborator

I have located a bug that might explain above. After adding or modifying the external ruleset that have to be run, the internal state of the plugin is not updated correctly. Can you restart you IDEA to see whether this resolves the problem?

@chrimaeon
Copy link
Author

chrimaeon commented Dec 28, 2023

Did that couple of times already. Re-install, disable, changing config - all if these don't show any changes.

Do you happen to know how Android Studio/IDEA would catch up the KTLINT_PLUGIN_LOG_TO_STDOUT=TRUE. I export it in my .zshrc but still no output in the logs.

started it from the command line. it shows logs now.

@chrimaeon
Copy link
Author

looks like the custom rule is at least loaded:

2023-12-28 11:56:18,540 [  16402]   INFO - STDOUT - Loaded 1 providers from ktlint jars
2023-12-28 11:56:18,570 [  16432]   INFO - STDOUT - Loaded 1 custom ruleset providers from file:/Users/christian/AndroidStudioProjects/phonews-android/scripts/ktlint-compose-0.3.8-all.jar

but no lint erros are showing up.

2023-12-28 12:00:20,062 [ 257924]   INFO - STDOUT - Finished ktlintFormat on file 'Post.kt' triggered by 'KtlintAnnotator' successfully
2023-12-28 12:00:20,456 [ 258318]   INFO - STDOUT - Annotator: KtlintAnnotatorUserData(modificationTimestamp=2146, lintErrors=[], displayAllKtlintViolations=false)
2023-12-28 12:00:20,486 [ 258348]   INFO - STDOUT - Start ktlintFormat on file 'Post.kt' triggered by 'KtlintActionOnSave'
2023-12-28 12:00:20,562 [ 258424]   INFO - STDOUT - Finished ktlintFormat on file 'Post.kt' 

@paul-dingemans
Copy link
Collaborator

I can confirm that the Compose rules do not work in the plugin. The rules are properly provided to the KtLintRuleEngine, which also calls the specific rule. Unfortunately, I loose track when debuging the actual call to the Compose rule.

To verify that custom ruleset work in the plugin, I have created a new custom ruleset with the no-var rule. When running with Ktlint CLI:

$ ktlint-1.1.0 --relative -R ~/Downloads/ktlint-compose-0.3.8-all.jar -R ~/Downloads/ktlint-custom-ruleset-example.jar
12:26:23.254 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine -- Enable default patterns [**/*.kt, **/*.kts]
src/main/kotlin/Foo.kt:4:5: This @Composable function emits content but doesn't have a modifier parameter.

See https://mrmans0n.github.io/compose-rules/rules/#when-should-i-expose-modifier-parameters for more information. (compose:modifier-missing-check)
src/main/kotlin/Foo.kt:8:5: Function name should start with a lowercase letter (except factory methods) and use camel case (standard:function-naming)
src/main/kotlin/Foo.kt:11:5: Unexpected var, use val instead (custom-rule-set-id:no-var)

Summary error count (descending) by rule:
  compose:modifier-missing-check: 1
  custom-rule-set-id:no-var: 1
  standard:function-naming: 1

The plugin reports:
Screenshot 2023-12-29 at 12 30 55

@paul-dingemans paul-dingemans changed the title Use .editorconfig custom config for custom ktlint rules Rules from Compose ruleset are not (properly) executed Dec 29, 2023
@paul-dingemans
Copy link
Collaborator

With 0.3.9-SNAPSHOT version of the compose rules, this problem is fixed in mrmans0n/compose-rules#176. No further action needed in ktlint-intellij-plugin.

@paul-dingemans
Copy link
Collaborator

Problem needs another fix in compose rules. See mrmans0n/compose-rules#190

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

No branches or pull requests

2 participants