fix: normalize Windows paths in generated snapshot and server entry (#3727)
## Summary - When building on Windows, `path.relative()` produces backslash separators (`\`) that get hardcoded into the generated `snapshot.js` and `server.js` files - If the bundle is then deployed to a Linux server (e.g. Windows CI → Linux prod), those paths break at runtime - Fix: add a `toPosix()` helper that normalizes all paths written into generated files to use forward slashes, applied in three places: - `root` path passed to `generateServerEntry()` - `filePath` in `prepareStaticFile()` (from `path.relative()`) - All path options inside `generateServerEntry()` (root, serverEntry, snapshotSpecifier) Closes #3513 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
6dd2c97fb2
commit
d6fc1d08a8
@@ -17,6 +17,13 @@ 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). */
|
||||
function toPosix(p: string): string {
|
||||
return p.replaceAll(WINDOWS_SEPARATOR, "/");
|
||||
}
|
||||
|
||||
export interface MemoryFile {
|
||||
hash: string | null;
|
||||
contentType: string;
|
||||
@@ -378,7 +385,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
|
||||
await Deno.writeTextFile(
|
||||
path.join(outDir, "server.js"),
|
||||
generateServerEntry({
|
||||
root: appPath,
|
||||
root: toPosix(appPath),
|
||||
serverEntry: pathToSpec(outDir, this.#config.serverEntry),
|
||||
snapshotSpecifier: "./snapshot.js",
|
||||
}),
|
||||
@@ -550,9 +557,11 @@ export async function prepareStaticFile(
|
||||
return {
|
||||
name: encodedPathname,
|
||||
hash,
|
||||
filePath: path.isAbsolute(item.filePath)
|
||||
? path.relative(outDir, item.filePath)
|
||||
: item.filePath,
|
||||
filePath: toPosix(
|
||||
path.isAbsolute(item.filePath)
|
||||
? path.relative(outDir, item.filePath)
|
||||
: item.filePath,
|
||||
),
|
||||
contentType: getContentType(item.filePath),
|
||||
};
|
||||
}
|
||||
@@ -564,22 +573,24 @@ export function generateServerEntry(
|
||||
snapshotSpecifier: string;
|
||||
},
|
||||
): string {
|
||||
let rootPath = `path.join(import.meta.dirname, ${
|
||||
JSON.stringify(options.root)
|
||||
})`;
|
||||
if (path.isAbsolute(options.root)) {
|
||||
const root = toPosix(options.root);
|
||||
const serverEntry = toPosix(options.serverEntry);
|
||||
const snapshotSpecifier = toPosix(options.snapshotSpecifier);
|
||||
|
||||
let rootPath = `path.join(import.meta.dirname, ${JSON.stringify(root)})`;
|
||||
if (path.isAbsolute(root)) {
|
||||
// deno-lint-ignore no-console
|
||||
console.warn(
|
||||
`WARN: using absolute root path in snapshot: "${options.root}"`,
|
||||
`WARN: using absolute root path in snapshot: "${root}"`,
|
||||
);
|
||||
|
||||
rootPath = JSON.stringify(options.root);
|
||||
rootPath = JSON.stringify(root);
|
||||
}
|
||||
|
||||
return `${EDIT_WARNING}
|
||||
import { setBuildCache, ProdBuildCache, path } from "fresh/internal";
|
||||
import * as snapshot from "${options.snapshotSpecifier}";
|
||||
import { app } from "${options.serverEntry}";
|
||||
import * as snapshot from "${snapshotSpecifier}";
|
||||
import { app } from "${serverEntry}";
|
||||
|
||||
const root = ${rootPath};
|
||||
setBuildCache(app, new ProdBuildCache(root, snapshot), "production");
|
||||
|
||||
Reference in New Issue
Block a user