rrrene / credo

A static code analysis tool for the Elixir language with a focus on code consistency and teaching.
http://credo-ci.org/
MIT License
4.94k stars 419 forks source link

Question: Is it possible to define a .credo.exs in a dependency with custom checks and apply it when running mix credo in a project #1083

Closed jsolana closed 11 months ago

jsolana commented 1 year ago

Hi mates! I have a question related about if is possible to define an internal .credo.exs in a dependency library with custom checks, eg: my_credo_checks, add this dependency to a project, eg: my_project and when run mix credo run the default checks + custom checks defined in my_credo_checks.

The idea is to avoid (if is possible) create a .credo.exs file in every project defining the checks as is described here.

Thanks a lot!!

Environment

Credo version (mix credo -v):

Version 1.7.1

Erlang/Elixir version (elixir -v):

Erlang/OTP 25 [erts-13.0.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Elixir 1.14.2 (compiled with Erlang/OTP 23)

Operating system (sw_vers):

ProductName: macOS ProductVersion: 12.5.1 BuildVersion: 21G83

Expected outcome

Have a my_custom_checks project with a .credo.exs file with all checks configured as is described here. Add this dependency to a project, my_project and when run mix credo run all default checks + custom checks defined in my_custom_check.

Actual outcome

Running mix credo in my_project only run default checks. I need to add a .credo.exs config file with the custom checks to run them when run mix credo

rrrene commented 1 year ago

You can do that by making my_custom_checks a Credo plugin (which has to be enabled in your config).

That plugin then has to add a default config via Credo.Plugin.register_default_config/2.

The config you're adding has to add your checks using the :extra key (the last part is important, see here and scroll down to :extra).

Again: The plugin has to be enabled via a project config or a global one.

jsolana commented 1 year ago

Hi! I tried a different approach in this example

I defined a lib (my_credo) with checks and a .credo.exs file.

Then in a random project if I want to apply those checks, I can run

mix credo --config-name my-rules --config-file ./deps/my_credo/config/.credo.exs

To avoid this command, I can define aliases to run this easily.

Wdyt?

It is better with the plugin approach? What I want to do is to use this new checks to be executed in CI/CD pipelines too, and the most seamlessly and less manual work to be done integration the best :)

Thanks a lot!!

rrrene commented 11 months ago

@jsolana your approach is fine and pragmatic. It might even be more transparent for someone new to a project 👍