Skip to content

Commit

Permalink
fix: avoid recreating existing, valid symlinks
Browse files Browse the repository at this point in the history
This is a somewhat lazy approach to fixing an issue where pnpm
regenerates symlinks, even if the existing symlink is perfectly fine.
This behavior (of clobbering valid symlinks) can have adverse downstream
effects, like triggering watchers and invalidating caches (the mtime
changes).

This is not the ideal solution, because it incurs the cost of a failed
`readlink` if the symlink does _not_ currently exist (which is always
the case in a fresh pnpm install, and _sometimes_ the case in a pnpm
install that needs to move things around).

But in the happy case—when the node_modules are already mostly correct,
at Discord, we saw a 0.2-0.5s speedup in `pnpm install`. So I think it's
worth doing this, and then considering this function more holistically
if we find time.
  • Loading branch information
stevenpetryk committed Aug 19, 2024
1 parent a87323e commit 92c6298
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ async function forceSymlink (
renameTried?: boolean
}
): Promise<{ reused: boolean, warn?: string }> {
// The happy path: the symlink already exists, and is correct (which is true
// in most already-initialized projects).
try {
const actualPath = await fs.readlink(path)
if (actualPath === target) {
return { reused: true }
}
} catch (e) { /* logic below handles all other cases */ }

try {
await fs.symlink(target, path, symlinkType)
return { reused: false }
Expand Down

0 comments on commit 92c6298

Please sign in to comment.