From 1120e972fa4b64515e99c09ad68c8b302386642f Mon Sep 17 00:00:00 2001 From: Mark Yen Date: Tue, 4 Feb 2025 11:30:55 -0800 Subject: [PATCH] WSL-helper: Use UTF8 when interrogating WSL Turns out WSL has supported using UTF-8 (instead of UTF-16) for output if we set an environment variable. Do so to avoid having to do the conversion ourselves; as a bonus, this also means if people do have it set we'll parse the output correctly. Signed-off-by: Mark Yen --- .../wsl-helper/pkg/wsl-utils/run_windows.go | 11 ++++---- .../pkg/wsl-utils/utf16writer_windows.go | 25 ------------------- 2 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 src/go/wsl-helper/pkg/wsl-utils/utf16writer_windows.go diff --git a/src/go/wsl-helper/pkg/wsl-utils/run_windows.go b/src/go/wsl-helper/pkg/wsl-utils/run_windows.go index e030b6f315d..9dfed8d2afd 100644 --- a/src/go/wsl-helper/pkg/wsl-utils/run_windows.go +++ b/src/go/wsl-helper/pkg/wsl-utils/run_windows.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "os" "os/exec" "path/filepath" @@ -55,12 +56,10 @@ func (r *wslRunnerImpl) run(ctx context.Context, args ...string) error { } wslPath := filepath.Join(systemDir, "wsl.exe") cmd := exec.CommandContext(ctx, wslPath, args...) - if r.stdout != nil { - cmd.Stdout = &utf16Writer{r.stdout} - } - if r.stderr != nil { - cmd.Stderr = &utf16Writer{r.stderr} - } + cmd.Env = append(cmd.Env, os.Environ()...) + cmd.Env = append(cmd.Env, "WSL_UTF8=1") + cmd.Stdout = r.stdout + cmd.Stderr = r.stderr cmd.SysProcAttr = &windows.SysProcAttr{HideWindow: true} err = cmd.Run() if err != nil { diff --git a/src/go/wsl-helper/pkg/wsl-utils/utf16writer_windows.go b/src/go/wsl-helper/pkg/wsl-utils/utf16writer_windows.go deleted file mode 100644 index ddce3a3be02..00000000000 --- a/src/go/wsl-helper/pkg/wsl-utils/utf16writer_windows.go +++ /dev/null @@ -1,25 +0,0 @@ -package wslutils - -import ( - "io" - "unicode/utf16" - "unsafe" -) - -// utf16Writer is a writer that attempts to convert writes in UTF-16 to UTF-8. -type utf16Writer struct { - io.Writer -} - -func (w *utf16Writer) Write(p []byte) (int, error) { - // Manually decode the data; don't use windows.UTF16PtrToString because we - // don't assume the input is zero-terminated (but we have a valid length). - runes := utf16.Decode(unsafe.Slice((*uint16)(unsafe.Pointer(unsafe.SliceData(p))), len(p)/2)) - str := string(runes) - _, err := w.Writer.Write(unsafe.Slice(unsafe.StringData(str), len(str))) - // Even though we may not have written all of the bytes, report that we - // consumed it all; this beats figuring out how many bytes we have _actually_ - // written. To do this correctly, we'd need to decode the runes one at a time - // and feed it to the writer without buffering, but that seems terrible. - return len(p), err -}