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

project: pre-commit hooks #2834

Closed
gauthsvenkat opened this issue Feb 16, 2024 · 8 comments
Closed

project: pre-commit hooks #2834

gauthsvenkat opened this issue Feb 16, 2024 · 8 comments
Labels
FeatureRequest New feature or request

Comments

@gauthsvenkat
Copy link

Is your feature request related to a problem? Please describe.

I would love to use cue to validate some yamls everytime I make a commit by the use of pre-commit hooks. At the moment, it is possible to run cue commands from a pre-commit hook but that requires cue to be already installed outside the pre-commit config.

Describe the solution you'd like
It would be lovely if the cue repo could offer pre-commit hooks and we can manage everything by just having a cue entry in the pre-commit config.

Describe alternatives you've considered
One option is to call cue commands by setting up a local script that pre-commit has to run. The disadvantage is that cue would need to already be installed on the machine pre-commit runs. It would be hard to adopt since that would require other repository stakeholders to install an additional dependency manually.

@gauthsvenkat gauthsvenkat added FeatureRequest New feature or request Triage Requires triage/attention labels Feb 16, 2024
@infogulch
Copy link

infogulch commented Feb 16, 2024

I've never seen this pre-commit project before, though I'm vaguely familiar with the concept. It seems a bit niche. The main benefit appears to be that it installs and manages hook dependencies centrally. From the home page:

It is a multi-language package manager for pre-commit hooks. You specify a list of hooks you want and pre-commit manages the installation and execution of any hook written in any language before every commit. pre-commit is specifically designed to not require root access. If one of your developers doesn’t have node installed but modifies a JavaScript file, pre-commit automatically handles downloading and building node to run eslint without root.

It seems kinda like a CI dependency manager and runner that you can also use locally. Is this really worth the trouble?


As far as implementation (here are some resources: Creating new hooks, golang), it appears that would involve adding a .pre-commit-hooks.yaml in the root of the cue repo. It might look something like this:

-   id: vet
    name: cue vet
    description: Validate CUE and other data files.
    language: golang
    language_version: "1.22" // ?

    // since it installs with `go install ./...` I think the cue cli binary would be named `cmd`, so invoke it with that name
    // not sure how to override the installer command to make it be named `cue`
    entry: cmd

Could you do this without modifying the cue repo at all and creating a new repo instead? Unclear.

And then in the repo where you want to use it, you would have something like:

repos:
-   repo: https://github.com/cue-lang/cue
    rev: v0.7.1
    hooks:
    -   id: vet
        args: ["args to pass to cue vet"]

@gauthsvenkat
Copy link
Author

I've never seen this pre-commit project before

I'm surprised, maybe it is huge in the Python community cause we use it fairly extensively at work and there are tons of Python-related projects that provide pre-commit hooks.

Is this really worth the trouble?

I would argue in favor of it, yeah. It is pretty unintrusive to the core codebase and enables adoption. The validation aspect of cue would be nice to have in a pre-commit hook.

Could you do this without modifying the cue repo at all and creating a new repo instead?

Not sure what you mean here. I guess there would need to be some extra files (at least a .pre-commit-hooks.yaml). But I don't think any existing files would have to be changed.

@gauthsvenkat
Copy link
Author

I made a small POC which you can check out in my fork of cue - https://github.com/gauthsvenkat/cue/blob/master/.pre-commit-hooks.yaml.

It turns out all that was needed was just a simple pre-commit-hooks.yaml.

@myitcv
Copy link
Member

myitcv commented Mar 12, 2024

Hi @gauthsvenkat - one thing I'm not clear on is why changes are required to the CUE project itself. Please can you help to explain that point?

If people want to use CUE in their pre-commit hooks in their projects, that's great.

Just not clear how/why we need to make a change in the project itself to support that.

@myitcv myitcv removed the Triage Requires triage/attention label Mar 12, 2024
@myitcv myitcv changed the title pre-commit hooks project: pre-commit hooks Mar 12, 2024
@gauthsvenkat
Copy link
Author

Hey @myitcv !

The way pre-commit works is that the project offering pre-commit hook(s) needs to have a .pre-commit-hooks.yaml file at the root of the repository, that describes the hooks that are offered. In this case, the most convenient way to achieve that is to have this yaml file in this repository (example - [hadolint](https://github.com/hadolint/hadolint/blob/master/.pre-commit-hooks.

The other option is to maintain a separate repository for the pre-commit-hook(s) (example - ruff). The question is would this repo live in the cue-lang organization? (I think it should).

I think both these options are fairly decent approaches and ultimately it boils down to preference.

What would be your advice?

@mvdan
Copy link
Member

mvdan commented May 14, 2024

My 2c would be that adding a "dot file" at the root of the main CUE repository feels like unnecessary noise. Note that every extra file in the root directory pushes the README further down the page, and is one of the first files anyone sees when looking at the source.

The Go module and ./cmd/cue package are very standard, to the point that one can run a specific version of CUE very easily:

$ go run cuelang.org/go/cmd/cue@v0.9.0-alpha.4 version
cue version v0.9.0-alpha.4

So my impression is that we should not be required to insert a root-level "dot file" to allow pre-commit to do the right thing. Anyone should be able to tell pre-commit to e.g. "run the Go tool at cuelang.org/go/cmd/cue@v0.9.0-alpha.4 with arguments "vet ./..."` and that shouldn't need us to maintain an extra YAML file.

@riclima
Copy link

riclima commented May 14, 2024

@gauthsvenkat unless I'm missing something in your use case, you don't need to have it pre-installed outside of the pre-commit config. For example:

# .pre-commit-config.yaml
repos:
  - repo: local
    hooks:
      - id: cue-fmt
        name: Format CUE files
        entry: cue
        args: [ fmt ]
        types: [ cue ]
        language: golang
        additional_dependencies:
          - cuelang.org/go/cmd/cue@v0.8.2

@gauthsvenkat
Copy link
Author

I agree with @mvdan's point and the following response by @riclima pretty much hits the nail. I checked it out and it works well.

Based on this discussion, I'm closing this issue (+ related pull request).

Thanks for your input guys! Especially @riclima for pointing out I could do this in a much better way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants