Skip to content

Commit

Permalink
Merge pull request #1044 from GitGuardian/agateau/fix-windows-encoding
Browse files Browse the repository at this point in the history
fix: force utf-8 to fix visual studio failure to print error messages
  • Loading branch information
salome-voltz authored Jan 8, 2025
2 parents dcd87b4 + 5abbd8b commit 0ec819b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Fix Visual Studio not being able to show error messages from ggshield pre-commit (#170).
16 changes: 16 additions & 0 deletions ggshield/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import multiprocessing
import os
import sys
from io import TextIOWrapper
from pathlib import Path
from typing import Any, List, Optional

Expand Down Expand Up @@ -175,6 +176,19 @@ def before_exit(ctx: click.Context, exit_code: int, *args: Any, **kwargs: Any) -
sys.exit(exit_code)


def force_utf8_output():
"""
Force stdout and stderr to always be UTF-8. This is not the case on Windows
when stdout or stderr is not the console. Doing this fixes integration with
Visual Studio (see #170).
"""
for out in sys.stdout, sys.stderr:
# pyright is not sure sys.stdout and stderr are TextIOWrapper, so it complains when
# calling `reconfigure()` on them, unless this check is there.
assert isinstance(out, TextIOWrapper)
out.reconfigure(encoding="utf-8")


def main(args: Optional[List[str]] = None) -> Any:
"""
Wrapper around cli.main() to handle the GITGUARDIAN_CRASH_LOG variable.
Expand All @@ -190,6 +204,8 @@ def main(args: Optional[List[str]] = None) -> Any:
if sys.stderr.isatty():
ui.set_ui(RichGGShieldUI())

force_utf8_output()

show_crash_log = getenv_bool("GITGUARDIAN_CRASH_LOG")
return cli.main(args, prog_name="ggshield", standalone_mode=not show_crash_log)

Expand Down

0 comments on commit 0ec819b

Please sign in to comment.