bridgecrewio / yor

Extensible auto-tagger for your IaC files. The ultimate way to link entities in the cloud back to the codified resource which created it.
https://www.yor.io
Apache License 2.0
810 stars 123 forks source link

Yor is writing temp.[0-9]+.tf files instead of changing the source code #281

Closed kayman-mk closed 1 year ago

kayman-mk commented 2 years ago

Describe the bug Executing yor via pre-commit hook results in many temporary files, but none of my Terraform files is changed.

modules/global_export/temp.3293289490.tf
modules/global_keys/temp.1193803556.tf
modules/messaging/sns/temp.3597613450.tf
modules/shared/temp.813126445.tf
temp.82075535.tf

Looks like that yor created one temporary file per Terraform source file. No error message is shown.

Using yor via Docker works as expected.

To Reproduce

repos:
-   repo: https://github.com/bridgecrewio/yor
    rev: 0.1.143
    hooks:
      - id: yor
        name: yor
        entry: yor tag --directory
        args: ["."]
        language: golang
        types: [terraform]
        pass_filenames: false

Install the pre-commit hook and change a Terraform file. After git commit the temporary file appears.

Expected behavior Have the Terraform source code changed (tags added).

Desktop (please complete the following information):

nimrodkor commented 2 years ago

Hey @kayman-mk !

It could be pre-commit "died" in the middle of the process once? After that, the bad tf files mess up the next runs...

kayman-mk commented 2 years ago

Hm, the output says "passed". Are there logs available?

$ git commit -m "testr"
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\kayma\.cache\pre-commit\patch1653467778-2272.
yor......................................................................Passed
[INFO] Restored changes from C:\Users\kayma\.cache\pre-commit\patch1653467778-2272.
[kayma/a 0ee2f54] testr
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 .pre-commit-config.yaml
kayman-mk commented 2 years ago

pre-commit run -v --files main.tf did it.

[WARNING] Unable to get git blame for file main.tf: failed to get blame for latest commit of file main.tf because of error file not found

git blame main.tf works.

Ok, what is the error file?

kayman-mk commented 2 years ago

Tried with 0.1.150. Error is still there. Also did a git blame my-file-name.tf before doing the yor command. git blame works fine.

Note: My pipeline jobs are using the Alpine image. I had to install libc6-compat to get it working. Is there anything else to install?

sr75 commented 1 year ago

This doesn't seem to have anything to do with the pre-commit hook, something broken in yor windows amd64 binary when running on windows 10. Our build process is using linux but the developers desktops are windows and the only place we see this error is when running on windows 10. Be nice if we could get this resolved.

To easily replicate, on any Windows 10 build and using yor version 0.1.151:

Edition Windows 10 Enterprise Version 20H2 Installed on ‎3/‎13/‎2022 OS build 19042.1889 Experience Windows Feature Experience Pack 120.2212.4180.0

Just create a simple module with main.tf in it,

Add something as simple as:

resource "aws_security_group" "default" {
    name = "test"
}

Then do the git init, git add. git commit, etc to setup for yor.

Run yor:

yor tag -d .

Error:

2022/10/31 11:27:23 [WARNING] Failed writing tags to file main.tf, because remove .\temp.874456116.tf: The process cannot access the file because it is being used by another process.

Run it again, and It just keeps creating temp.NNNNNNNNNN.tf files over and over.

git pull the same repo down to a linux machine using the linux amd64 binary and it works fine.

JamesWoolfenden commented 1 year ago

I found and fixed this in https://github.com/bridgecrewio/yor/pull/321 #321