{"record":{"id":"738fa1ce5aa64694","repo":"cli/cli","slug":"failed-to-remove-copilot-cli-w","errorCode":null,"errorMessage":"failed to remove Copilot CLI: %w","messagePattern":"failed to remove Copilot CLI: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/cmd/copilot/copilot.go","lineNumber":476,"sourceCode":"\t}\n\tdefer func() {\n\t\tif cerr := out.Close(); err == nil && cerr != nil {\n\t\t\terr = fmt.Errorf(\"failed to close file: %w\", cerr)\n\t\t}\n\t}()\n\tif _, err := io.Copy(out, r); err != nil {\n\t\treturn fmt.Errorf(\"failed to write file: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc removeCopilot(installDir string) error {\n\tif _, err := os.Stat(installDir); os.IsNotExist(err) {\n\t\treturn fmt.Errorf(\"failed to remove Copilot CLI: Copilot CLI not installed through `gh`\")\n\t}\n\n\tif err := os.RemoveAll(installDir); err != nil {\n\t\treturn fmt.Errorf(\"failed to remove Copilot CLI: %w\", err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":458,"sourceCodeEnd":481,"githubUrl":"https://github.com/cli/cli/blob/0eeec0b92edbe70199f9768522f831d3534f41ad/pkg/cmd/copilot/copilot.go#L458-L481","documentation":"removeCopilot's os.RemoveAll(installDir) returned an error, wrapped with the OS cause. The directory existed (Stat succeeded) but could not be fully deleted.","triggerScenarios":"RemoveAll fails on permission errors (files owned by root inside the dir), read-only filesystems, or files held open/locked by a running process (Windows file locking especially).","commonSituations":"Copilot CLI agent files installed via sudo so the directory is root-owned; the agent process still running and holding the binary open on Windows; install dir on a read-only or network mount.","solutions":["Read the wrapped error for the offending path and errno","Stop any running processes using files in the install dir, then retry","Fix ownership (chown -R) or remove with appropriate privileges","If the filesystem is read-only, remount or choose a writable location"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// pre-flight writability check on the install dir\nif info, err := os.Stat(installDir); err == nil {\n    if info.Mode().Perm()&0200 == 0 {\n        return fmt.Errorf(\"install dir %s not writable; fix permissions first\", installDir)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := removeCopilot(installDir); err != nil {\n    if strings.Contains(err.Error(), \"failed to remove Copilot CLI\") && errors.Is(err, fs.ErrPermission) {\n        return fmt.Errorf(\"run removal with sufficient privileges on %s\", installDir)\n    }\n    return err\n}","preventionTips":["Install and uninstall as the same (non-root) user consistently","Stop processes using files under the install dir before removal, especially on Windows","Never sudo-install into a user-owned config directory"],"tags":["copilot","uninstall","permissions","filesystem"],"backgroundTag":null,"analyzedSha":"0eeec0b92edbe70199f9768522f831d3534f41ad","analyzedAt":"2026-08-15T12:31:05.478Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}