{"record":{"id":"e4dbfbeddd74d172","repo":"googleapis/mcp-toolbox","slug":"failed-to-rename-file-w","errorCode":null,"errorMessage":"failed to rename file: %w","messagePattern":"failed to rename file: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/internal/migrate/command.go","lineNumber":103,"sourceCode":"\t\tif cmp.Equal(buf, newBuf) {\n\t\t\tcontinue\n\t\t}\n\n\t\tif cmd.dryRun {\n\t\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"printing migration to output for file: %s\", filePath))\n\t\t\tfmt.Fprintln(opts.IOStreams.Out, string(newBuf))\n\t\t} else {\n\t\t\tinfo, err := os.Stat(filePath)\n\t\t\tif err != nil {\n\t\t\t\terrMsg := fmt.Errorf(\"failed to stat file: %w\", err)\n\t\t\t\tlogger.ErrorContext(ctx, errMsg.Error())\n\t\t\t\terrs = append(errs, errMsg)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tbackupFile := filePath + \".bak\"\n\t\t\terr = os.Rename(filePath, backupFile)\n\t\t\tif err != nil {\n\t\t\t\terrMsg := fmt.Errorf(\"failed to rename file: %w\", err)\n\t\t\t\tlogger.ErrorContext(ctx, errMsg.Error())\n\t\t\t\terrs = append(errs, errMsg)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tlogger.DebugContext(ctx, fmt.Sprintf(\"successfully renamed %s to %s\", filePath, backupFile))\n\n\t\t\t// set the permission to the original file's permission.\n\t\t\terr = os.WriteFile(filePath, newBuf, info.Mode().Perm())\n\t\t\tif err != nil {\n\t\t\t\terrMsg := fmt.Errorf(\"failed to write to file: %w\", err)\n\t\t\t\t// restoring original file\n\t\t\t\tif removeErr := os.Remove(filePath); removeErr != nil { // Attempt to remove the possibly partial file to ensure Rename succeeds.\n\t\t\t\t\terrMsg = errors.Join(errMsg, removeErr)\n\t\t\t\t}\n\t\t\t\tif restoreErr := os.Rename(backupFile, filePath); restoreErr != nil {\n\t\t\t\t\tfullRestoreErr := fmt.Errorf(\"failed to restore original file: %w\", restoreErr)\n\t\t\t\t\terrMsg = errors.Join(errMsg, fullRestoreErr)\n\t\t\t\t}","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/cmd/internal/migrate/command.go#L85-L121","documentation":"Thrown when runMigrate cannot rename the original file to `<filePath>.bak` via `os.Rename` before writing the migrated content. The backup rename is part of a safe write sequence (backup, write, restore on failure); if it fails the file is left untouched and the error (with the wrapped OS reason) is appended to errs and that file is skipped. Typical underlying errors are EACCES/EPERM (no write permission on the directory) or EXDEV (source and destination on different filesystems, which cannot happen here since only `.bak` is appended, so permission/locking issues dominate).","triggerScenarios":"The directory containing the file is not writable by the current user, the file is immutable (chattr +i) or held by another process that blocks rename (rare on Linux, common on Windows file locks), or the filesystem is full/read-only.","commonSituations":"Running migrate inside a Docker container whose mounted volume is read-only; editing files owned by another user without sudo; a leftover `.bak` from a previous crashed run combined with odd directory ACLs; antivirus or editor locking the file on Windows/macOS; disk mounted read-only after filesystem errors.","solutions":["Check directory write permission: `ls -ld <dir>`; run migrate as a user with write access or fix ownership with chown/chmod.","Verify the volume is not mounted read-only (`mount | grep <dir>`) and remount read-write.","Look for a stale `filePath.bak` and remove it, then re-run migrate.","Close any process holding locks on the file (editors, antivirus, other containers) and retry.","Ensure the filesystem is not full: `df -h <dir>`."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"dir := filepath.Dir(filePath)\nprobe := filepath.Join(dir, \".migrate_probe\")\nif err := os.WriteFile(probe, nil, 0o600); err != nil {\n    return fmt.Errorf(\"directory %s is not writable: %v\", dir, err)\n}\nos.Remove(probe)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm the target directory is on a read-write mount before running inside containers.","Remove stale `.bak` files left by aborted runs before migrating.","Run migrate outside editors/AV scanners that may lock files, or exclude the workspace from AV.","Check `df -h` for a full filesystem before bulk migrations."],"tags":["go","filesystem","migrate","rename","permissions"],"backgroundTag":"permission-denied","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}