-
Notifications
You must be signed in to change notification settings - Fork 0
fix: avoid powershell browser opener on wsl #19
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,6 @@ | |
| *.swp | ||
| .env | ||
| .env.* | ||
|
|
||
| # Codex | ||
| .codex | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package browser | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "runtime" | ||
| ) | ||
|
|
||
| type commandSpec struct { | ||
| name string | ||
| args []string | ||
| } | ||
|
|
||
| var startProcess = defaultStartProcess | ||
|
|
||
| // OpenURL opens url in the user's browser. On WSL, avoid PowerShell-based | ||
| // launchers because their console encoding setup can write spurious errors. | ||
| func OpenURL(url string) error { | ||
| spec, err := commandForURL(runtime.GOOS, os.Getenv, exec.LookPath, url) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return launchCommand(spec) | ||
| } | ||
|
|
||
| func launchCommand(spec commandSpec) error { | ||
| wait, err := startProcess(spec) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| go func() { | ||
| _ = wait() | ||
| }() | ||
| return nil | ||
| } | ||
|
|
||
| func defaultStartProcess(spec commandSpec) (func() error, error) { | ||
| cmd := exec.CommandContext(context.Background(), spec.name, spec.args...) | ||
| cmd.Stdout = io.Discard | ||
| cmd.Stderr = io.Discard | ||
| if err := cmd.Start(); err != nil { | ||
| return nil, err | ||
| } | ||
| return cmd.Wait, nil | ||
| } | ||
|
|
||
| func commandForURL(goos string, getenv func(string) string, lookPath func(string) (string, error), url string) (commandSpec, error) { | ||
| switch goos { | ||
| case "darwin": | ||
| return commandSpec{name: "open", args: []string{url}}, nil | ||
| case "windows": | ||
| return commandSpec{name: "rundll32", args: []string{"url.dll,FileProtocolHandler", url}}, nil | ||
| case "linux": | ||
| if isWSL(getenv) { | ||
| if path, err := lookPath("rundll32.exe"); err == nil { | ||
| return commandSpec{name: path, args: []string{"url.dll,FileProtocolHandler", url}}, nil | ||
| } | ||
| if path, err := lookPath("cmd.exe"); err == nil { | ||
| return commandSpec{name: path, args: []string{"/C", "start", "", quoteCmdArg(url)}}, nil | ||
| } | ||
| } | ||
|
|
||
| for _, name := range []string{"xdg-open", "sensible-browser"} { | ||
| if path, err := lookPath(name); err == nil { | ||
| return commandSpec{name: path, args: []string{url}}, nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return commandSpec{}, errors.New("no browser opener found") | ||
| } | ||
|
|
||
| func isWSL(getenv func(string) string) bool { | ||
| return getenv("WSL_DISTRO_NAME") != "" || getenv("WSL_INTEROP") != "" | ||
| } | ||
|
|
||
| func quoteCmdArg(arg string) string { | ||
| quoted := `"` | ||
| for _, r := range arg { | ||
| switch r { | ||
| case '&', '|', '(', ')', '<', '>', '^': | ||
| quoted += "^" | ||
| case '"': | ||
| quoted += `\` | ||
| } | ||
| quoted += string(r) | ||
| } | ||
| return quoted + `"` | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package browser | ||
|
|
||
| import ( | ||
| "errors" | ||
| "reflect" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestCommandForURLUsesCmdExeOnWSL(t *testing.T) { | ||
| spec, err := commandForURL("linux", env(map[string]string{ | ||
| "WSL_DISTRO_NAME": "Ubuntu", | ||
| }), path(map[string]string{ | ||
| "cmd.exe": "/mnt/c/WINDOWS/system32/cmd.exe", | ||
| }), "https://example.test/device") | ||
| if err != nil { | ||
| t.Fatalf("commandForURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := commandSpec{name: "/mnt/c/WINDOWS/system32/cmd.exe", args: []string{"/C", "start", "", `"https://example.test/device"`}} | ||
| if !reflect.DeepEqual(spec, want) { | ||
| t.Fatalf("commandForURL() = %#v, want %#v", spec, want) | ||
| } | ||
| } | ||
|
|
||
| func TestCommandForURLQuotesCmdExeURLOnWSLWhenURLContainsCmdMetacharacters(t *testing.T) { | ||
| spec, err := commandForURL("linux", env(map[string]string{ | ||
| "WSL_DISTRO_NAME": "Ubuntu", | ||
| }), path(map[string]string{ | ||
| "cmd.exe": "/mnt/c/WINDOWS/system32/cmd.exe", | ||
| }), "https://example.test/device?foo=1&bar=(2)|baz=a%26b") | ||
| if err != nil { | ||
| t.Fatalf("commandForURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := commandSpec{name: "/mnt/c/WINDOWS/system32/cmd.exe", args: []string{"/C", "start", "", `"https://example.test/device?foo=1^&bar=^(2^)^|baz=a%26b"`}} | ||
| if !reflect.DeepEqual(spec, want) { | ||
| t.Fatalf("commandForURL() = %#v, want %#v", spec, want) | ||
| } | ||
| } | ||
|
|
||
| func TestCommandForURLUsesRundll32OnWSL(t *testing.T) { | ||
| spec, err := commandForURL("linux", env(map[string]string{ | ||
| "WSL_INTEROP": "/run/WSL/1_interop", | ||
| }), path(map[string]string{ | ||
| "rundll32.exe": "/mnt/c/WINDOWS/system32/rundll32.exe", | ||
| "cmd.exe": "/mnt/c/WINDOWS/system32/cmd.exe", | ||
| }), `https://example.test/device?code=A&B=(C)|D`) | ||
| if err != nil { | ||
| t.Fatalf("commandForURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := commandSpec{name: "/mnt/c/WINDOWS/system32/rundll32.exe", args: []string{"url.dll,FileProtocolHandler", `https://example.test/device?code=A&B=(C)|D`}} | ||
| if !reflect.DeepEqual(spec, want) { | ||
| t.Fatalf("commandForURL() = %#v, want %#v", spec, want) | ||
| } | ||
| } | ||
|
|
||
|
lusu007 marked this conversation as resolved.
|
||
| func TestCommandForURLUsesXDGOpenOnLinux(t *testing.T) { | ||
| spec, err := commandForURL("linux", env(nil), path(map[string]string{ | ||
| "xdg-open": "/usr/bin/xdg-open", | ||
| }), "https://example.test/device") | ||
| if err != nil { | ||
| t.Fatalf("commandForURL returned error: %v", err) | ||
| } | ||
|
|
||
| want := commandSpec{name: "/usr/bin/xdg-open", args: []string{"https://example.test/device"}} | ||
| if !reflect.DeepEqual(spec, want) { | ||
| t.Fatalf("commandForURL() = %#v, want %#v", spec, want) | ||
| } | ||
| } | ||
|
|
||
| func TestCommandForURLReturnsErrorWhenNoLinuxOpenerExists(t *testing.T) { | ||
| _, err := commandForURL("linux", env(nil), path(nil), "https://example.test/device") | ||
| if err == nil { | ||
| t.Fatal("commandForURL returned nil error") | ||
| } | ||
| } | ||
|
|
||
| func TestLaunchCommandWaitsForStartedProcess(t *testing.T) { | ||
| waited := make(chan struct{}) | ||
| started := false | ||
| startProcess = func(commandSpec) (func() error, error) { | ||
| started = true | ||
| return func() error { | ||
| close(waited) | ||
| return nil | ||
| }, nil | ||
| } | ||
| t.Cleanup(func() { | ||
| startProcess = defaultStartProcess | ||
| }) | ||
|
|
||
| if err := launchCommand(commandSpec{name: "opener", args: []string{"https://example.test/device"}}); err != nil { | ||
| t.Fatalf("launchCommand returned error: %v", err) | ||
| } | ||
| if !started { | ||
| t.Fatal("launchCommand did not start process") | ||
| } | ||
|
|
||
| select { | ||
| case <-waited: | ||
| case <-time.After(time.Second): | ||
| t.Fatal("launchCommand did not wait for process") | ||
| } | ||
| } | ||
|
|
||
| func env(values map[string]string) func(string) string { | ||
| return func(key string) string { | ||
| return values[key] | ||
| } | ||
| } | ||
|
|
||
| func path(available map[string]string) func(string) (string, error) { | ||
| return func(name string) (string, error) { | ||
| if path, ok := available[name]; ok { | ||
| return path, nil | ||
| } | ||
| return "", errors.New("not found") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.