fix(input): preserve legacy capture when the probe fails

This commit is contained in:
千川白浪 committed 2026-12-04 23:01:43 -07:00
1 parent ed1a0ba254
commit 3d5c6cb826
8 files changed
+50 -43

No files matched your search

+1 -1
View File
@@ -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),
}
}
+1 -1
View File
@@ -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))
+3 -3
View File
@@ -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 {
+1 -1
View File
@@ -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)
}
+2 -6
View File
@@ -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)
}
+6 -10
View File
@@ -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)
}
+32 -14
View File
@@ -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)
}
+4 -7
View File
@@ -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 {