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

Remaining color codes in shell when running tofu fmt #955

Open
stephanrenggli opened this issue Feb 18, 2025 · 0 comments
Open

Remaining color codes in shell when running tofu fmt #955

stephanrenggli opened this issue Feb 18, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@stephanrenggli
Copy link

🔧 Summary

It appears that after running tofu fmt in lefthook there are remaining color codes in the shell. So this might not be a lefthook issue but a tofu issue.

When running tofu fmt filename.tf this does not happen however, so my guess would be this has something to do with the colored output of lefthook.
It is also worth noting that tofu fmt does not output anything to the shell if no changes were made/all files were already correctly formatted. If tofu fmt changes a file because it was not formatted correctly the file name of the changed file is written to the terminal.
This does not affect the result though, the color code is "printed" wether tofu fmt changes files or not.

I have tested this on sh, bash and zsh (with prezto + starship) and have gotten the same results. The tests described below and the logs in the end were with bash.

Lefthook version

1.10.3 24a3aff (also tested on latest version 1.10.10, same results)

Steps to reproduce

  1. Install lefthook
  2. Add lefthook configuration
  3. Make sure lefthook runs with colors enabled
  4. Add the following lefthook config:

.lefthook.yaml

---
pre-commit:
  parallel: true
  commands:
    tofu-fmt:
      glob: "*.{tf,tofu}"
      run: tofu fmt {staged_files}
  1. Stage a .tf file
  2. Run lefthook run pre-commit

Expected results

There are no color codes remaining in the shell after the command completes.

Actual results

Color codes remain in the shell, i.e. 11;rgb:ffff/fafa/f3f3;1R^C

Possible Solution

Have not found one yet.

  • I tried running lefthook with --colors off though this did not help:
    Image
  • I tried running tofu fmt -no-color {staged_files} instead in the .lefthook.yaml (tofu fmt does not have a -no-color parameter documented so I don't think this does anything, the parameter exists for other tofu functions though)
  • I tried running a combination of the above
    All these resulted in the same result: there are remaining color codes in the prompt.

When running a different command (such as echo or tflint) instead of tofu fmt this issue did not occur.

Logs / Screenshots

Image

LEFTHOOK_VERBOSE=true git ...
root@2560b03b9115:/workspaces/terraform-template# lefthook run pre-commit
│ [lefthook] cmd:    [git version]
│ [lefthook] stdout: git version 2.39.5
                                     
│ [lefthook] cmd:    [git rev-parse --path-format=absolute --show-toplevel]
│ [lefthook] stdout: /workspaces/terraform-template
                                                 
│ [lefthook] cmd:    [git rev-parse --path-format=absolute --git-path hooks]
│ [lefthook] stdout: /workspaces/terraform-template/.git/hooks
                                                            
│ [lefthook] cmd:    [git rev-parse --path-format=absolute --git-path info]
│ [lefthook] stdout: /workspaces/terraform-template/.git/info
                                                           
│ [lefthook] cmd:    [git rev-parse --path-format=absolute --git-dir]
│ [lefthook] stdout: /workspaces/terraform-template/.git
                                                      
│ [lefthook] cmd:    [git hash-object -t tree /dev/null]
│ [lefthook] stdout: 4b825dc642cb6eb9a060e54bf8d69288fbee4904
                                                           
╭───────────────────────────────────────╮
│ 🥊 lefthook v1.10.3  hook: pre-commit │
╰───────────────────────────────────────╯
sync hooks: ✔️ (pre-commit)
│ [lefthook] cmd:    [git status --short --porcelain]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout:  M .lefthook.yaml
MM variables.tf                     
                                    
│ [lefthook] saving partially staged files
│ [lefthook] cmd:    [git diff --binary --unified=0 --no-color --no-ext-diff --src-prefix=a/ --dst-prefix=b/ --patch --submodule=short --output /workspaces/terraform-template/.git/info/lefthook-unstaged.patch -- variables.tf]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: 
│ [lefthook] cmd:    [git stash create]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: 7a19a1ce6d08ec81b32759df59c2a2558cefa319
                                                           
│ [lefthook] cmd:    [git stash store --quiet --message lefthook auto backup 7a19a1ce6d08ec81b32759df59c2a2558cefa319]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: 
│ [lefthook] cmd:    [git checkout --force -- variables.tf]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: 
│ [lefthook] hide partially staged files: [variables.tf]
                                                      
│ [lefthook] cmd:    [git diff --name-only --cached --diff-filter=ACMR]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: variables.tf
                               
│ [lefthook] files before filters:
[variables.tf]                  
│ [lefthook] files after filters:
[variables.tf]                 
│ [lefthook] files after escaping:
[variables.tf]                  
│ [lefthook] executing: tofu fmt variables.tf
┃  tofu-fmt ❯ 


│ [lefthook] cmd:    [git apply -v --whitespace=nowarn --recount --unidiff-zero /workspaces/terraform-template/.git/info/lefthook-unstaged.patch]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: 
│ [lefthook] stderr: Checking patch variables.tf...
Applied patch variables.tf cleanly.              
                                                 
│ [lefthook] cmd:    [git stash list]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: stash@{0}: lefthook auto backup
                                                  
│ [lefthook] cmd:    [git stash drop --quiet stash@{0}]
│ [lefthook] dir:    /workspaces/terraform-template
│ [lefthook] stdout: 
                                      
  ────────────────────────────────────
summary: (done in 5.20 seconds)       
✔️ tofu-fmt
root@2560b03b9115:/workspaces/terraform-template# 11;rgb:ffff/fafa/f3f3;1R^C
root@2560b03b9115:/workspaces/terraform-template# ^C
root@2560b03b9115:/workspaces/terraform-template#
@stephanrenggli stephanrenggli added the bug Something isn't working label Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant