{"record":{"id":"be7ffc9d2c506b6b","repo":"golang/go","slug":"copying-s-w","errorCode":null,"errorMessage":"copying %s: %w","messagePattern":"copying (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/work/shell.go","lineNumber":225,"sourceCode":"\t\tif _, err := os.Stat(dst + \"~\"); err == nil {\n\t\t\tos.Remove(dst + \"~\")\n\t\t}\n\t}\n\n\tmayberemovefile(dst)\n\tdf, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)\n\tif err != nil && runtime.GOOS == \"windows\" {\n\t\t// Windows does not allow deletion of a binary file\n\t\t// while it is executing. Try to move it out of the way.\n\t\t// If the move fails, which is likely, we'll try again the\n\t\t// next time we do an install of this binary.\n\t\tif err := os.Rename(dst, dst+\"~\"); err == nil {\n\t\t\tos.Remove(dst + \"~\")\n\t\t}\n\t\tdf, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"copying %s: %w\", src, err) // err should already refer to dst\n\t}\n\n\t_, err = io.Copy(df, sf)\n\tdf.Close()\n\tif err != nil {\n\t\tmayberemovefile(dst)\n\t\treturn fmt.Errorf(\"copying %s to %s: %v\", src, dst, err)\n\t}\n\treturn nil\n}\n\n// mayberemovefile removes a file only if it is a regular file\n// When running as a user with sufficient privileges, we may delete\n// even device files, for example, which is not intended.\nfunc mayberemovefile(s string) {\n\tif fi, err := os.Lstat(s); err == nil && !fi.Mode().IsRegular() {\n\t\treturn\n\t}","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/work/shell.go#L207-L243","documentation":"Thrown by copyFile in the go build/install path when the destination file cannot be opened for writing (O_WRONLY|O_CREATE|O_TRUNC). On Windows a second attempt is made after moving the running binary aside; if that also fails, the wrapped error (referring to dst) is returned with the source path in the message.","triggerScenarios":"Run `go install` / `go build -o` to a destination that is read-only, on a full disk, in a directory without write permission, or held open by another process (and on non-Windows there is no rename-aside retry). os.OpenFile fails and the error is wrapped as `copying <src>: <err>`.","commonSituations":"Installing into a GOBIN owned by root without sudo; destination on a read-only filesystem; ENOSPC (disk full); SELinux/AppArmor denying the write; antivirus or indexer holding the file (Windows).","solutions":["Check write permission on the destination directory: `ls -ld $(dirname <dst>)`.","Free disk space if full: `df -h`.","On Windows, close any process running the previous binary, then rebuild.","Fix ownership/permissions or pick a writable GOBIN: `go install -o /tmp/bin/app`."],"exampleFix":"# before\n// go install ./...   (GOBIN not writable)\n\n# after\n// sudo chown -R $USER ~/go/bin && go install ./...","handlingStrategy":"try-catch","validationCode":"// Ensure the install destination is writable before building\ndst := filepath.Join(os.Getenv(\"GOBIN\"), \"app\")\nif fi, err := os.Stat(filepath.Dir(dst)); err != nil || !fi.IsDir() {\n    log.Fatalf(\"GOBIN dir not usable: %v\", err)\n}\nif err := os.WriteFile(dst+\".probe\", []byte{}, 0644); err != nil {\n    log.Fatalf(\"cannot write to %s: %v\", filepath.Dir(dst), err)\n}\nos.Remove(dst + \".probe\")","typeGuard":null,"tryCatchPattern":"// Wrap go build/install and surface a clear message\nerr := sh.copyFile(src, dst)\nif err != nil {\n    if errors.Is(err, fs.ErrPermission) {\n        return fmt.Errorf(\"permission denied writing %s: check ownership/GOBIN\", dst)\n    }\n    if errors.Is(err, fs.ErrNotExist) {\n        return fmt.Errorf(\"destination dir missing: %s\", filepath.Dir(dst))\n    }\n    return fmt.Errorf(\"copy failed: %w\", err)\n}","preventionTips":["Pre-flight check write permission on GOBIN.","Keep sufficient free disk space before install.","On Windows, stop running binaries before reinstalling."],"tags":["go-toolchain","build","filesystem","permissions","install"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}