From 8aa05e104d131b2a45c66b9cb7f72962aed6ad78 Mon Sep 17 00:00:00 2001 From: paulaboks Date: Wed, 11 Nov 2026 14:46:51 -0300 Subject: [PATCH] Fix warning with vite dev --- deno.lock | 20 ++++++ packages/fresh/src/app.ts | 3 +- packages/fresh/src/build_cache.ts | 26 ++++++++ packages/fresh/src/dev/dev_build_cache.ts | 9 +-- packages/fresh/src/dev/fs_crawl.ts | 3 +- packages/fresh/src/internals_dev.ts | 3 +- packages/fresh/src/test_utils.ts | 33 ++------- packages/fresh/src/utils.ts | 7 ++ packages/fresh/tests/runtime_imports_test.ts | 70 ++++++++++++++++++++ 9 files changed, 132 insertions(+), 42 deletions(-) create mode 100644 packages/fresh/tests/runtime_imports_test.ts diff --git a/deno.lock b/deno.lock index b6c94bf9..07686385 100644 --- a/deno.lock +++ b/deno.lock @@ -11,6 +11,8 @@ "jsr:@deno/graph@~0.82.3": "0.82.3", "jsr:@deno/loader@0.4": "0.4.0", "jsr:@deno/loader@~0.3.10": "0.3.14", + "jsr:@fresh/build-id@1": "1.0.1", + "jsr:@fresh/core@2.3.3": "2.3.3", "jsr:@marvinh-test/fresh-island@^0.0.3": "0.0.3", "jsr:@marvinh-test/import-json@^0.0.1": "0.0.1", "jsr:@matmen/imagescript@^1.3.0": "1.3.1", @@ -194,6 +196,24 @@ "@deno/loader@0.4.0": { "integrity": "6c1b18cfa18592740613ce79e15625c24268d60dbfc54bebfb5153bf512c536b" }, + "@fresh/build-id@1.0.1": { + "integrity": "12a2ec25fd52ae9ec68c26848a5696cd1c9b537f7c983c7e56e4fb1e7e816c20", + "dependencies": [ + "jsr:@std/encoding@^1.0.10" + ] + }, + "@fresh/core@2.3.3": { + "integrity": "6c2aad199976644352c89a1d601f3ef331b1d24cdadb53ed469324a303598383", + "dependencies": [ + "jsr:@fresh/build-id", + "jsr:@std/fmt@^1.0.8", + "jsr:@std/fs@^1.0.19", + "jsr:@std/html@^1.0.5", + "jsr:@std/http@^1.0.21", + "jsr:@std/net", + "jsr:@std/path@^1.1.2" + ] + }, "@marvinh-test/fresh-island@0.0.3": { "integrity": "6d06b6009b7dfba9bba28e941e03e6ff652c4ef4f2fbfdf4b78741abd6c6c1c6", "dependencies": [ diff --git a/packages/fresh/src/app.ts b/packages/fresh/src/app.ts index 8c3f8655..6db5638a 100644 --- a/packages/fresh/src/app.ts +++ b/packages/fresh/src/app.ts @@ -10,7 +10,7 @@ import { } from "./context.ts"; import { mergePath, type Method, UrlPatternRouter } from "./router.ts"; import type { FreshConfig, ResolvedFreshConfig } from "./config.ts"; -import type { BuildCache } from "./build_cache.ts"; +import { type BuildCache, MockBuildCache } from "./build_cache.ts"; import { HttpError } from "./error.ts"; import { STATUS_TEXT } from "@std/http/status"; import type { LayoutConfig, MaybeLazy, Route, RouteConfig } from "./types.ts"; @@ -29,7 +29,6 @@ import { newNotFoundCmd, newRouteCmd, } from "./commands.ts"; -import { MockBuildCache } from "./test_utils.ts"; // TODO: Completed type clashes in older Deno versions // deno-lint-ignore no-explicit-any diff --git a/packages/fresh/src/build_cache.ts b/packages/fresh/src/build_cache.ts index 630dccbb..650785bc 100644 --- a/packages/fresh/src/build_cache.ts +++ b/packages/fresh/src/build_cache.ts @@ -102,6 +102,32 @@ export class ProdBuildCache implements BuildCache { } } +/** Fallback build cache for when there's no build output (and for tests). */ +export class MockBuildCache implements BuildCache { + #files: FsRouteFile[]; + root = ""; + clientEntry = ""; + islandRegistry: ServerIslandRegistry = new Map(); + features = { errorOverlay: false }; + + constructor(files: FsRouteFile[], mode: "development" | "production") { + this.features.errorOverlay = mode === "development"; + this.#files = files; + } + + getEntryAssets(): string[] { + return []; + } + + getFsRoutes(): Command[] { + return fsItemsToCommands(this.#files); + } + + readFile(_pathname: string): Promise { + return Promise.resolve(null); + } +} + export class IslandPreparer { #namer = new UniqueNamer(); diff --git a/packages/fresh/src/dev/dev_build_cache.ts b/packages/fresh/src/dev/dev_build_cache.ts index 3b1c79bb..d4976d73 100644 --- a/packages/fresh/src/dev/dev_build_cache.ts +++ b/packages/fresh/src/dev/dev_build_cache.ts @@ -8,7 +8,7 @@ import * as pathWin32 from "@std/path/windows"; import { encodeHex } from "@std/encoding/hex"; import { fsAdapter } from "../fs.ts"; import type { FileTransformer } from "./file_transformer.ts"; -import { assertInDir, pathToSpec } from "../utils.ts"; +import { assertInDir, pathToSpec, toPosix } from "../utils.ts"; import type { ResolvedBuildConfig } from "./builder.ts"; import { fsItemsToCommands, type FsRouteFile } from "../fs_routes.ts"; import type { Command } from "../commands.ts"; @@ -17,13 +17,6 @@ import { contentType as getStdContentType } from "@std/media-types/content-type" const WINDOWS_SEPARATOR = pathWin32.SEPARATOR; -/** Normalize a path to use forward slashes so that generated files - * are portable across operating systems (e.g. build on Windows, - * deploy on Linux). */ -export function toPosix(p: string): string { - return p.replaceAll(WINDOWS_SEPARATOR, "/"); -} - export interface MemoryFile { hash: string | null; contentType: string; diff --git a/packages/fresh/src/dev/fs_crawl.ts b/packages/fresh/src/dev/fs_crawl.ts index 5612a84a..d96975b4 100644 --- a/packages/fresh/src/dev/fs_crawl.ts +++ b/packages/fresh/src/dev/fs_crawl.ts @@ -1,6 +1,7 @@ import { type FsAdapter, fsAdapter } from "../fs.ts"; import type { WalkEntry } from "@std/fs/walk"; -import { type FsRouteFileNoMod, toPosix } from "./dev_build_cache.ts"; +import type { FsRouteFileNoMod } from "./dev_build_cache.ts"; +import { toPosix } from "../utils.ts"; import * as path from "@std/path"; import { pathToPattern } from "../router.ts"; import { CommandType } from "../commands.ts"; diff --git a/packages/fresh/src/internals_dev.ts b/packages/fresh/src/internals_dev.ts index bda505f2..10e3e24e 100644 --- a/packages/fresh/src/internals_dev.ts +++ b/packages/fresh/src/internals_dev.ts @@ -7,10 +7,9 @@ export { type IslandModChunk, type PendingStaticFile, prepareStaticFile, - toPosix, writeCompiledEntry, } from "./dev/dev_build_cache.ts"; export { specToName } from "./dev/builder.ts"; -export { pathToSpec, UniqueNamer } from "./utils.ts"; +export { pathToSpec, toPosix, UniqueNamer } from "./utils.ts"; export { updateCheck } from "./dev/update_check.ts"; export { TEST_FILE_PATTERN, UPDATE_INTERVAL } from "./constants.ts"; diff --git a/packages/fresh/src/test_utils.ts b/packages/fresh/src/test_utils.ts index f76dff31..9085fc17 100644 --- a/packages/fresh/src/test_utils.ts +++ b/packages/fresh/src/test_utils.ts @@ -1,13 +1,11 @@ -import { Context, type ServerIslandRegistry } from "./context.ts"; +import { Context } from "./context.ts"; import type { FsAdapter } from "./fs.ts"; -import type { BuildCache, StaticFile } from "./build_cache.ts"; +import { type BuildCache, MockBuildCache } from "./build_cache.ts"; import type { ResolvedFreshConfig } from "./config.ts"; import type { WalkEntry } from "@std/fs/walk"; import { DEFAULT_CONN_INFO } from "./app.ts"; -import type { Command } from "./commands.ts"; -import { fsItemsToCommands, type FsRouteFile } from "./fs_routes.ts"; import * as path from "@std/path"; -import { toPosix } from "./dev/dev_build_cache.ts"; +import { toPosix } from "./utils.ts"; const STUB = {} as unknown as Deno.ServeHandlerInfo; @@ -162,30 +160,7 @@ export async function withTmpDir( }; } -export class MockBuildCache implements BuildCache { - #files: FsRouteFile[]; - root = ""; - clientEntry = ""; - islandRegistry: ServerIslandRegistry = new Map(); - features = { errorOverlay: false }; - - constructor(files: FsRouteFile[], mode: "development" | "production") { - this.features.errorOverlay = mode === "development"; - this.#files = files; - } - - getEntryAssets(): string[] { - return []; - } - - getFsRoutes(): Command[] { - return fsItemsToCommands(this.#files); - } - - readFile(_pathname: string): Promise { - return Promise.resolve(null); - } -} +export { MockBuildCache }; /** * Wrapper around `Deno.test` for integration tests that disables sanitizers. diff --git a/packages/fresh/src/utils.ts b/packages/fresh/src/utils.ts index 96952e34..18550516 100644 --- a/packages/fresh/src/utils.ts +++ b/packages/fresh/src/utils.ts @@ -2,6 +2,13 @@ import * as path from "@std/path"; import type { Lazy, MaybeLazy } from "./types.ts"; import { pathToFileUrl, relativeUrl } from "./file_url.ts"; +/** Normalize a path to use forward slashes so that generated files + * are portable across operating systems (e.g. build on Windows, + * deploy on Linux). */ +export function toPosix(p: string): string { + return p.replaceAll("\\", "/"); +} + export function assertInDir( filePath: string, dir: string, diff --git a/packages/fresh/tests/runtime_imports_test.ts b/packages/fresh/tests/runtime_imports_test.ts new file mode 100644 index 00000000..5918cdb8 --- /dev/null +++ b/packages/fresh/tests/runtime_imports_test.ts @@ -0,0 +1,70 @@ +import { expect } from "@std/expect"; + +// Guards against runtime code importing the esbuild dev builder (`src/dev/`) +// or test helpers. That happened once through `app.ts` -> `test_utils.ts` -> +// `dev/dev_build_cache.ts`: every app then loaded esbuild at startup, and +// Vite projects printed "dynamic import cannot be analyzed" warnings. + +const SRC_URL = new URL("../src/", import.meta.url).href; + +/** Entry points that end up in a user's app at runtime (see deno.json). */ +const RUNTIME_ENTRIES = [ + "mod.ts", + "runtime/shared.ts", + "runtime/client/mod.ts", + "internals.ts", + "compat.ts", +]; + +/** Under `src/dev/` but lightweight and needed by the server renderer. */ +const ALLOWED_DEV_MODULES = ["dev/middlewares/error_overlay/code_frame.ts"]; + +/** Paths under `src/` of every local module in the graph, plus all others. */ +async function moduleGraph( + entry: string, +): Promise<{ local: string[]; other: string[] }> { + const out = await new Deno.Command(Deno.execPath(), { + // Must be a file URL: on Windows a bare `C:\...` path parses as a URL + // with scheme `c:` and the graph comes back empty. + args: ["info", "--json", new URL(entry, SRC_URL).href], + stdout: "piped", + stderr: "piped", + }).output(); + if (!out.success) { + throw new Error(new TextDecoder().decode(out.stderr)); + } + const info = JSON.parse(new TextDecoder().decode(out.stdout)) as { + modules: { specifier: string }[]; + }; + + // Drive letter casing can differ between the two, so compare lowercased. + const prefix = SRC_URL.toLowerCase(); + const local: string[] = []; + const other: string[] = []; + for (const { specifier } of info.modules) { + if (specifier.toLowerCase().startsWith(prefix)) { + local.push(specifier.slice(prefix.length)); + } else { + other.push(specifier); + } + } + return { local, other }; +} + +for (const entry of RUNTIME_ENTRIES) { + Deno.test(`runtime imports - ${entry} doesn't load dev builder or test helpers`, async () => { + const { local, other } = await moduleGraph(entry); + + // Sanity check, so a failed lookup can't pass with an empty graph. + expect(local).toContain(entry); + + const forbidden = [ + ...local.filter((rel) => + rel === "test_utils.ts" || + (rel.startsWith("dev/") && !ALLOWED_DEV_MODULES.includes(rel)) + ), + ...other.filter((spec) => /esbuild/.test(spec)), + ]; + expect(forbidden).toEqual([]); + }); +}