Skip to content

Commit

Permalink
fix: handle linkDir existence
Browse files Browse the repository at this point in the history
As nodeModulesDirForPackageInner is asynchronous and contains
several `await` calls, the file can be created after the first
check for existence.

The error thrown is not an instance of Deno.errors.AlreadyExists,
as tested on macOS (OS error 66) and linux (OS error 39)
  • Loading branch information
mxdvl committed Aug 28, 2023
1 parent e0941bd commit ebee61e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
"https://deno.": false,
"https://deno.l": false,
"https://deno.lan": false
},
"editor.defaultFormatter": "denoland.vscode-deno",
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
resolve,
toFileUrl,
} from "https://deno.land/[email protected]/path/mod.ts";
export { copy } from "https://deno.land/[email protected]/fs/mod.ts";
export { copy, exists } from "https://deno.land/[email protected]/fs/mod.ts";
export { basename, extname } from "https://deno.land/[email protected]/path/mod.ts";
export * as JSONC from "https://deno.land/[email protected]/encoding/jsonc.ts";
export { encode as base32Encode } from "https://deno.land/[email protected]/encoding/base32.ts";
Expand Down
12 changes: 4 additions & 8 deletions src/loader_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DenoDir,
dirname,
esbuild,
exists,
fromFileUrl,
join,
} from "../deps.ts";
Expand Down Expand Up @@ -120,23 +121,18 @@ export class NativeLoader implements Loader {

// check if the package is already linked, if so, return the link and skip
// a bunch of work
try {
await Deno.stat(linkDir);
this.#linkDirCache.set(npmPackageId, linkDir);
return linkDir;
} catch {
// directory does not yet exist
}
if (await exists(linkDir)) return linkDir;

// create a temporary directory, recursively hardlink the package contents
// into it, and then rename it to the final location
const tmpDir = await Deno.makeTempDir();

await linkRecursive(packageDir, tmpDir);
try {
await Deno.mkdir(linkDirParent, { recursive: true });
await Deno.rename(tmpDir, linkDir);
} catch (err) {
if (err instanceof Deno.errors.AlreadyExists) {
if (err instanceof Deno.errors.AlreadyExists || await exists(linkDir)) {
// ignore
} else {
throw err;
Expand Down

0 comments on commit ebee61e

Please sign in to comment.