{"record":{"id":"45f69f7253593689","repo":"ahmetb/kubectx","slug":"no-previous-context-found","errorCode":null,"errorMessage":"no previous context found","messagePattern":"no previous context found","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubectx/switch.go","lineNumber":101,"sourceCode":"\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}\n\treturn switchContext(prev)\n}\n","sourceCodeStart":83,"sourceCodeEnd":105,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubectx/switch.go#L83-L105","documentation":"Guard error in swapContext: readLastContext returned an empty value, meaning the previous-context state file is missing, empty, or has never been written. `kubectx -s` (swap) can only work after at least one prior `kubectx <NAME>` switch recorded the previous context.","triggerScenarios":"Thrown at cmd/kubectx/switch.go:101 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Switch to a context with `kubectx <NAME>` first; afterwards `kubectx -s` can swap back","Check that the cache file (~/.cache/kubectx/kubectx or XDG_CACHE_HOME) was not deleted"],"exampleFix":null,"handlingStrategy":"validation","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"}