{"record":{"id":"d93a6ea4951c29c5","repo":"abiosoft/colima","slug":"error-retrieving-home-directory-on-host","errorCode":null,"errorMessage":"error retrieving home directory on host","messagePattern":"error retrieving home directory on host","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"environment/container/kubernetes/kubeconfig.go","lineNumber":35,"sourceCode":"func (c kubernetesRuntime) provisionKubeconfig(ctx context.Context) error {\n\tip := limautil.IPAddress(config.CurrentProfile().ID)\n\tif ip == c.guest.Get(masterAddressKey) {\n\t\treturn nil\n\t}\n\n\tlog := c.Logger(ctx)\n\ta := c.Init(ctx)\n\n\ta.Stage(\"updating config\")\n\n\t// remove existing configs (if any)\n\t// this is safe as the profile name is unique to colima\n\tc.unsetKubeconfig(a)\n\n\t// ensure host kube directory exists\n\thostHome := c.host.Env(\"HOME\")\n\tif hostHome == \"\" {\n\t\treturn fmt.Errorf(\"error retrieving home directory on host\")\n\t}\n\n\tprofile := config.CurrentProfile().ID\n\thostKubeDir := filepath.Join(hostHome, \".kube\")\n\ta.Add(func() error {\n\t\treturn c.host.Run(\"mkdir\", \"-p\", filepath.Join(hostKubeDir, \".\"+profile))\n\t})\n\n\tkubeconfFile := filepath.Join(hostKubeDir, \"config\")\n\tenvKubeConfFile := c.host.Env(\"KUBECONFIG\")\n\tif envKubeConfFile != \"\" {\n\t\tkubeconfFile = filepath.SplitList(envKubeConfFile)[0]\n\t}\n\ttmpkubeconfFile := filepath.Join(hostKubeDir, \".\"+profile, \"colima-temp\")\n\n\t// manipulate in VM and save to host\n\ta.Add(func() error {\n\t\tkubeconfig, err := c.guest.Read(\"/etc/rancher/k3s/k3s.yaml\")","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/abiosoft/colima/blob/c3a5f9184d83a197184f897a9f07eb3c01b3bc88/environment/container/kubernetes/kubeconfig.go#L17-L53","documentation":"Raised in the kubeconfig setup chain when the host's HOME environment variable is empty (c.host.Env(\"HOME\") == \"\"). Colima needs the host home directory to locate/create ~/.kube and place the merged k3s kubeconfig there; without HOME there is no target path, so setup aborts before any host command is run.","triggerScenarios":"Any invocation of 'colima start --kubernetes' (or kube context update) in an environment where HOME is unset: cron jobs, launchd/systemd units, bare 'sudo -E' misuse stripping env, some CI runners, or 'env -i colima ...'. On the Go side, os.Getenv(\"HOME\") returns \"\" when the variable is absent (notably sudo sets HOME to root's home only with -H/-i, and unset when the caller clears it).","commonSituations":"Running colima from a launchd agent or Docker/CI container without a login shell; scripts that use 'sudo env -i' or sanitize the environment; switched users via 'su' without '-' so HOME points nowhere; shell misconfiguration exporting HOME=''.","solutions":["Run colima from a normal login shell where HOME is set: 'HOME=$HOME colima start --kubernetes'.","For scripts/services, explicitly export HOME before invoking colima (e.g. in the systemd unit: Environment=HOME=/Users/me or EnvironmentFile covering HOME).","If you must run as root, use 'sudo -E colima ...' (preserve env) or set HOME explicitly, and understand kubeconfig will then target that HOME's ~/.kube.","Verify with 'echo $HOME' and 'env | grep HOME' in the exact context that fails before re-running."],"exampleFix":"# before\n$ sudo -i some-wrapper.sh   # HOME cleared/changed\n# after\n$ sudo -E env HOME=\"$HOME\" ./some-wrapper.sh   # colima sees the real home","handlingStrategy":"validation","validationCode":"// guard before calling any kubeconfig setup\nif os.Getenv(\"HOME\") == \"\" {\n    return fmt.Errorf(\"HOME is not set; set HOME=<your home> and retry\")\n}","typeGuard":"func hasHomeEnv() bool { return os.Getenv(\"HOME\") != \"\" }","tryCatchPattern":"hostHome := c.host.Env(\"HOME\")\nif hostHome == \"\" {\n    // fallback: expand ~ via os.UserHomeDir() before giving up\n    if h, err := os.UserHomeDir(); err == nil {\n        hostHome = h\n    } else {\n        return fmt.Errorf(\"error retrieving home directory on host: %w\", err)\n    }\n}","preventionTips":["Always run colima from a login shell or a context that exports HOME.","In launchd/systemd/cron wrappers, set Environment=HOME explicitly.","Never sanitize env with 'env -i' around colima commands."],"tags":["kubeconfig","kubernetes","environment-variables","home","host"],"backgroundTag":null,"analyzedSha":"c3a5f9184d83a197184f897a9f07eb3c01b3bc88","analyzedAt":"2026-08-15T18:58:08.334Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}