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

How does one configure Detekt? #3579

Closed
michaeloa opened this issue May 24, 2024 · 14 comments · Fixed by #3590
Closed

How does one configure Detekt? #3579

michaeloa opened this issue May 24, 2024 · 14 comments · Fixed by #3590
Labels
question Further information is requested

Comments

@michaeloa
Copy link

I've tried several different variants of rules config.
.detekt-config.yml
detekt-config.yml
detekt.yaml
detekt.yml
default-detekt-config.yml

None of them seem to be working. How do I get it to use a custom rules config?

I find this a general issue with the documentation, btw. Lots of variable documentation, but I've yet to see any documentation about how to add rules config for various linters easily. The documentation of the individual linters is often not clear enough on this.

@michaeloa michaeloa added the question Further information is requested label May 24, 2024
@nvuillam
Copy link
Member

nvuillam commented May 27, 2024

Hi @michaeloa , MegaLinter embeds linters and links to their documentation, but it's not our responsibility to document the linters themselves :)

About detekt config, the doc is here -> https://detekt.dev/docs/introduction/configurations

It does not seem detekt CLI looks for a config file name

Please can you try with the following arguments in your .mega-linter.yml , and define a .detekt.yml at the root of your repo ?

KOTLIN_DETEKT_ARGUMENTS: ["--config",".detekt.yml"]

If you don't want to totally overwrite detekt config, you might use the following

KOTLIN_DETEKT_ARGUMENTS: ["--build-upon-default-config","--config",".detekt.yml"]

@michaeloa
Copy link
Author

I've used detekt in a "super" linter like implementation before, and it does indeed not require a configuration file to run, nor look for a default.

So I actually did try to pass kotlin arguments previously, but all that leads to is this:

❌ Linted [KOTLIN] files with [detekt]: Found 2 error(s) - (0.53s)
- Using [detekt v1.23.5] https://megalinter.io/7.11.1/descriptors/kotlin_detekt
- MegaLinter key: [KOTLIN_DETEKT]
- Rules config: identified by [detekt]
- Number of files analyzed: [2]
[detekt] src/Test.kt - ERROR - 1 error(s)
--Error detail:
Was passed main parameter 'src/Test.kt' but no main parameter was defined in your arg class
Usage: detekt [options]
  Options:
    --all-rules
      Activates all available (even unstable) rules.
      Default: false
    --auto-correct, -ac
      Allow rules to auto correct code if they support it. The default rule
      sets do NOT support auto correcting and won't change any line in the
      users code base. However custom rules can be written to support auto
      correcting. The additional 'formatting' rule set, added with
      '--plugins', does support it and needs this flag.
      Default: false
    --base-path, -bp
      Specifies a directory as the base path.Currently it impacts all file
      paths in the formatted reports. File paths in console output and txt
      report are not affected and remain as absolute paths.
    --baseline, -b
      If a baseline xml file is passed in, only new code smells not in the
      baseline are printed in the console.
    --build-upon-default-config
      Preconfigures detekt with a bunch of rules and some opinionated defaults
      for you. Allows additional provided configurations to override the
      defaults.
      Default: false
    --classpath, -cp
      EXPERIMENTAL: Paths where to find user class files and depending jar
      files. Used for type resolution.
    --config, -c
      Path to the config file (path/to/config.yml). Multiple configuration
      files can be specified with ',' or ';' as separator.
    --config-resource, -cr
      Path to the config resource on detekt's classpath (path/to/config.yml).
    --create-baseline, -cb
      Treats current analysis findings as a smell baseline for future detekt
      runs.
      Default: false
    --debug
      Prints extra information about configurations and extensions.
      Default: false
    --disable-default-rulesets, -dd
      Disables default rule sets.
      Default: false
    --excludes, -ex
      Globbing patterns describing paths to exclude from the analysis.
    --generate-config, -gc
      Export default config. Path can be specified with --config option
      (default path: default-detekt-config.yml)
      Default: false
    --help, -h
      Shows the usage.
    --includes, -in
      Globbing patterns describing paths to include in the analysis. Useful in
      combination with 'excludes' patterns.
    --input, -i
      Input paths to analyze. Multiple paths are separated by comma. If not
      specified the current working directory is used.
    --jdk-home
      EXPERIMENTAL: Use a custom JDK home directory to include into the
      classpath
    --jvm-target
      EXPERIMENTAL: Target version of the generated JVM bytecode that was
      generated during compilation and is now being used for type resolution
      (1.8, 9, 10, ..., 20)
      Default: 1.8
    --language-version
      EXPERIMENTAL: Compatibility mode for Kotlin language version X.Y,
      reports errors for all language features that came out later
      Possible Values: [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1]
    --max-issues
      Return exit code 0 only when found issues count does not exceed
      specified issues count.
    --parallel
      Enables parallel compilation and analysis of source files. Do some
      benchmarks first before enabling this flag. Heuristics show performance
      benefits starting from 2000 lines of Kotlin code.
      Default: false
    --plugins, -p
      Extra paths to plugin jars separated by ',' or ';'.
    --report, -r
      Generates a report for given 'report-id' and stores it on given 'path'.
      Entry should consist of: [report-id:path]. Available 'report-id' values:
      'txt', 'xml', 'html', 'md', 'sarif'. These can also be used in
      combination with each other e.g. '-r txt:reports/detekt.txt -r
      xml:reports/detekt.xml'
    --version
      Prints the detekt CLI version.
      Default: false

