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:
@@ -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(
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user