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

Run custom rules on Android #2285

Closed
robertpicsart opened this issue Jan 23, 2020 · 9 comments
Closed

Run custom rules on Android #2285

robertpicsart opened this issue Jan 23, 2020 · 9 comments
Labels

Comments

@robertpicsart
Copy link

robertpicsart commented Jan 23, 2020

Hi. I am trying to run my custom rules on Android project, but it seems not working.

Here is my project build.gradle file

buildscript {
    ext.kotlin_version = '1.3.61'
    repositories {
        mavenCentral()
        jcenter()
        google()
        maven { url "https://plugins.gradle.org/m2/" }
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.4.0"
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
        google()
        maven { url "https://plugins.gradle.org/m2/" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Here is the app level build.gradle file

...
dependencies {
    ...
    implementation "io.gitlab.arturbosch.detekt:detekt-api:1.4.0"
}

apply plugin: 'io.gitlab.arturbosch.detekt'

detekt {
    toolVersion = "1.4.0"
//    buildUponDefaultConfig = true
    input = files("src/main/java/demo/detekt")
    config = files("$rootDir/custom-config.yml")
    reports {
        xml { enabled = false }
        html {
            enabled = true
            destination = file("build/reports/detekt.html")
        }
        txt { enabled = false }
    }
//    ruleSets = "$rootDir/app/libs/detekt-rules-1.4.0.jar"
}

dependencies {
    detekt "io.gitlab.arturbosch.detekt:detekt-api:1.4.0"
    detekt "io.gitlab.arturbosch.detekt:detekt-cli:1.4.0"
    detekt "io.gitlab.arturbosch.detekt:detekt-rules:1.4.0"
    detektPlugins(project(path: ':detekt-rules-1.4.0'))
}

My custom config is:

config:
  validation: true
  # 1. exclude rule set 'sample' and all its nested members
  # 2. exclude every property in every rule under the rule set 'sample'
  excludes: "test.*,test>.*>.*"
test:
  TooManyFunctions:
    active: false

My custom rule provider is:

class CustomRulesProvider : RuleSetProvider {
    override val ruleSetId: String = "test"

    override fun instance(config: Config): RuleSet {
        return RuleSet(
            ruleSetId, listOf(
                TooManyFunctions()
            )
        )
    }
}

TooManyFunctions() I took from your example.

@cortinico
Copy link
Member

Is your project on Github? Can you share it so we can check the whole configuration?

@robertpicsart
Copy link
Author

robertpicsart commented Jan 24, 2020

Unfortunately It is not on Github

@cortinico
Copy link
Member

but it seems not working.

Can you provide more context?
What does it mean that is not working? Is it just ignored or does it print a stack trace on the terminal?
Have you copied over the tests from the detekt-sample-extensions project? Are they running fine from the terminal?

@robertpicsart
Copy link
Author

I've created a custom config file. When I'm adding a detekt rule (like empty constructor), it works, but with my custom rule it is not able to find a problem

@arturbosch
Copy link
Member

Please take a look at how mozilla configured their custom detekt rules inside an Android project: https://github.com/mozilla-mobile/fenix/blob/master/build.gradle

@SergJ4
Copy link

SergJ4 commented Feb 7, 2020

Hi! I have similar issue. In my case when I try to add my custom ruleset block inside config.yml, detekt fails with message:

> Task :detekt FAILED
Property 'customrules' is misspelled or does not exist.
io.gitlab.arturbosch.detekt.cli.InvalidConfig: Run failed with 1 invalid config property.

I used mozilla example and it does not works in my project. My detekt version was 1.4.0. After several hours I tried to downgrade detekt version like in mozilla repo (1.0.0-RC16) and it works!! No other changes was made. I suppose something is broken in new detekt versions. Or maybe there are undocumented changes for detekt custom rules integration.

I've tried several different detekt versions:

1.0.0-RC16 - works
1.1.0 - works
1.1.1 - works
1.2.0 - does not work
1.2.1 - does not work
1.4.0 - does not work
1.5.1 - does not work

@arturbosch
Copy link
Member

@SergJ4
Please take a look at the last paragraph of https://arturbosch.github.io/detekt/extensions.html#custom-rulesets.

The config validation was introduced to "protect" users from misspelling property names.
Having the need to manually add some excludes for custom rule sets is indeed inconvenient.

We could automatically add a generic [custom_ruleset]>.*...

@SergJ4
Copy link

SergJ4 commented Feb 17, 2020

@arturbosch, thanks a lot! It works now. I saw this paragpraph, but I misunderstood it. I thought that if it uses exclude keyword, then it should exclude something from config, not include. But anyway, it's my fault. I will read more carefully in the future.

arturbosch added a commit that referenced this issue Feb 21, 2020
* Include default config validation excludes for plugin rule sets - #2285

* Allow to validate configuration by a new ConfigValidator extension - #2285
@schalkms
Copy link
Member

schalkms commented Mar 5, 2020

Closed with d0d41cf and 94b89b3

@schalkms schalkms closed this as completed Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants