{"record":{"id":"2407892d10dbd755","repo":"ahmetb/kubectx","slug":"failed-to-get-current-context-w-240789","errorCode":null,"errorMessage":"failed to get current context: %w","messagePattern":"failed to get current context: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubectx/switch.go","lineNumber":66,"sourceCode":"\treturn nil\n}\n\n// switchContext switches to specified context name.\nfunc switchContext(name string) (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\n\tkc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)\n\tdefer kc.Close()\n\tif err := kc.Parse(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"kubeconfig error: %w\", err)\n\t}\n\n\tprev, err := kc.GetCurrentContext()\n\tif err != nil {\n\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)","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubectx/switch.go#L48-L84","documentation":"GetCurrentContext errored while recording the previous context before switching. The current-context field could not be read via kyaml (e.g. field is not a scalar), which is distinct from it being merely unset — unset is not an error here.","triggerScenarios":"Thrown at cmd/kubectx/switch.go:66 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect current-context in each kubeconfig file and fix non-scalar values","Run kubectl config current-context to locate the offending file","Repair the YAML and retry the switch"],"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"}