Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove file at symlink destination if it cannot be renamed #39

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ function symlinkDir (src: string, dest: string, opts?: { overwrite?: boolean }):
* Creates a symlink. Re-link if a symlink already exists at the supplied
* srcPath. API compatible with [`fs#symlink`](https://nodejs.org/api/fs.html#fs_fs_symlink_srcpath_dstpath_type_callback).
*/
async function forceSymlink (src: string, dest: string, opts?: { overwrite?: boolean }): Promise<{ reused: Boolean, warn?: string }> {
async function forceSymlink (
src: string,
dest: string,
opts?: {
overwrite?: boolean
renameTried?: boolean
}
): Promise<{ reused: Boolean, warn?: string }> {
try {
await fs.symlink(src, dest, symlinkType)
return { reused: false }
Expand All @@ -49,7 +56,7 @@ async function forceSymlink (src: string, dest: string, opts?: { overwrite?: boo
`Details: ${mkdirError}`
throw mkdirError
}
await forceSymlink(src, dest)
await forceSymlink(src, dest, opts)
return { reused: false }
case 'EEXIST':
case 'EISDIR':
Expand All @@ -64,26 +71,35 @@ async function forceSymlink (src: string, dest: string, opts?: { overwrite?: boo
}
}

let linkString
let linkString: string
try {
linkString = await fs.readlink(dest)
} catch (err) {
// Dest is not a link
const parentDir = path.dirname(dest)
const ignore = `.ignored_${path.basename(dest)}`
await renameOverwrite(dest, path.join(parentDir, ignore))
let warn!: string
if (opts?.renameTried) {
// This is needed in order to fix a mysterious bug that sometimes happens on macOS.
// It is hard to reproduce and is described here: https://github.com/pnpm/pnpm/issues/5909#issuecomment-1400066890
await fs.unlink(dest)
warn = `Symlink wanted name was occupied by directory or file. Old entity removed: "${parentDir}${path.sep}{${path.basename(dest)}".`
} else {
const ignore = `.ignored_${path.basename(dest)}`
await renameOverwrite(dest, path.join(parentDir, ignore))
warn = `Symlink wanted name was occupied by directory or file. Old entity moved: "${parentDir}${path.sep}{${path.basename(dest)} => ${ignore}".`
}

return {
...await forceSymlink(src, dest),
warn: `Symlink wanted name was occupied by directory or file. Old entity moved: "${parentDir}${path.sep}{${path.basename(dest)} => ${ignore}}".`,
...await forceSymlink(src, dest, { ...opts, renameTried: true }),
warn,
}
}

if (src === linkString) {
return { reused: true }
}
await fs.unlink(dest)
return await forceSymlink(src, dest)
return await forceSymlink(src, dest, opts)
}

// for backward compatibility
Expand Down