fix: set terminal type for tmux attach (#2312)

- Force TERM for tmux attach processes
- Cover missing and unsupported TERM values
This commit is contained in:
Khushi Diwan 2026-07-01 03:27:29 +05:30 committed by GitHub
parent d302414f52
commit 6a7ba46078
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -236,7 +236,7 @@ func (r *Runtime) Attach(ctx context.Context, handle ports.RuntimeHandle, rows,
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ptyexec.Spawn(ctx, argv, nil, rows, cols) return ptyexec.Spawn(ctx, argv, attachEnv(os.Environ()), rows, cols)
} }
// attachCommand returns the argv to attach a terminal to the session. // attachCommand returns the argv to attach a terminal to the session.
@ -249,6 +249,17 @@ func (r *Runtime) attachCommand(handle ports.RuntimeHandle) ([]string, error) {
return []string{r.binary, "attach-session", "-t", id}, nil return []string{r.binary, "attach-session", "-t", id}, nil
} }
func attachEnv(base []string) []string {
env := append([]string(nil), base...)
for i, kv := range env {
if strings.HasPrefix(kv, "TERM=") {
env[i] = "TERM=xterm-256color"
return env
}
}
return append(env, "TERM=xterm-256color")
}
// run wraps runner.Run with a per-call timeout context. // run wraps runner.Run with a per-call timeout context.
func (r *Runtime) run(ctx context.Context, args ...string) ([]byte, error) { func (r *Runtime) run(ctx context.Context, args ...string) ([]byte, error) {
cmdCtx, cancel := context.WithTimeout(ctx, r.timeout) cmdCtx, cancel := context.WithTimeout(ctx, r.timeout)

View File

@ -551,6 +551,18 @@ func TestAttachCommandRejectsInvalidHandle(t *testing.T) {
} }
} }
func TestAttachEnvForcesUsableTerm(t *testing.T) {
env := attachEnv([]string{"PATH=/bin", "TERM=dumb", "SHELL=/bin/sh"})
if got, want := env, []string{"PATH=/bin", "TERM=xterm-256color", "SHELL=/bin/sh"}; !reflect.DeepEqual(got, want) {
t.Fatalf("attachEnv = %#v, want %#v", got, want)
}
env = attachEnv([]string{"PATH=/bin"})
if got, want := env, []string{"PATH=/bin", "TERM=xterm-256color"}; !reflect.DeepEqual(got, want) {
t.Fatalf("attachEnv without TERM = %#v, want %#v", got, want)
}
}
// -- commandError tests -- // -- commandError tests --
func TestCommandErrorUnwraps(t *testing.T) { func TestCommandErrorUnwraps(t *testing.T) {