1

I'm using pre-commit 2.8.2 and trying to get the new coursier language to work.

I'm able to run the coursier command successfully from CLI.

❯ cs launch io.gitlab.arturbosch.detekt:detekt-cli:1.14.2 -M io.gitlab.arturbosch.detekt.cli.Main -r https://kotlin.bintray.com/kotlinx -- --help
Usage: detekt [options]
  Options:
    --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

However, I'm not clear how to declare a hook for coursier based on the documentation. Is there an example using coursier to review? This is what I have in .pre-commit-hooks.yaml.

---
- id: detekt
  name: detekt
  description: "Runs the Detekt static code analyzer."
  language: coursier
  entry: launch io.gitlab.arturbosch.detekt:detekt-cli:1.14.2 -M io.gitlab.arturbosch.detekt.cli.Main -r https://kotlin.bintray.com/kotlinx
  files: \.kt$
  require_serial: true

When I try to use the hook I get this error:

❯ pre-commit try-repo /Users/dustin/workspaces/pre-commit/pre-commit-jvm detekt --verbose --all-files
[WARNING] Creating temporary repo with uncommitted changes...
===============================================================================
Using config:
===============================================================================
repos:
-   repo: /var/folders/x7/18ntjgfd2j5gy1g1xlzby15w0000gq/T/tmpl5i4qu5r/shadow-repo
    rev: 233b290c180e6f6639f14689505ce5098c1c27ad
    hooks:
    -   id: detekt
===============================================================================
[INFO] Initializing environment for /var/folders/x7/18ntjgfd2j5gy1g1xlzby15w0000gq/T/tmpl5i4qu5r/shadow-repo.
[INFO] Installing environment for /var/folders/x7/18ntjgfd2j5gy1g1xlzby15w0000gq/T/tmpl5i4qu5r/shadow-repo.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/x7/18ntjgfd2j5gy1g1xlzby15w0000gq/T/tmpl5i4qu5r/repo3f0zmsce/.pre-commit-channel'
Check the log at /Users/dustin/.cache/pre-commit/pre-commit.log
Dustin Shimono
  • 1,031
  • 7
  • 10
  • sorry, the docs aren't up to date here yet -- probably best to consult the implementation until the implementer can get around to documenting it: https://github.com/pre-commit/pre-commit/pull/1633 -- when the docs are up to date the answer should live here: https://pre-commit.com/#coursier (but they're just blank for now) – anthony sottile Nov 04 '20 at 00:12
  • Found answer to my own question by reviewing the Coursier documentation and reviewing the pre-commit PR as suggested by https://stackoverflow.com/users/812183/anthony-sottile – Dustin Shimono Nov 04 '20 at 22:26
  • you should post that as an answer (even self-answers are good on stackoverflow!) – anthony sottile Nov 05 '20 at 00:01

1 Answers1

1

Found solution:

  1. Coursier language requires a .pre-commit-channel/detekt.json file to be created too.
    {
  "mainClass" : "io.gitlab.arturbosch.detekt.cli.Main",
  "repositories": [
    "central",
    "https://kotlin.bintray.com/kotlinx"
  ],
  "dependencies": [
    "io.gitlab.arturbosch.detekt:detekt-cli:1.14.2"
  ]
}
  1. Lastly, updated .pre-commit-hooks.yaml to
---
- id: detekt
  name: detekt
  description: "Runs the Detekt static code analyzer."
  language: coursier
  entry: detekt
  args: ["--config", "detekt-config.yml"]
  pass_filenames: false
  files: \.kt$
  require_serial: true

Intentionally caused a violation and the hook caught it.

❯ pre-commit try-repo /Users/dustin/workspaces/pre-commit/pre-commit-jvm detekt --all-files
[WARNING] Creating temporary repo with uncommitted changes...
===============================================================================
Using config:
===============================================================================
repos:
-   repo: /var/folders/x7/18ntjgfd2j5gy1g1xlzby15w0000gq/T/tmp23ad2fe4/shadow-repo
    rev: 452f70214d1960a82bff4c11523ecb36d5f6f333
    hooks:
    -   id: detekt
===============================================================================
[INFO] Initializing environment for /var/folders/x7/18ntjgfd2j5gy1g1xlzby15w0000gq/T/tmp23ad2fe4/shadow-repo.
[INFO] Installing environment for /var/folders/x7/18ntjgfd2j5gy1g1xlzby15w0000gq/T/tmp23ad2fe4/shadow-repo.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
detekt...................................................................Failed
- hook id: detekt
- exit code: 2

/Users/dustin/workspaces/spring-boot/custom-starters/txn-app-event-starter/src/main/java/com/dustinsand/txn/app/event/TransactionalApplicationEventAutoConfiguration.kt - 5min debt
        WildcardImport - [/Users/dustin/workspaces/spring-boot/custom-starters/txn-app-event-starter/src/main/java/com/dustinsand/txn/app/event/TransactionalApplicationEventAutoConfiguration.kt] at /Users/dustin/workspaces/spring-boot/custom-starters/txn-app-event-starter/src/main/java/com/dustinsand/txn/app/event/TransactionalApplicationEventAutoConfiguration.kt:7:1

Overall debt: 5min

Build failed with 1 weighted issues.
Dustin Shimono
  • 1,031
  • 7
  • 10