{"record":{"id":"7ed7cf73903572d3","repo":"ahmetb/kubectx","slug":"failed-to-save-previous-context-name-w","errorCode":null,"errorMessage":"failed to save previous context name: %w","messagePattern":"failed to save previous context name: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubectx/switch.go","lineNumber":84,"sourceCode":"\t\treturn \"\", fmt.Errorf(\"failed to get current context: %w\", err)\n\t}\n\texists, err := kc.ContextExists(name)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to check context: %w\", err)\n\t}\n\tif !exists {\n\t\treturn \"\", fmt.Errorf(\"no context exists with the name: \\\"%s\\\"\", name)\n\t}\n\tif err := kc.ModifyCurrentContext(name); err != nil {\n\t\treturn \"\", err\n\t}\n\tif err := kc.Save(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to save kubeconfig: %w\", err)\n\t}\n\n\tif prev != name {\n\t\tif err := writeLastContext(prevCtxFile, prev); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to save previous context name: %w\", err)\n\t\t}\n\t}\n\treturn name, nil\n}\n\n// swapContext switches to previously switch context.\nfunc swapContext() (string, error) {\n\tprevCtxFile, err := kubectxPrevCtxFile()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to determine state file: %w\", err)\n\t}\n\tprev, err := readLastContext(prevCtxFile)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to read previous context file: %w\", err)\n\t}\n\tif prev == \"\" {\n\t\treturn \"\", errors.New(\"no previous context found\")\n\t}","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubectx/switch.go#L66-L102","documentation":"writeLastContext failed to persist the previous context name into the kubectx state file so that '-' can switch back. Fires when creating the state file's parent directory or writing the file fails (permissions, unwritable home).","triggerScenarios":"Thrown at cmd/kubectx/switch.go:84 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the state directory (under ~/.kube or XDG state dir) is writable","Fix HOME if it points to an unwritable location","Note the kubeconfig switch itself already saved; only the '-' history is lost"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}