-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
155 lines (148 loc) · 4.15 KB
/
Copy pathexec.go
File metadata and controls
155 lines (148 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
)
// ignoreCtrlC toggles the calling process's CTRL+C disposition via
// SetConsoleCtrlHandler(NULL, ignore). When set, the OS does not deliver
// CTRL_C_EVENT to this process, so Go's runtime handler never fires for
// Ctrl+C. We use this while a child shell is running — without it, Go's
// CTRL_C_EVENT dispatch (even with [signal.Ignore]) disturbs interactive
// shells like nushell when they Ctrl+C a grandchild process, leaving
// stdin corrupted afterward.
func ignoreCtrlC(ignore bool) {
var add uintptr
if ignore {
add = 1
}
proc := syscall.NewLazyDLL("kernel32.dll").NewProc("SetConsoleCtrlHandler")
_, _, _ = proc.Call(0, add)
}
func mergedEnv(diff map[string]string) []string {
parent := os.Environ()
overrides := make(map[string]struct{}, len(diff))
for k := range diff {
overrides[strings.ToUpper(k)] = struct{}{}
}
keep := make([]string, 0, len(parent)+len(diff))
for _, e := range parent {
eq := strings.IndexByte(e, '=')
if eq <= 0 {
continue
}
if _, ok := overrides[strings.ToUpper(e[:eq])]; ok {
continue
}
keep = append(keep, e)
}
for k, v := range diff {
keep = append(keep, k+"="+v)
}
return keep
}
func envValue(env []string, name string) string {
upper := strings.ToUpper(name) + "="
for _, e := range env {
if strings.HasPrefix(strings.ToUpper(e), upper) {
return e[len(upper):]
}
}
return ""
}
func lookInEnvPath(file, pathEnv, pathExt string) (string, error) {
if strings.ContainsAny(file, `\/`) || filepath.IsAbs(file) {
return file, nil
}
exts := []string{""}
for x := range strings.SplitSeq(pathExt, ";") {
x = strings.TrimSpace(x)
if x != "" {
exts = append(exts, x)
}
}
for _, dir := range filepath.SplitList(pathEnv) {
if dir == "" {
continue
}
for _, ext := range exts {
full := filepath.Join(dir, file+ext)
if info, err := os.Stat(full); err == nil && !info.IsDir() {
return full, nil
}
}
}
return "", fmt.Errorf("%q not found in PATH", file)
}
func execChild(ctx context.Context, args []string, diff map[string]string) error {
env := mergedEnv(diff)
pathExt := envValue(env, "PATHEXT")
if pathExt == "" {
pathExt = ".COM;.EXE;.BAT;.CMD"
}
resolved, err := lookInEnvPath(args[0], envValue(env, "PATH"), pathExt)
if err != nil {
return err
}
c := exec.CommandContext(ctx, resolved, args[1:]...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Env = env
if runErr := c.Run(); runErr != nil {
if ee, ok := errors.AsType[*exec.ExitError](runErr); ok {
os.Exit(ee.ExitCode())
}
return runErr
}
return nil
}
func spawnShell(ctx context.Context, shell string, diff map[string]string) error {
autoDetected := shell == ""
if autoDetected {
shell = detectParentShell()
}
var cmd *exec.Cmd
switch strings.ToLower(shell) {
case shellIDPowerShell:
cmd = exec.CommandContext(ctx, "powershell.exe", "-NoLogo")
case shellIDPwsh:
cmd = exec.CommandContext(ctx, "pwsh.exe", "-NoLogo")
case shellIDCmd:
cmd = exec.CommandContext(ctx, "cmd.exe", "/k", "prompt [vsenv] $P$G")
case shellIDNu, "nushell":
cmd = exec.CommandContext(ctx, "nu.exe")
case shellIDBash:
cmd = exec.CommandContext(ctx, "bash.exe")
default:
return fmt.Errorf("unknown shell %q", shell)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = mergedEnv(diff)
if autoDetected {
fmt.Fprintf(os.Stderr, "[vsenv] launching %s (auto-detected; override with --shell)\n", shell)
}
// Belt: ignore SIGINT at the Go level so Ctrl+Break (mapped to SIGINT by
// the Go runtime) doesn't terminate us, and to cover the microsecond
// window before SetConsoleCtrlHandler takes effect below.
signal.Ignore(os.Interrupt)
defer signal.Reset(os.Interrupt)
if err := cmd.Start(); err != nil {
return err
}
// Suspenders: after the child is spawned (so it doesn't inherit the
// disposition), tell Windows to suppress CTRL_C_EVENT delivery to us
// entirely. Without this, Go's CTRL_C_EVENT handler running in vsenv
// corrupts interactive shells (e.g. nushell) when they Ctrl+C a child.
ignoreCtrlC(true)
defer ignoreCtrlC(false)
return cmd.Wait()
}