Skip to content

Commit 0ce8919

Browse files
authored
fix(v2): set LANG for pbpaste/pbcopy to prevent clipboard mojibake on macOS (#5012)
1 parent 63725f1 commit 0ce8919

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

v2/internal/frontend/desktop/darwin/clipboard.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,25 @@
33
package darwin
44

55
import (
6+
"os"
67
"os/exec"
78
)
89

10+
// ensureUTF8Env returns the current environment with LANG set to en_US.UTF-8
11+
// if it is not already set. This is needed because packaged macOS apps do not
12+
// inherit the terminal's LANG variable, causing pbpaste/pbcopy to default to
13+
// an ASCII-compatible encoding that mangles non-ASCII text.
14+
func ensureUTF8Env() []string {
15+
env := os.Environ()
16+
if _, ok := os.LookupEnv("LANG"); !ok {
17+
env = append(env, "LANG=en_US.UTF-8")
18+
}
19+
return env
20+
}
21+
922
func (f *Frontend) ClipboardGetText() (string, error) {
1023
pasteCmd := exec.Command("pbpaste")
24+
pasteCmd.Env = ensureUTF8Env()
1125
out, err := pasteCmd.Output()
1226
if err != nil {
1327
return "", err
@@ -17,6 +31,7 @@ func (f *Frontend) ClipboardGetText() (string, error) {
1731

1832
func (f *Frontend) ClipboardSetText(text string) error {
1933
copyCmd := exec.Command("pbcopy")
34+
copyCmd.Env = ensureUTF8Env()
2035
in, err := copyCmd.StdinPipe()
2136
if err != nil {
2237
return err

website/src/pages/changelog.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
### Fixed
1818

19+
- Fixed clipboard mojibake on macOS by setting LANG environment variable for pbpaste/pbcopy operations [#5012](https://github.com/wailsapp/wails/pull/5012) by @veeceey
1920
- Fixed `wails init` to prevent initialization in non-empty directories when using the `-d` flag, avoiding accidental data loss [`#4940`](https://github.com/wailsapp/wails/issues/4940) by `@leaanthony`
2021
- Fixed missing `EventsOffAll` in runtime templates for all frontend frameworks [#4883](https://github.com/wailsapp/wails/pull/4883) by @narcilee7
2122
- Fixed Linux crash on panic in JS-bound Go methods due to WebKit overriding signal handlers [#3965](https://github.com/wailsapp/wails/issues/3965) by @leaanthony

0 commit comments

Comments
 (0)