Skip to content

Commit 0ec819b

Browse files
authored
Merge pull request #1044 from GitGuardian/agateau/fix-windows-encoding
fix: force utf-8 to fix visual studio failure to print error messages
2 parents dcd87b4 + 5abbd8b commit 0ec819b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Fixed
2+
3+
- Fix Visual Studio not being able to show error messages from ggshield pre-commit (#170).

ggshield/__main__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import multiprocessing
44
import os
55
import sys
6+
from io import TextIOWrapper
67
from pathlib import Path
78
from typing import Any, List, Optional
89

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

177178

179+
def force_utf8_output():
180+
"""
181+
Force stdout and stderr to always be UTF-8. This is not the case on Windows
182+
when stdout or stderr is not the console. Doing this fixes integration with
183+
Visual Studio (see #170).
184+
"""
185+
for out in sys.stdout, sys.stderr:
186+
# pyright is not sure sys.stdout and stderr are TextIOWrapper, so it complains when
187+
# calling `reconfigure()` on them, unless this check is there.
188+
assert isinstance(out, TextIOWrapper)
189+
out.reconfigure(encoding="utf-8")
190+
191+
178192
def main(args: Optional[List[str]] = None) -> Any:
179193
"""
180194
Wrapper around cli.main() to handle the GITGUARDIAN_CRASH_LOG variable.
@@ -190,6 +204,8 @@ def main(args: Optional[List[str]] = None) -> Any:
190204
if sys.stderr.isatty():
191205
ui.set_ui(RichGGShieldUI())
192206

207+
force_utf8_output()
208+
193209
show_crash_log = getenv_bool("GITGUARDIAN_CRASH_LOG")
194210
return cli.main(args, prog_name="ggshield", standalone_mode=not show_crash_log)
195211

0 commit comments

Comments
 (0)