Repository navigation
fix(start-plugin-core): find Start libraries from Vite's root, not the working directory #8866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9b84e1
fix(start-plugin-core): find Start libraries from Vite's root
Sheraff 5d7cfd4
fix(start-plugin-core): resolve a relative Vite root before crawling …
Sheraff 8d974d4
test(start-plugin-core): leave the temporary working directory before…
Sheraff ec09446
fix(start-plugin-core): crawl Start libraries from the physical Vite …
Sheraff 6791b97
test(start-plugin-core): leave the symlinked directory before removin…
Sheraff e3242ed
Merge branch 'main' into start-plugin-core/vite-root-library-discovery
Sheraff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/start-plugin-core': patch | ||
| --- | ||
|
|
||
| Vite: Start finds the Start libraries it bundles in the SSR build from Vite's root, not the working directory. A `vite build <app>` run from another directory (a monorepo root, a script) no longer leaves them external and uncompiled, where their server fns returned `undefined`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
packages/start-plugin-core/tests/integration/start-vite-root.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| /** | ||
| * Start's Vite plugin reads the app from Vite's root, not from the working | ||
| * directory: `vite build <app>` run from another directory (a monorepo root, | ||
| * a script) builds the app as `vite build` in its directory does. | ||
| */ | ||
| import { execFile } from 'node:child_process' | ||
| import { realpathSync } from 'node:fs' | ||
| import { mkdtemp, rm, symlink, writeFile } from 'node:fs/promises' | ||
| import os from 'node:os' | ||
| import path from 'node:path' | ||
| import { pathToFileURL } from 'node:url' | ||
| import { promisify } from 'node:util' | ||
| import { createBuilder } from 'vite' | ||
| import { expect, onTestFinished, test } from 'vitest' | ||
| import { appFiles, buildStartApp, createStartApp } from './start-app' | ||
|
|
||
| const timeout = 60_000 | ||
|
|
||
| /** | ||
| * Makes a new directory the working directory: a monorepo root, whose | ||
| * package.json is not the app's. (A sibling of the app: the generator moves | ||
| * its temporary files from under the working directory into the app.) | ||
| */ | ||
| async function runElsewhere() { | ||
| const dir = realpathSync(await mkdtemp(path.join(os.tmpdir(), 'start-cwd-'))) | ||
| const cwd = process.cwd() | ||
| // (finish callbacks run last first: leave `dir` before removing it) | ||
| onTestFinished(async () => { | ||
| process.chdir(cwd) | ||
| await rm(dir, { recursive: true, force: true }) | ||
| }) | ||
| await writeFile( | ||
| path.join(dir, 'package.json'), | ||
| JSON.stringify({ name: 'monorepo', private: true }), | ||
| ) | ||
| process.chdir(dir) | ||
| } | ||
|
|
||
| /** The HTML the built server of the app in `root` answers `url` with, in a Node process of its own. */ | ||
| async function serverHtml(root: string, url: string) { | ||
| const server = pathToFileURL(path.join(root, 'dist/server/server.js')) | ||
| const script = `const { default: server } = await import(${JSON.stringify(server.href)}) | ||
| const response = await server.fetch(new Request(${JSON.stringify(url)})) | ||
| process.stdout.write(await response.text()) | ||
| ` | ||
| const { stdout } = await promisify(execFile)( | ||
| process.execPath, | ||
| ['--input-type=module', '--eval', script], | ||
| { cwd: root }, | ||
| ) | ||
| return stdout | ||
| } | ||
|
|
||
| // Start bundles the Start libraries (dependencies with a peer dependency on | ||
| // Start) in the SSR build, so that its compiler compiles their server fns. | ||
| // It looked for them in the working directory's package.json: run elsewhere, | ||
| // the SSR build left the library external, and its server fn, uncompiled, | ||
| // returned nothing. | ||
| test( | ||
| "a build run from another directory: a Start library's server fn runs during SSR", | ||
| async () => { | ||
| const app = await createStartApp( | ||
| appFiles({ | ||
| 'package.json': JSON.stringify({ | ||
| name: 'app', | ||
| type: 'module', | ||
| dependencies: { 'server-fn-lib': '*' }, | ||
| }), | ||
| 'node_modules/server-fn-lib/package.json': JSON.stringify({ | ||
| name: 'server-fn-lib', | ||
| type: 'module', | ||
| exports: './index.js', | ||
| peerDependencies: { '@tanstack/react-start': '*' }, | ||
| }), | ||
| 'node_modules/server-fn-lib/index.js': `import { createServerFn } from '@tanstack/react-start' | ||
| export const libFn = createServerFn().handler(async () => 'MARK_LIB_HANDLER') | ||
| `, | ||
| 'src/routes/index.tsx': `import { createFileRoute } from '@tanstack/react-router' | ||
| import { libFn } from 'server-fn-lib' | ||
| export const Route = createFileRoute('/')({ | ||
| loader: () => libFn(), | ||
| component: () => \`loaded: \${Route.useLoaderData()}\`, | ||
| }) | ||
| `, | ||
| }), | ||
| ) | ||
| await runElsewhere() | ||
| await buildStartApp(app, 'vite') | ||
| expect(await serverHtml(app.root, 'http://localhost/')).toContain( | ||
| 'loaded: MARK_LIB_HANDLER', | ||
| ) | ||
| }, | ||
| timeout, | ||
| ) | ||
|
|
||
| /** | ||
| * A monorepo whose `node_modules` holds the Start library (hoisted), and its | ||
| * app in `apps/web`; returns the app's directory. | ||
| */ | ||
| async function hoistedLibraryMonorepo() { | ||
| const lib = 'node_modules/server-fn-lib' | ||
| const monorepo = await createStartApp({ | ||
| 'package.json': JSON.stringify({ name: 'monorepo', private: true }), | ||
| [`${lib}/package.json`]: JSON.stringify({ | ||
| name: 'server-fn-lib', | ||
| type: 'module', | ||
| exports: './index.js', | ||
| peerDependencies: { '@tanstack/react-start': '*' }, | ||
| }), | ||
| [`${lib}/index.js`]: `import { createServerFn } from '@tanstack/react-start' | ||
| export const libFn = createServerFn().handler(async () => 'MARK_LIB_HANDLER') | ||
| `, | ||
| ...Object.fromEntries( | ||
| Object.entries( | ||
| appFiles({ | ||
| 'package.json': JSON.stringify({ | ||
| name: 'web', | ||
| type: 'module', | ||
| dependencies: { 'server-fn-lib': '*' }, | ||
| }), | ||
| 'src/routes/index.tsx': `import { createFileRoute } from '@tanstack/react-router' | ||
| import { libFn } from 'server-fn-lib' | ||
| export const Route = createFileRoute('/')({ | ||
| loader: () => libFn(), | ||
| component: () => \`loaded: \${Route.useLoaderData()}\`, | ||
| }) | ||
| `, | ||
| }), | ||
| ).map(([file, code]) => [`apps/web/${file}`, code]), | ||
| ), | ||
| }) | ||
| return path.join(monorepo.root, 'apps/web') | ||
| } | ||
|
|
||
| /** Builds with Vite's root as given, as `vite build <root>` does. */ | ||
| async function buildWithRoot(root: string) { | ||
| const { tanstackStart } = await import( | ||
| new URL('../../../react-start/dist/esm/plugin/vite.js', import.meta.url) | ||
| .href | ||
| ) | ||
| const builder = await createBuilder({ | ||
| root, | ||
| configFile: false, | ||
| logLevel: 'error', | ||
| plugins: [tanstackStart()], | ||
| }) | ||
| await builder.buildApp() | ||
| } | ||
|
|
||
| // Vite's root as configured can be relative (`root: '.'`, `vite build .`): | ||
| // the libraries' lookup then stopped at the working directory, short of a | ||
| // library hoisted to a monorepo's `node_modules` above it. | ||
| test( | ||
| "a relative root: a Start library hoisted above the app's directory runs during SSR", | ||
| async () => { | ||
| const web = await hoistedLibraryMonorepo() | ||
| process.chdir(web) | ||
| await buildWithRoot('.') | ||
| expect(await serverHtml(web, 'http://localhost/')).toContain( | ||
| 'loaded: MARK_LIB_HANDLER', | ||
| ) | ||
| }, | ||
| timeout, | ||
| ) | ||
|
|
||
| // A root through a symlink (`vite build "$PWD"` in a symlinked directory): | ||
| // the lookup walked up the symlink's parents, not the app's, where Vite | ||
| // resolves the app's imports from. | ||
| test( | ||
| "a symlinked root: a Start library hoisted above the app's directory runs during SSR", | ||
| async () => { | ||
| const web = await hoistedLibraryMonorepo() | ||
| const linkParent = realpathSync( | ||
| await mkdtemp(path.join(os.tmpdir(), 'start-link-')), | ||
| ) | ||
| const cwd = process.cwd() | ||
| // (finish callbacks run last first: leave the link before removing it) | ||
| onTestFinished(async () => { | ||
| process.chdir(cwd) | ||
| await rm(linkParent, { recursive: true, force: true }) | ||
| }) | ||
| const link = path.join(linkParent, 'web') | ||
| await symlink(web, link, 'junction') | ||
| process.chdir(link) | ||
| await buildWithRoot(link) | ||
| expect(await serverHtml(web, 'http://localhost/')).toContain( | ||
| 'loaded: MARK_LIB_HANDLER', | ||
| ) | ||
| }, | ||
| timeout, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.