Skip to content

Be able to run pre-commit check on file removal (for languages which not file-limited, but dir-limited) #3048

Description

@MaxymVlasov

search you tried in the issue tracker

file removal

describe your actual problem

I'm maintainer of Terraform pre-commit hooks, and Terraform during checks and execution, merges all *.tf, *.tfvars, and few other file extensions in one folder in one big configuration and checks that across all these files everything is OK.

For example, we have 2 files in one directory:

vars.tf

variable "kms_enabled" {
  description = "Enable creation of app-specific KMS key. Required if S3 or RDS is enabled."
  type        = bool
  default     = false
}

outputs.tf

output "kms_enabled" {
  description = "Is KMS used"
  value       = var.kms_enabled
}

If everything that I do in the folder - removes vars.tf - pre-commit will skip any checks which means that we commit invalid code.

In terraform hooks, we get all the changed file paths, extract dir paths and execute hooks on dirs.
https://github.com/antonbabenko/pre-commit-terraform/blob/7122298824a9530e8f9c48e5de4281cca6301d67/hooks/_common.sh#L245C27-L253

Is there any possibility of forcing pre-commit to not skip deleted file paths from the list of files that need to be checked?

pre-commit --version

pre-commit 3.2.1

Activity

  1. asottile commented on Nov 1, 2023

    @asottile
    Member

    pre-commit only supports files and files that exist -- you can of course write your own tool which handles the file filtering itself to support directories and/or deletions but this is intentionally not supported by the framework

    note also that the git hook parts are not supposed to be a gating mechanism because everything is optional. if you need a gating mechanism the only safe thing to do (always) is pre-commit run --all-files in CI -- such as through https://pre-commit.ci

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions