From 3d5c6cb82602bb28ee7a91118402dfd8392ce1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Fri, 4 Dec 2026 23:01:43 -0700 Subject: [PATCH] fix(input): preserve legacy capture when the probe fails --- internal/app/app.go | 2 +- internal/app/create_flow.go | 2 +- internal/app/macos_settings.go | 6 ++-- internal/cli/macos_native.go | 2 +- internal/macos/macosdriver/clone.go | 8 ++--- internal/macos/reims/capture_compat.go | 16 ++++----- internal/macos/reims/compat_test.go | 46 ++++++++++++++++++-------- internal/macos/reims/reims.go | 11 +++--- 8 files changed, 50 insertions(+), 43 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index a6e8a30..8c87b9b 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -453,7 +453,7 @@ func (a *App) GetState() AppState { EffectiveLocale: string(i18n.EffectiveLocale(cfgCopy.Locale)), MissingFontHint: host.CheckMissingLocaleFontsForUI(string(i18n.EffectiveLocale(cfgCopy.Locale)), i18n.EffectiveLocale(cfgCopy.Locale)), - RuntimeCaptureSupported: !needsSetup && !rootStatus.ReadOnly && active != nil && runtimeSupportsCapture(*active), + RuntimeCaptureSupported: !needsSetup && !rootStatus.ReadOnly && active != nil && runtimeAllowsCapture(*active), } } diff --git a/internal/app/create_flow.go b/internal/app/create_flow.go index 7aaa035..b006061 100644 --- a/internal/app/create_flow.go +++ b/internal/app/create_flow.go @@ -186,7 +186,7 @@ func (a *App) createVMFlow(opts reims.CreateOpts) (err error) { finishTask(err) return fmt.Errorf("record VM driver ownership: %w", err) } - if postSet, ok := reims.PostCreateSetOptsWithCapture(opts, c.SupportsSetCapture()); ok { + if postSet, ok := reims.PostCreateSetOptsWithCapture(opts, c.AllowsSetCapture()); ok { var parts []string if postSet.DiskController != "" { parts = append(parts, "disk="+strings.ToUpper(postSet.DiskController)) diff --git a/internal/app/macos_settings.go b/internal/app/macos_settings.go index dc5a231..4531365 100644 --- a/internal/app/macos_settings.go +++ b/internal/app/macos_settings.go @@ -68,11 +68,11 @@ func (a *App) PreviewCreate(opts reims.CreateOpts) (string, error) { } cfg := a.runtimeConfig() active, err := rt.ResolveActive(&cfg) - return reims.PreviewCreateCommandWithCapture(opts, err == nil && runtimeSupportsCapture(*active)) + return reims.PreviewCreateCommandWithCapture(opts, err == nil && runtimeAllowsCapture(*active)) } -func runtimeSupportsCapture(active rt.Installed) bool { - return reims.ProbeReimsCTLSupportsCapture(active.ReimsCtlPath(), nil) +func runtimeAllowsCapture(active rt.Installed) bool { + return reims.ReimsCTLAllowsSetCapture(active.ReimsCtlPath(), nil) } func (a *App) CreateVM(opts reims.CreateOpts) error { diff --git a/internal/cli/macos_native.go b/internal/cli/macos_native.go index 343890f..2ee753c 100644 --- a/internal/cli/macos_native.go +++ b/internal/cli/macos_native.go @@ -110,7 +110,7 @@ func createMacOSVM(ctx context.Context, cfg *config.Config, loc i18n.Locale, in if err := driver.WriteOwnerDriver(createdDir, driver.BuiltinReimsMacOSDriverID); err != nil { return err } - if postSet, ok := reims.PostCreateSetOptsWithCapture(opts, client.SupportsSetCapture()); ok { + if postSet, ok := reims.PostCreateSetOptsWithCapture(opts, client.AllowsSetCapture()); ok { if err := client.Set(ctx, postSet, os.Stdout, os.Stderr); err != nil { fmt.Fprintf(os.Stderr, "Warning: could not apply post-create defaults: %v\n", err) } diff --git a/internal/macos/macosdriver/clone.go b/internal/macos/macosdriver/clone.go index 13e9bba..d186342 100644 --- a/internal/macos/macosdriver/clone.go +++ b/internal/macos/macosdriver/clone.go @@ -178,16 +178,12 @@ func (d *ReimsMacOSDriver) CloneCoW(ctx context.Context, sourceName, cloneName s if src.NetworkMode == "bridge" { network = true } - createOpts := reims.CreateOpts{Name: cloneName, SourceMode: "disk", Disk: overlayPath, DiskMode: "in-place", DiskController: src.DiskController, CPUs: src.CPUs, Memory: src.Memory, Graphics: src.Graphics, GPU: src.GPU, Resolution: src.Resolution, Displays: src.Displays, Audio: &audio, Network: &network, SSHPort: clonePort, Logs: &logs, Start: false} - captureSupported := c.SupportsSetCapture() - if captureSupported { - createOpts.Capture = &src.Capture - } + createOpts := reims.CreateOpts{Name: cloneName, SourceMode: "disk", Disk: overlayPath, DiskMode: "in-place", DiskController: src.DiskController, CPUs: src.CPUs, Memory: src.Memory, Graphics: src.Graphics, GPU: src.GPU, Resolution: src.Resolution, Displays: src.Displays, Capture: &src.Capture, Audio: &audio, Network: &network, SSHPort: clonePort, Logs: &logs, Start: false} if err := c.RegisterCoWClone(ctx, createOpts, stdout, stderr); err != nil { return err } cloneRegistered = true - if postSet, ok := reims.PostCreateSetOptsWithCapture(createOpts, captureSupported); ok { + if postSet, ok := reims.PostCreateSetOptsWithCapture(createOpts, c.AllowsSetCapture()); ok { if err := c.Set(ctx, postSet, stdout, stderr); err != nil { _, _ = fmt.Fprintf(stdout, "Warning: could not apply clone settings: %v\n", err) } diff --git a/internal/macos/reims/capture_compat.go b/internal/macos/reims/capture_compat.go index b392707..128cfa2 100644 --- a/internal/macos/reims/capture_compat.go +++ b/internal/macos/reims/capture_compat.go @@ -28,11 +28,11 @@ var ( captureOption = regexp.MustCompile(`(?m)(?:^|\s)--capture(?:\s|,|=|$)`) ) -// ProbeReimsCTLSupportsCapture checks set --help and caches successful checks -// for the current version of the binary. -func ProbeReimsCTLSupportsCapture(binaryPath string, env []string) bool { +// ReimsCTLAllowsSetCapture keeps the legacy option unless set --help +// successfully confirms that the runtime removed it. +func ReimsCTLAllowsSetCapture(binaryPath string, env []string) bool { ok, err := probeReimsCTLSupportsCapture(binaryPath, env) - return err == nil && ok + return err != nil || ok } func probeReimsCTLSupportsCapture(binaryPath string, env []string) (bool, error) { @@ -76,10 +76,6 @@ func probeReimsCTLSupportsCapture(binaryPath string, env []string) (bool, error) return call.ok, call.err } -func (c *Client) SupportsSetCapture() bool { - return ProbeReimsCTLSupportsCapture(c.Binary, c.Env) -} - -func (c *Client) probeSetCapture() (bool, error) { - return probeReimsCTLSupportsCapture(c.Binary, c.Env) +func (c *Client) AllowsSetCapture() bool { + return ReimsCTLAllowsSetCapture(c.Binary, c.Env) } diff --git a/internal/macos/reims/compat_test.go b/internal/macos/reims/compat_test.go index 6e9fc52..74fe627 100644 --- a/internal/macos/reims/compat_test.go +++ b/internal/macos/reims/compat_test.go @@ -251,10 +251,10 @@ func captureTestClient(t *testing.T, helpMode string) (Client, string) { "REIMS_TEST_CTL_DIR="+dir, "REIMS_TEST_CTL_CAPTURE_HELP="+helpMode)}, dir } -func TestProbeReimsCTLSupportsCapture(t *testing.T) { +func TestReimsCTLAllowsSetCapture(t *testing.T) { for _, path := range []string{"", filepath.Join(t.TempDir(), "missing.exe")} { - if ProbeReimsCTLSupportsCapture(path, nil) { - t.Errorf("probe reported support for %q", path) + if _, err := probeReimsCTLSupportsCapture(path, nil); !os.IsNotExist(err) { + t.Errorf("probe error for %q = %v, want file not found", path, err) } } for _, tc := range []struct { @@ -267,10 +267,10 @@ func TestProbeReimsCTLSupportsCapture(t *testing.T) { } { t.Run(tc.mode, func(t *testing.T) { c, dir := captureTestClient(t, tc.mode) - if got := ProbeReimsCTLSupportsCapture(c.Binary, c.Env); got != tc.want { + if got := ReimsCTLAllowsSetCapture(c.Binary, c.Env); got != tc.want { t.Fatalf("probe = %t, want %t", got, tc.want) } - if got := c.SupportsSetCapture(); got != tc.want { + if got := c.AllowsSetCapture(); got != tc.want { t.Fatalf("cached probe = %t, want %t", got, tc.want) } calls, err := os.ReadFile(filepath.Join(dir, "commands")) @@ -281,13 +281,13 @@ func TestProbeReimsCTLSupportsCapture(t *testing.T) { } t.Run("error", func(t *testing.T) { c, dir := captureTestClient(t, "error") - if c.SupportsSetCapture() { - t.Fatal("unsuccessful probe reported support") + if !c.AllowsSetCapture() { + t.Fatal("unsuccessful probe disabled the legacy option") } - if !ProbeReimsCTLSupportsCapture(c.Binary, append(c.Env, "REIMS_TEST_CTL_CAPTURE_HELP=stdout")) { - t.Fatal("successful probe after failure did not report support") + if ReimsCTLAllowsSetCapture(c.Binary, append(c.Env, "REIMS_TEST_CTL_CAPTURE_HELP=absent")) { + t.Fatal("successful retry did not detect removal") } - if !c.SupportsSetCapture() { + if c.AllowsSetCapture() { t.Fatal("successful retry was not cached") } calls, err := os.ReadFile(filepath.Join(dir, "commands")) @@ -308,7 +308,7 @@ func TestProbeReimsCTLSupportsCaptureConcurrent(t *testing.T) { go func() { ready.Done() <-start - results <- c.SupportsSetCapture() + results <- c.AllowsSetCapture() }() } ready.Wait() @@ -318,7 +318,7 @@ func TestProbeReimsCTLSupportsCaptureConcurrent(t *testing.T) { t.Error("concurrent probe did not report support") } } - if !ProbeReimsCTLSupportsCapture(c.Binary, append(c.Env, "REIMS_TEST_CTL_CAPTURE_HELP=error")) { + if !ReimsCTLAllowsSetCapture(c.Binary, append(c.Env, "REIMS_TEST_CTL_CAPTURE_HELP=absent")) { t.Error("concurrent probe result was not cached") } calls, err := os.ReadFile(filepath.Join(dir, "commands")) @@ -345,15 +345,32 @@ func TestSetSkipsUnsupportedCapture(t *testing.T) { } } +func TestPostCreateKeepsCaptureOffWhenHelpFails(t *testing.T) { + c, dir := captureTestClient(t, "error") + off := false + opts := CreateOpts{Name: "test", DiskController: "ahci", Capture: &off} + postSet, ok := PostCreateSetOptsWithCapture(opts, c.AllowsSetCapture()) + if !ok || postSet.Capture == nil || *postSet.Capture { + t.Fatalf("post-create options = %+v, ok = %t, want capture disabled", postSet, ok) + } + if err := c.Set(t.Context(), postSet, nil, nil); err != nil { + t.Fatal(err) + } + calls, err := os.ReadFile(filepath.Join(dir, "commands")) + if err != nil || string(calls) != "set\t--help\nset\t--help\nset\ttest\t--capture\tfalse\n" { + t.Fatalf("commands = %q, err = %v, want the legacy set after failed probes", calls, err) + } +} + func TestShowCaptureSupported(t *testing.T) { - for _, mode := range []string{"stdout", "absent"} { + for _, mode := range []string{"stdout", "absent", "error"} { t.Run(mode, func(t *testing.T) { c, _ := captureTestClient(t, mode) detail, err := c.Show(t.Context(), "test") if err != nil { t.Fatal(err) } - if want := mode == "stdout"; detail.CaptureSupported != want { + if want := mode != "absent"; detail.CaptureSupported != want { t.Fatalf("capture_supported = %t, want %t", detail.CaptureSupported, want) } }) @@ -407,6 +424,7 @@ func TestHelperProcess(t *testing.T) { case slices.Equal(args, []string{"show", "test"}): fmt.Println("test\n state stopped") case slices.Equal(args, []string{"set", "test", "--cpus", "4"}): + case helpMode == "error" && slices.Equal(args, []string{"set", "test", "--capture", "false"}): default: t.Fatalf("unexpected capture test command %q", args) } diff --git a/internal/macos/reims/reims.go b/internal/macos/reims/reims.go index b5c7ea6..d79dbac 100644 --- a/internal/macos/reims/reims.go +++ b/internal/macos/reims/reims.go @@ -764,7 +764,7 @@ func (c *Client) Show(ctx context.Context, name string) (VMDetail, error) { return VMDetail{}, fmt.Errorf("reims-ctl show %s: %w (%s)", name, err, strings.TrimSpace(errBuf.String())) } d := ParseShow(name, out.String()) - d.CaptureSupported = c.SupportsSetCapture() + d.CaptureSupported = c.AllowsSetCapture() applyBorderlessResolutionFallback(&d) return d, nil } @@ -826,12 +826,9 @@ func (c *Client) Create(ctx context.Context, o CreateOpts, stdout, stderr io.Wri } func (c *Client) Set(ctx context.Context, o SetOpts, stdout, stderr io.Writer) error { - skipCapture := false - if o.Capture != nil { - if supported, probeErr := c.probeSetCapture(); probeErr == nil && !supported { - skipCapture = true - o.Capture = nil - } + skipCapture := o.Capture != nil && !c.AllowsSetCapture() + if skipCapture { + o.Capture = nil } args, err := BuildSetArgs(o) if err != nil {