conda-lock icon indicating copy to clipboard operation
conda-lock copied to clipboard

Support pre-commit

Open maresb opened this issue 3 years ago • 8 comments

It would be nice to make a pre-commit.com-compatible hook so that a Git repository can guarantee that the lockfile is up-to-date.

It would be nice to have a check vs fix feature, depending on whether or not conda-lock should update the working directory.

For speed we should probably default to just checking the hash. But there might be cases where it's desired to relock each time.

maresb avatar Sep 10 '22 11:09 maresb

You can already define this hook yourself:

  - repo: local
    hooks:
      - id: conda-lock
        name: conda-lock
        entry: conda-lock lock -f environment.yml --check-input-hash
        language: python
        additional_dependencies: ["conda-lock==1.0.5"]
        always_run: true
        pass_filenames: false

I use version 1.0.5 because higher versions don't work with CI, as the full path to the environemnt file is stored in the lock file.

Riezebos avatar Sep 21 '22 22:09 Riezebos

Thanks a lot for sharing this example! I had not yet gotten this far.

I do believe that storing the full path is a bug. Paths should be relative to the repo root.

Not sure if it helps in this case but you should be able to force the classical behavior in newer versions with -k explicit.

maresb avatar Sep 22 '22 06:09 maresb

I agree, but it looks like this will be fixed in one of the next versions :) See: https://github.com/conda-incubator/conda-lock/issues/229

Riezebos avatar Sep 22 '22 07:09 Riezebos

Ah yes, thanks for pointing this out. Looks like it's waiting on #204 which is stuck in review. I hope it gets merged soon.

maresb avatar Sep 22 '22 08:09 maresb

I think it would still be good to support pre-commit directly. For example, the entries suggested above would create a local venv containing your local package and conda-lock. This is a little wasteful and would not work if your package has a dependency conflict with conda-lock. The only change would be to add a .pre-commit-hooks.yaml file to the root of the repo. I'd be happy to submit a PR if there is appetite for this.

tomrutter avatar Jul 06 '23 14:07 tomrutter

@tomrutter, that would be excellent!

BTW I believe that the pre-commit venv would not install the project, so there should be no potential for conflicts.

maresb avatar Jul 06 '23 15:07 maresb

You're right! So not such a big deal, but will get on it anyway!

tomrutter avatar Jul 06 '23 16:07 tomrutter