https://kotlinlang.org logo
Title
k

Kirill Zhukov

05/16/2023, 12:14 AM
I seem to be stuck with adding custom rule set in my KMP project, not sure what I might be missing. So I’ve got a
build-logic
custom Gradle module with various build conventions. In there I have a gradle plugin extension that applies and configures Detekt Gradle plugin (that works with existing rules). Now, I added my own custom rule set, following official guide, but the rule does not seem to be picked up, what am I missing? 1. create custom Detekt rule, say,
MyRule
2. create custom rule set, say,
MyRuleSetProvider
that includes
MyRule
package com.example

class MyRuleSetProvider : RuleSetProvider {
  override val ruleSetId: String = "MyRuleSet"

  override fun instance(config: Config): RuleSet {
    return RuleSet(
      id = ruleSetId,
      rules = listOf(
        MyRule(config)
      )
    )
  }
}
3. define
build-logic/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider
com.example.MyRuleSetProvider
4. define detekt yml config
build-logic/resources/config.yml
MyRuleSet:
  MyRule:
    active: true
I’m testing out to see if the rule gets evaluated by reporting dummy violation - which does not fail so I assume something is off in my configuration where the ruleset doesn’t get picked up
Added absolute config path
detekt {
        autoCorrect = false
        parallel = true
        config = files(
          rootProject.file("detekt/config.yml"), // built-in rules
          file("xyz.../build-logic/src/main/kotlin/resources/config.yml")
        )
        buildUponDefaultConfig = true
      }
now getting error:
Property 'MyRuleSet' is misspelled or does not exist
b

Benoît Liessens

05/16/2023, 4:20 AM
Shouldn’t the path to the service location start with
build-logic/src/main/resources/…
?
k

Kirill Zhukov

05/16/2023, 5:44 PM
oh, I thought it needed to start with a package id
You need a resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider file which has as content the fully qualified name of your RuleSetProvider e.g. io.gitlab.arturbosch.detekt.sample.extensions.SampleProvider.