{"record":{"id":"6d8770f8e6694957","repo":"lima-vm/lima","slug":"failed-to-run-v-w-stderr-q","errorCode":null,"errorMessage":"failed to run %v: %w; stderr=%#q","messagePattern":"failed to run (.+?): %w; stderr=%#q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/guestagent/kubernetesservice/kubernetesservice.go","lineNumber":136,"sourceCode":"\t\treturn fmt.Errorf(\"failed to run %v: %w; stdout=%#q, stderr=%#q\", cmd.Args, err, stdout.String(), stderr.String())\n\t}\n\tif strings.TrimSpace(stdout.String()) != \"yes\" {\n\t\treturn fmt.Errorf(\"failed to run %v: expected `yes`, got %#q\", cmd.Args, stdout.String())\n\t}\n\treturn nil\n}\n\nfunc (s *ServiceWatcher) startAndStreamKubectl(cmd *exec.Cmd) error {\n\tstdout, err := cmd.StdoutPipe()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tvar stderr bytes.Buffer\n\tcmd.Stderr = &stderr\n\n\tif err := cmd.Start(); err != nil {\n\t\treturn fmt.Errorf(\"failed to run %v: %w; stderr=%#q\", cmd.Args, err, stderr.String())\n\t}\n\n\treadErr := s.readKubectlStream(stdout)\n\twaitErr := cmd.Wait()\n\tif waitErr != nil {\n\t\twaitErr = fmt.Errorf(\"failed to run %v: %w; stderr=%#q\", cmd.Args, waitErr, stderr.String())\n\t}\n\treturn errors.Join(readErr, waitErr)\n}\n\n// readKubectlStream reads kubectl JSON watch events from r and updates the internal\n// services map. The stream is newline-delimited JSON objects representing \"WatchEvent\".\nfunc (s *ServiceWatcher) readKubectlStream(r io.Reader) error {\n\tscanner := bufio.NewScanner(r)\n\t// increase buffer in case of large JSON objects\n\tconst maxBuf = 10 * units.MiB\n\tbuf := make([]byte, 0, 64*units.KiB)\n\tscanner.Buffer(buf, maxBuf)","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/guestagent/kubernetesservice/kubernetesservice.go#L118-L154","documentation":"startAndStreamKubectl launches a long-running kubectl command with StdoutPipe and streams its output; if cmd.Start fails (or the subsequent cmd.Wait fails, which is wrapped with the same message), this error includes cmd.Args and whatever stderr was captured. Note that stderr is captured into a buffer that is only filled during the run, so on Start failure it is typically empty.","triggerScenarios":"The streaming kubectl command cannot be started: the kubectl binary is missing or not executable (exec: not found / permission denied), or the process exits during Wait with a nonzero status (context canceled, auth failure, connection loss) - the Wait variant wraps the same message.","commonSituations":"kubectl absent from PATH in the guest; kubeconfig removed or made invalid while streaming; cluster API server restarting mid-stream; guestagent context canceled during shutdown (benign during teardown).","solutions":["Check that the kubectl binary in cmd.Args exists and is executable inside the guest.","Inspect the stderr=... portion (on Wait failures) for kubectl's own error output.","Verify the kubeconfig remains valid for the duration of streaming (`kubectl auth can-i get services`).","If the error appears during instance shutdown, it is expected context cancellation and can be ignored."],"exampleFix":"// before\nexec.Command(\"kubectl-not-installed\", \"get\", \"services\", \"--watch\")\n// after\nexec.Command(\"kubectl\", \"get\", \"services\", \"--watch\") // ensure kubectl is on PATH","handlingStrategy":"try-catch","validationCode":"if _, err := exec.LookPath(\"kubectl\"); err != nil {\n    return fmt.Errorf(\"kubectl not found in PATH\")\n}\nif _, err := os.Stat(kubeconfig); err != nil {\n    return fmt.Errorf(\"kubeconfig missing: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := startAndStreamKubectl(cmd); err != nil {\n    if errors.Is(err, exec.ErrNotFound) || strings.Contains(err.Error(), \"not found\") {\n        log.Println(\"install kubectl in the guest\")\n    } else if errors.Is(err, context.Canceled) {\n        // expected during shutdown; ignore\n    }\n}","preventionTips":["Ensure kubectl is present and executable before starting streaming watchers.","Treat context-cancellation errors during shutdown as benign.","Keep the kubeconfig valid for the entire lifetime of the streaming process."],"tags":["kubernetes","kubectl","guestagent","process"],"backgroundTag":"kubectl-start-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}