{"record":{"id":"529a62a531a742cc","repo":"ahmetb/kubectx","slug":"no-previous-namespace-found-for-current-context","errorCode":null,"errorMessage":"No previous namespace found for current context (%s)","messagePattern":"No previous namespace found for current context \\((.+?)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"cmd/kubens/switch.go","lineNumber":72,"sourceCode":"\t\treturn \"\", fmt.Errorf(\"failed to get current context: %w\", err)\n\t}\n\tif ctx == \"\" {\n\t\treturn \"\", errors.New(\"current-context is not set\")\n\t}\n\tcurNS, err := kc.NamespaceOfContext(ctx)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get current namespace: %w\", err)\n\t}\n\n\tf := NewNSFile(ctx)\n\tprev, err := f.Load()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to load previous namespace from file: %w\", err)\n\t}\n\n\tif ns == \"-\" {\n\t\tif prev == \"\" {\n\t\t\treturn \"\", fmt.Errorf(\"No previous namespace found for current context (%s)\", ctx)\n\t\t}\n\t\tns = prev\n\t}\n\n\tif !force {\n\t\tok, err := namespaceExists(kc, ns)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to query if namespace exists (is cluster accessible?): %w\", err)\n\t\t}\n\t\tif !ok {\n\t\t\treturn \"\", fmt.Errorf(\"no namespace exists with name \\\"%s\\\"\", ns)\n\t\t}\n\t}\n\n\tif err := kc.SetNamespace(ctx, ns); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to change to namespace \\\"%s\\\": %w\", ns, err)\n\t}\n\tif err := kc.Save(); err != nil {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubens/switch.go#L54-L90","documentation":"When the user runs `kubens -`, switchNamespace swaps to the previous namespace recorded in the per-context namespace file. If that file loaded successfully but contains an empty string, there is nothing to swap to and this error is thrown. Thrown at cmd/kubens/switch.go:72.","triggerScenarios":"Target is \"-\" and f.Load() returned (\"\", nil): no previous namespace has ever been saved for the current context (first switch, or history was wiped).","commonSituations":"Running `kubens -` right after installing kubens; after switching clusters/contexts (history is per-context); after deleting the kubens state directory; in a fresh container where ~/.kube/kubens does not persist.","solutions":["Switch to a namespace by name first (kubens <ns>); after that `kubens -` will work","Check you are on the expected context: kubectl config current-context — history is tracked per context","Use the explicit namespace name instead of '-' for this one switch"],"exampleFix":"// before\nkubens -   # no history for this context\n// after\nkubens kube-system   # first switch records history\nkubens -             # now swaps back","handlingStrategy":"validation","validationCode":"if target == \"-\" {\n    prev, _ := NewNSFile(currentCtx).Load()\n    if prev == \"\" {\n        return errors.New(\"no previous namespace recorded; switch by name first\")\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := runKubens(\"-\"); err != nil && strings.Contains(err.Error(), \"No previous namespace\") {\n    return runKubens(fallbackNamespace) // e.g. \"default\"\n}","preventionTips":["Only use `kubens -` after at least one successful named switch in the current context","Remember history is per-context; switching contexts does not carry history over","Persist ~/.kube/kubens across container restarts if you rely on '-'"],"tags":["kubens","state","user-input"],"backgroundTag":"no-previous-namespace","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}