From 3a19f5c165a4ba4ec176539930f51cff59761e4c Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 5 Nov 2023 15:46:51 +0900 Subject: [PATCH 1/3] fix(vite): use `/@fs/` path to support default server/client entry outside of app root --- packages/remix-dev/vite/plugin.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index 7da143fa24a..1b0b84271f2 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -84,6 +84,17 @@ const resolveFileUrl = ( { rootDirectory }: Pick, filePath: string ) => { + if (filePath.includes("/node_modules/")) { + // use "/@fs/" url to workaround the case where remix client/server default entry files live outside of app root, + // for example, when using pnpm, the structure becomes: + // (workspace-root) + // |- node_modules/.pnpm/@remix-run+dev@xxx/node_modules/@remix-run/dev/dist/config/defaults/entry.client.tsx + // |- packages/remix-app/vite.config.ts + // note that, by default, vite allows serving files from workspace root. + // https://vitejs.dev/config/server-options.html#server-fs-allow + return `/@fs` + filePath; + } + let relativePath = path.relative(rootDirectory, filePath); if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) { From 7d9da2988703163f9a57f38f140d082ccf7e38ed Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 5 Nov 2023 16:25:23 +0900 Subject: [PATCH 2/3] chore: changeset --- .changeset/nasty-waves-whisper.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/nasty-waves-whisper.md diff --git a/.changeset/nasty-waves-whisper.md b/.changeset/nasty-waves-whisper.md new file mode 100644 index 00000000000..e84f2325029 --- /dev/null +++ b/.changeset/nasty-waves-whisper.md @@ -0,0 +1,5 @@ +--- +"@remix-run/dev": patch +--- + +fix(vite): use `/@fs/` path to support default server/client entry outside of app root From 60665ffa3b1a107a09cad47f112545daf5425624 Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Mon, 6 Nov 2023 19:40:53 -0500 Subject: [PATCH 3/3] fix(vite): serve any file outside of project root via `/@fs` By default, Vite prevents access to files outside the workspace root (when using workspaces) or outside of the project root (when not using workspaces) unless user explicitly opts into it via Vite's `server.fs.allow`. --- .changeset/nasty-waves-whisper.md | 9 ++++++++- packages/remix-dev/vite/plugin.ts | 22 ++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/.changeset/nasty-waves-whisper.md b/.changeset/nasty-waves-whisper.md index e84f2325029..e47ce6c1318 100644 --- a/.changeset/nasty-waves-whisper.md +++ b/.changeset/nasty-waves-whisper.md @@ -2,4 +2,11 @@ "@remix-run/dev": patch --- -fix(vite): use `/@fs/` path to support default server/client entry outside of app root +fix(vite): Let Vite handle serving files outside of project root via `/@fs` + +This fixes errors when using default client entry or server entry in a pnpm project +where those files may be outside of the project root, but within the workspace root. + +By default, Vite prevents access to files outside the workspace root +(when using workspaces) or outside of the project root (when not using +workspaces) unless user explicitly opts into it via Vite's `server.fs.allow`. diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index 1b0b84271f2..1c3ff65c029 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -84,24 +84,14 @@ const resolveFileUrl = ( { rootDirectory }: Pick, filePath: string ) => { - if (filePath.includes("/node_modules/")) { - // use "/@fs/" url to workaround the case where remix client/server default entry files live outside of app root, - // for example, when using pnpm, the structure becomes: - // (workspace-root) - // |- node_modules/.pnpm/@remix-run+dev@xxx/node_modules/@remix-run/dev/dist/config/defaults/entry.client.tsx - // |- packages/remix-app/vite.config.ts - // note that, by default, vite allows serving files from workspace root. - // https://vitejs.dev/config/server-options.html#server-fs-allow - return `/@fs` + filePath; - } - let relativePath = path.relative(rootDirectory, filePath); + let isWithinRoot = + !relativePath.startsWith("..") && !path.isAbsolute(relativePath); - if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) { - throw new Error( - `Cannot resolve asset path "${filePath}" outside of root directory "${rootDirectory}".` - ); - } + // Vite will prevent serving files outside of the workspace + // unless user explictly opts in with `server.fs.allow` + // https://vitejs.dev/config/server-options.html#server-fs-allow + if (!isWithinRoot) return `/@fs` + filePath; return `/${normalizePath(relativePath)}`; };