fix: switch to nodeModulesDir: "manual" (#3588)

The `"auto"` value causes all kinds of race conditions between various
processes trying to auto install their view of dependencies.
This commit is contained in:
Marvin Hagemeister
2025-10-30 14:50:49 +01:00
committed by GitHub
parent 45bb094bd6
commit 5e94dfe9fd
6 changed files with 129 additions and 28 deletions
+14 -1
View File
@@ -12,7 +12,20 @@
"./internal-dev": "./src/internals_dev.ts"
},
"imports": {
"@deno/esbuild-plugin": "jsr:@deno/esbuild-plugin@^1.2.0",
"@opentelemetry/api": "npm:@opentelemetry/api@^1.9.0",
"@std/encoding": "jsr:@std/encoding@^1.0.10",
"@std/fmt": "jsr:@std/fmt@^1.0.8",
"@types/node": "npm:@types/node@^24.2.1"
"@std/fs": "jsr:@std/fs@^1.0.19",
"@std/html": "jsr:@std/html@^1.0.5",
"@std/http": "jsr:@std/http@^1.0.21",
"@std/jsonc": "jsr:@std/jsonc@^1.0.2",
"@std/media-types": "jsr:@std/media-types@^1.1.0",
"@std/path": "jsr:@std/path@^1.1.2",
"@std/semver": "jsr:@std/semver@^1.0.6",
"@std/uuid": "jsr:@std/uuid@^1.0.9",
"@types/node": "npm:@types/node@^24.2.1",
"preact": "npm:preact@^10.27.2",
"preact-render-to-string": "npm:preact-render-to-string@^6.6.3"
}
}
+11 -1
View File
@@ -86,6 +86,7 @@ export async function initProject(
builder?: boolean | null;
help?: boolean | null;
h?: boolean | null;
skipInstall?: boolean | null;
} = {},
): Promise<void> {
const freshVersion = await getLatestVersion("@fresh/core", FRESH_VERSION);
@@ -559,7 +560,7 @@ if (Deno.args.includes("build")) {
}
const denoJson = {
nodeModulesDir: "auto",
nodeModulesDir: "manual",
tasks: {
check: "deno fmt --check . && deno lint . && deno check",
dev: "deno run -A --watch=static/,routes/ dev.ts",
@@ -763,6 +764,15 @@ This will watch the project directory and restart as necessary.`;
}
}
if (!flags.skipInstall) {
console.log("Installing dependencies...");
await new Deno.Command(Deno.execPath(), {
cwd: unresolvedDirectory,
args: ["install"],
}).output();
console.log("Installing dependencies...%cdone!", "color: green");
}
// Specifically print unresolvedDirectory, rather than resolvedDirectory in order to
// not leak personal info (e.g. `/Users/MyName`)
console.log(
+74 -18
View File
@@ -16,6 +16,10 @@ import { stub } from "@std/testing/mock";
// deno-lint-ignore no-explicit-any
(globalThis as any).INIT_TEST = true;
const testInitProject: typeof initProject = async (cwd, input, flags = {}) => {
return await initProject(cwd, input, { ...flags, skipInstall: true });
};
function stubPrompt(result: string) {
return stub(globalThis, "prompt", () => result);
}
@@ -49,15 +53,67 @@ async function patchProject(dir: string): Promise<void> {
const jsonPath = path.join(dir, "deno.json");
const json = JSON.parse(await Deno.readTextFile(jsonPath));
json.workspace = [];
// See https://github.com/denoland/deno/issues/27313
// json.links = [path.fromFileURL(new URL("../..", import.meta.url))];
json.links = [new URL("../../fresh", import.meta.url).href];
const linkedDir = path.join(dir, "_linked");
try {
await Deno.mkdir(linkedDir, { recursive: true });
} catch (err) {
if (!(err instanceof Deno.errors.AlreadyExists)) {
throw err;
}
}
const ignore = /[/\\]+(.|[^/\\]+_test\..*|tests[^/\\])/;
await copyRecursive(
path.join(import.meta.dirname!, "..", "..", "fresh"),
path.join(linkedDir, "fresh"),
ignore,
);
await copyRecursive(
path.join(import.meta.dirname!, "..", "..", "build-id"),
path.join(linkedDir, "build-id"),
ignore,
);
json.workspace = ["./_linked/*"];
// assert with this stricter rule, before adding it to initialized projects
json.lint.rules.include = ["verbatim-module-syntax"];
await Deno.writeTextFile(jsonPath, JSON.stringify(json, null, 2) + "\n");
const { code } = await new Deno.Command(Deno.execPath(), {
cwd: dir,
args: ["install"],
}).output();
expect(code).toEqual(0);
}
async function copyRecursive(
from: string,
to: string,
ignore?: RegExp,
): Promise<void> {
for await (const entry of Deno.readDir(from)) {
const source = path.join(from, entry.name);
const target = path.join(to, entry.name);
if (ignore && ignore.test(source)) continue;
if (entry.isFile) {
try {
await Deno.mkdir(to, { recursive: true });
} catch (err) {
if (!(err instanceof Deno.errors.AlreadyExists)) {
throw err;
}
}
await Deno.copyFile(source, target);
} else if (entry.isDirectory) {
await copyRecursive(source, target);
}
}
}
async function expectProjectFile(dir: string, pathname: string) {
@@ -91,10 +147,10 @@ async function readProjectFile(dir: string, pathname: string): Promise<string> {
Deno.test("init - show help", async () => {
using logs = stubLogs();
await initProject("", [], { help: true });
await testInitProject("", [], { help: true });
const args = logs.calls.flatMap((c) => c.args);
const out = args.join("\n");
await initProject("", [], { h: true });
await testInitProject("", [], { h: true });
expect(out).toBe(HELP_TEXT);
expect(args).toEqual(logs.calls.flatMap((c) => c.args).slice(args.length));
@@ -105,7 +161,7 @@ Deno.test("init - new project", async () => {
using _promptStub = stubPrompt("fresh-init");
using _confirmStub = stubConfirm();
await initProject(tmp.dir, [], { builder: true });
await testInitProject(tmp.dir, [], { builder: true });
});
Deno.test("init - create project dir", async () => {
@@ -113,7 +169,7 @@ Deno.test("init - create project dir", async () => {
const dir = tmp.dir;
using _promptStub = stubPrompt("fresh-init");
using _confirmStub = stubConfirm();
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
const root = path.join(dir, "fresh-init");
await expectProjectFile(root, "deno.json");
@@ -130,7 +186,7 @@ Deno.test("init - with tailwind", async () => {
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
const css = await readProjectFile(dir, "static/styles.css");
expect(css).toMatch(/@import "tailwindcss"/);
@@ -148,7 +204,7 @@ Deno.test("init - with vscode", async () => {
using _confirmStub = stubConfirm({
[CONFIRM_VSCODE_MESSAGE]: true,
});
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
await expectProjectFile(dir, ".vscode/settings.json");
await expectProjectFile(dir, ".vscode/extensions.json");
@@ -164,7 +220,7 @@ Deno.test({
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
@@ -190,7 +246,7 @@ Deno.test(
[CONFIRM_TAILWIND_MESSAGE]: true,
});
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
@@ -216,7 +272,7 @@ Deno.test({
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
@@ -239,7 +295,7 @@ Deno.test("init - can start built project", async () => {
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
@@ -271,7 +327,7 @@ Deno.test("init - errors on missing build cache in prod", async () => {
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], { builder: true });
await testInitProject(dir, [], { builder: true });
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
@@ -297,7 +353,7 @@ Deno.test.ignore("init - vite dev server", async () => {
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await testInitProject(dir, [], {});
await expectProjectFile(dir, "vite.config.ts");
await expectNotProjectFile(dir, "dev.ts");
@@ -322,7 +378,7 @@ Deno.test.ignore("init - vite build", async () => {
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await testInitProject(dir, [], {});
await expectProjectFile(dir, "vite.config.ts");
@@ -356,7 +412,7 @@ Deno.test("init - with vite", async () => {
using _confirmStub = stubConfirm({
[CONFIRM_VITE_MESSAGE]: true,
});
await initProject(dir, [], {});
await testInitProject(dir, [], {});
await expectProjectFile(dir, "vite.config.ts");
await expectNotProjectFile(dir, "dev.ts");