Looks like something is not correct in the way the arguments are passed to Detekt, perhaps?

@nvuillam
Copy link
Member

@michaeloa please can you run with LOG_LEVEL: DEBUG so we can see the exact command line built by MegaLinter ?

@michaeloa
Copy link
Author

The debug returns the following:

[detekt] command: ['detekt-cli', '-i', '--config', '.detekt.yml', 'src/Test.kt']

That would seem to explain the issue (which would appear to be a bug). The file to be analyzed should be passed to the -i parameter. This should be something along the lines of ['detekt-cli', '--config', '.detekt.yml', '-i', 'src/Test.kt'] instead.

@nvuillam
Copy link
Member

@michaeloa ok got it, i know how to solve that bug

But it seems that deteckt is called once by file, that seems not optimized at all, do you know if it can take a list of files, which would be more performant ?

@michaeloa
Copy link
Author

Let me check. I knew this once, but it's been a while.

@michaeloa
Copy link
Author

AFAIK, you do not need -i to run detekt; it should default to scan the working directory.

Alternatively, you should also be able to input a path to detekt.

@nvuillam
Copy link
Member

We can run detekt with project mode (on repo root without -i ), and in case someone wants something special he/she can configure extra parameters

What do you think about this proposition ?

nvuillam added a commit that referenced this issue May 28, 2024
…3590)

* Deetekt: add -i as last argument before file name

Fixes #3579

* Upgrade Kotlin and make it with cli_lint_mode = project

* test folder

* changelog
@nvuillam
Copy link
Member

@michaeloa please can you confirm with newest beta version that now it's ok for your use case ? :)

@michaeloa
Copy link
Author

Seems like a reasonable approach. It is a bit of an inconvenience if one wants to fetch the detekt.yml from a URL (as we currently do with the other linters), but I assume I can use the pre/post hook commands to download a remote config file, if needed.

Going to be away from my work PC for the rest of the week, but I did a quick test with a local detekt.yml and -r beta, and that works now, so the immediate issue seems solved. Many thanks.

@nvuillam
Copy link
Member

thanks for your feedback :)

@enciyo
Copy link
Contributor

enciyo commented Jul 8, 2024

Hi,

I want to run detekt for git diff. Is this possible with the latest changes?

@enciyo
Copy link
Contributor

enciyo commented Jul 9, 2024

For now I have overcome this by overriding the following value on the CI/CD side. But that doesn't seem like good behavior for us:

KOTLIN_DETEKT_ARGUMENTS: "--config config.yml --report sarif:megalinter-reports/detekt-result.sarif --report html:megalinter-reports/detekt-result.html --report md:megalinter-reports/detekt-result.md -i ${{ steps.diff.outputs.diff }}"

@michaeloa
Copy link
Author

@enciyo Interesting question - haven't looked into that yet (though it'll surely become relevant once we work more on using megalinter).

Might make sense to open a new issue, since this one is resolved and closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants