Files
Baocang Nie 58c7e27cc3 fix(runas): run agent in its workspace under run_as_user (#1315)
sudo -i simulates a login and runs the command from the target user's HOME,
silently overriding cmd.Dir. Under run_as_user the agent therefore ignored its
(multi-workspace) working directory and started in HOME, so it couldn't see the
bound workspace or its CLAUDE.md.

Re-establish the intended cwd inside the spawn: when SpawnOptions.WorkDir is set,
BuildSpawnCommand wraps the command to 'cd "$CC_RUNAS_CHDIR" && exec "$@"'.
The path travels via the preserved env var (not argv) so non-ASCII/space paths
survive sudo's command re-quoting.

Fixes #1312
2026-06-16 10:01:32 +08:00

57 lines
1.7 KiB
Go

//go:build windows
package core
import (
"context"
"errors"
"os/exec"
)
// RunAsChdirEnv is unused on Windows (run_as_user is not supported) but is
// defined so cross-platform callers referencing core.RunAsChdirEnv compile.
const RunAsChdirEnv = "CC_RUNAS_CHDIR"
// DefaultEnvAllowlist is a stub on Windows — run_as_user is not supported.
var DefaultEnvAllowlist = []string{}
// SpawnOptions is a stub on Windows.
type SpawnOptions struct {
RunAsUser string
EnvAllowlist []string
WorkDir string
}
// IsolationMode always returns false on Windows.
func (o SpawnOptions) IsolationMode() bool { return false }
// BuildSpawnCommand on Windows ignores RunAsUser (config validation rejects
// it before we get here) and delegates to exec.CommandContext.
func BuildSpawnCommand(ctx context.Context, _ SpawnOptions, name string, args ...string) *exec.Cmd {
return exec.CommandContext(ctx, name, args...)
}
// FilterEnvForSpawn on Windows is a no-op pass-through.
func FilterEnvForSpawn(env []string, _ SpawnOptions) []string { return env }
// SudoRunner is a stub interface on Windows for API compatibility.
type SudoRunner interface {
Run(ctx context.Context, args ...string) ([]byte, error)
}
// ExecSudoRunner is a stub on Windows.
type ExecSudoRunner struct{}
// Run always fails on Windows.
func (ExecSudoRunner) Run(ctx context.Context, args ...string) ([]byte, error) {
return nil, errors.New("sudo is not supported on Windows")
}
// VerifyRunAsUserCheap always fails on Windows.
func VerifyRunAsUserCheap(_ context.Context, _ SudoRunner, runAsUser string) error {
if runAsUser == "" {
return nil
}
return errors.New("run_as_user is not supported on Windows")
}