feat: add error messages for missing build cache (#2551)

This commit is contained in:
Marvin Hagemeister
2024-06-26 15:33:25 +02:00
committed by GitHub
parent 28eca25a42
commit fc820e8965
8 changed files with 89 additions and 12 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
"screenshot": "deno run -A www/utils/screenshot.ts",
"check:types": "deno check src/**/*.ts src/**/*.tsx tests/**/*.ts tests/**/*.tsx update/**/*.ts plugin-tailwindcss/**/*.ts init/**/*.ts",
"ok": "deno fmt --check && deno lint && deno task check:types && deno task test",
"test:www": "deno test -A tests/www/",
"test:www": "deno test -A www/main_test.*",
"release": "deno run -A tools/release.ts"
},
"exclude": ["**/_fresh/*", "**/tmp/*", "*/tests_OLD/**"],
Generated
+23
View File
@@ -12,6 +12,7 @@
"jsr:@std/async@0.223.0": "jsr:@std/async@0.223.0",
"jsr:@std/async@^0.224.1": "jsr:@std/async@0.224.1",
"jsr:@std/cli@^0.224.5": "jsr:@std/cli@0.224.6",
"jsr:@std/collections@^1.0.0-rc.1": "jsr:@std/collections@1.0.0",
"jsr:@std/crypto@1.0.0-rc.1": "jsr:@std/crypto@1.0.0-rc.1",
"jsr:@std/datetime@^0.224.0": "jsr:@std/datetime@0.224.0",
"jsr:@std/encoding@0.213": "jsr:@std/encoding@0.213.1",
@@ -19,6 +20,7 @@
"jsr:@std/expect@^0.224.4": "jsr:@std/expect@0.224.4",
"jsr:@std/fmt@0.221.0": "jsr:@std/fmt@0.221.0",
"jsr:@std/fmt@^0.225.3": "jsr:@std/fmt@0.225.4",
"jsr:@std/front-matter@^0.224.2": "jsr:@std/front-matter@0.224.2",
"jsr:@std/fs@0.223.0": "jsr:@std/fs@0.223.0",
"jsr:@std/fs@^0.229.1": "jsr:@std/fs@0.229.1",
"jsr:@std/html@^0.224.2": "jsr:@std/html@0.224.2",
@@ -34,6 +36,8 @@
"jsr:@std/path@^0.225.2": "jsr:@std/path@0.225.2",
"jsr:@std/semver@^0.224.3": "jsr:@std/semver@0.224.3",
"jsr:@std/streams@^0.224.3": "jsr:@std/streams@0.224.3",
"jsr:@std/toml@^0.224.1": "jsr:@std/toml@0.224.1",
"jsr:@std/yaml@^0.224.1": "jsr:@std/yaml@0.224.2",
"jsr:@zip-js/zip-js@2.7.41": "jsr:@zip-js/zip-js@2.7.41",
"npm:@preact/signals@1.2.3": "npm:@preact/signals@1.2.3_preact@10.22.0",
"npm:@preact/signals@^1.2.3": "npm:@preact/signals@1.2.3_preact@10.22.0",
@@ -104,6 +108,9 @@
"@std/cli@0.224.6": {
"integrity": "98d7e17a70c1c24980baa2b9a511b29e0a460e7a3b8e3d4e220979d77d5f348e"
},
"@std/collections@1.0.0": {
"integrity": "8e4e07ce1df7badae20b80f2678c3ca3bc14cae5514c4e1817a4668ba8b1adf2"
},
"@std/crypto@1.0.0-rc.1": {
"integrity": "5f7bf77275b35bc963a27aa0d83eed2c8243a48f0f34fef9bd660d0e313e9805"
},
@@ -129,6 +136,13 @@
"@std/fmt@0.225.4": {
"integrity": "584c681cf422b70e28959b57e59012823609c087384cbf12d05f67814797fda3"
},
"@std/front-matter@0.224.2": {
"integrity": "e445a33b6d2f5d24ec3a446fa440c4d677129ad19ee7a663ac0d423149017401",
"dependencies": [
"jsr:@std/toml@^0.224.1",
"jsr:@std/yaml@^0.224.1"
]
},
"@std/fs@0.223.0": {
"integrity": "3b4b0550b2c524cbaaa5a9170c90e96cbb7354e837ad1bdaf15fc9df1ae9c31c"
},
@@ -191,6 +205,15 @@
"@std/streams@0.224.3": {
"integrity": "38755b17defdb8a7f617acb7242ea029ac83a5deeb289b5820a3650cb031e2f6"
},
"@std/toml@0.224.1": {
"integrity": "c364b9ca3b18207223c4e2a1f652ef2970ff80967f7575ddf89b6b0565a17148",
"dependencies": [
"jsr:@std/collections@^1.0.0-rc.1"
]
},
"@std/yaml@0.224.2": {
"integrity": "3b647d449084d76ec6292341212efbd4b7c71a2d6db8a9512876eed1e34c3fb2"
},
"@zip-js/zip-js@2.7.41": {
"integrity": "a883116c1b46673de643f9cd3b6619d746271b96cd9a8e2b9d0bb8353d764d3d"
}
+28 -2
View File
@@ -1,7 +1,7 @@
import { expect } from "@std/expect";
import { initProject, InitStep, type MockTTY } from "./init.ts";
import * as path from "@std/path";
import { withBrowser } from "../../tests/test_utils.tsx";
import { getStdOutput, withBrowser } from "../../tests/test_utils.tsx";
import { waitForText } from "../../tests/test_utils.tsx";
import { withChildProcessServer } from "../../tests/test_utils.tsx";
@@ -194,7 +194,7 @@ Deno.test("init - can start dev server", async () => {
});
});
Deno.test("init - can start build project", async () => {
Deno.test("init - can start built project", async () => {
await withTmpDir(async (dir) => {
const mock = mockUserInput({
[InitStep.ProjectName]: ".",
@@ -227,3 +227,29 @@ Deno.test("init - can start build project", async () => {
);
});
});
Deno.test("init - errors on missing build cache in prod", async () => {
await withTmpDir(async (dir) => {
const mock = mockUserInput({
[InitStep.ProjectName]: ".",
});
await initProject(dir, [], {}, mock.tty);
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
const cp = await new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "main.ts"],
stdin: "null",
stdout: "piped",
stderr: "piped",
cwd: dir,
}).output();
const { stderr } = getStdOutput(cp);
expect(cp.code).toEqual(1);
expect(stderr).toMatch(/Found 1 islands, but did not/);
});
});
+4 -1
View File
@@ -171,7 +171,10 @@ export class App<State> {
(request: Request, info?: Deno.ServeHandlerInfo) => Promise<Response>
> {
if (this.#buildCache === null) {
this.#buildCache = await ProdBuildCache.fromSnapshot(this.config);
this.#buildCache = await ProdBuildCache.fromSnapshot(
this.config,
this.#islandRegistry.size,
);
}
if (
+2 -2
View File
@@ -1,5 +1,5 @@
import { expect } from "@std/expect";
import { App, setBuildCache } from "./app.ts";
import { App, getIslandRegistry, setBuildCache } from "./app.ts";
import { FakeServer } from "./test_utils.ts";
import { ProdBuildCache } from "./build_cache.ts";
@@ -429,7 +429,7 @@ Deno.test.ignore("FreshApp - finish setup", async () => {
build: {
outDir: "foo",
},
}),
}, getIslandRegistry(app).size),
);
const server = new FakeServer(await app.handler());
+25 -3
View File
@@ -1,6 +1,7 @@
import * as path from "@std/path";
import type { ResolvedFreshConfig } from "./config.ts";
import { setBuildId } from "./runtime/build_id.ts";
import { DENO_DEPLOYMENT_ID, setBuildId } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors";
export interface FileSnapshot {
generated: boolean;
@@ -28,7 +29,7 @@ export interface BuildCache {
}
export class ProdBuildCache implements BuildCache {
static async fromSnapshot(config: ResolvedFreshConfig) {
static async fromSnapshot(config: ResolvedFreshConfig, islandCount: number) {
const snapshotPath = path.join(config.build.outDir, "snapshot.json");
const staticFiles = new Map<string, FileSnapshot>();
@@ -53,8 +54,29 @@ export class ProdBuildCache implements BuildCache {
const pathname = islands[i];
islandToChunk.set(pathname, snapshot.islands[pathname]);
}
if (!DENO_DEPLOYMENT_ID) {
// deno-lint-ignore no-console
console.log(
`Found snapshot at ${colors.cyan(snapshotPath)}`,
);
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
if ((err instanceof Deno.errors.NotFound)) {
if (islandCount > 0) {
throw new Error(
`Found ${
colors.green(`${islandCount} islands`)
}, but did not find build snapshot at:\n${
colors.red(snapshotPath)
}.\n\nMaybe your forgot to run ${
colors.cyan("deno task build")
} before starting the production server\nor maybe you wanted to run ${
colors.cyan("deno task dev")
} to spin up a development server instead?\n`,
);
}
} else {
throw err;
}
}
+5 -2
View File
@@ -1,4 +1,4 @@
import { App, setBuildCache } from "../src/app.ts";
import { App, getIslandRegistry, setBuildCache } from "../src/app.ts";
import { launch, type Page } from "@astral/astral";
import * as colors from "@std/fmt/colors";
import { type Document, DOMParser, HTMLElement } from "linkedom";
@@ -66,7 +66,10 @@ export async function buildProd(app: App<unknown>) {
app.config.build.outDir = outDir;
const builder = new Builder({});
await builder.build(app);
const cache = await ProdBuildCache.fromSnapshot(app.config);
const cache = await ProdBuildCache.fromSnapshot(
app.config,
getIslandRegistry(app).size,
);
setBuildCache(app, cache);
}
+1 -1
View File
@@ -4,11 +4,11 @@ import { buildProd, withBrowserApp } from "../tests/test_utils.tsx";
import { expect } from "@std/expect";
import { retry } from "@std/async/retry";
await buildProd(app);
const handler = await app.handler();
Deno.test("CORS should not set on GET /fresh-badge.svg", async () => {
const req = new Request("http://localhost/fresh-badge.svg");
await buildProd(app);
const resp = await handler(req);
await resp?.body?.cancel();