-
Notifications
You must be signed in to change notification settings - Fork 5k
bake: resolve app.root against the cwd and require it to be a string #39188
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84cffc3
bake: resolve app.root against the cwd and require it to be a string
robobun f2b2364
ci: retrigger
robobun 6806c54
bake: shorten the root invariant comment
robobun 0fd82fb
bake: drop the root comment, the consumers assert the invariant
robobun c3f6ba5
bake: resolve app.root against top_level_dir, the base the framework …
robobun 858f564
bake: one line doc on resolve_root
robobun 6b8450e
bake: name the path limit in the app.root error; cover unset and empt…
robobun 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
Some comments aren't visible on the classic Files Changed page.
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
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,108 @@ | ||
| // Parsing of the `app` option of `Bun.serve({ app })` (src/runtime/bake/bake_body.rs, | ||
| // UserOptions::from_js). The dev server strips `app.root` off absolute file paths to build | ||
| // route patterns and module IDs, so the value the user passes has to be resolved against | ||
| // the cwd and normalized before it gets there. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| const appFiles = { | ||
| "server.ts": `export function render(req, meta) { return meta.pageModule.default(); }`, | ||
| "routes/index.ts": `export default () => new Response("index route");`, | ||
| }; | ||
|
|
||
| const framework = `{ | ||
| fileSystemRouterTypes: [{ root: "routes", style: "nextjs-pages", serverEntryPoint: "./server.ts" }], | ||
| }`; | ||
|
|
||
| describe("app.root", () => { | ||
| // Every spelling below names the cwd, which is also what `root` defaults to. | ||
| test.concurrent.each([".", "./", "routes/..", "<cwd>/"])("routes are served when root is %j", async spelling => { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| using dir = tempDir("bake-app-root", { | ||
| ...appFiles, | ||
| "serve.ts": ` | ||
| const root = process.argv[2].replace("<cwd>", process.cwd()); | ||
| const server = Bun.serve({ | ||
| port: 0, | ||
| development: true, | ||
| app: { framework: ${framework}, root }, | ||
| fetch: () => new Response("fallback"), | ||
| }); | ||
| const res = await fetch(server.url); | ||
| console.log(JSON.stringify({ status: res.status, body: await res.text() })); | ||
| await server.stop(true); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "serve.ts", spelling], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| const line = stdout.split("\n").find(l => l.startsWith("{")); | ||
| expect(line, stderr).toBeDefined(); | ||
| expect(JSON.parse(line!)).toEqual({ status: 200, body: "index route" }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test("values that cannot be used as a root are rejected while parsing the options", async () => { | ||
|
robobun marked this conversation as resolved.
Outdated
|
||
| using dir = tempDir("bake-app-root-invalid", { | ||
| ...appFiles, | ||
| "check.ts": ` | ||
| const framework = ${framework}; | ||
| const serverComponents = extra => ({ | ||
| framework: { ...framework, serverComponents: { separateSSRGraph: false, ...extra } }, | ||
| }); | ||
| const apps = { | ||
| "root: null": { framework, root: null }, | ||
| "root: 123": { framework, root: 123 }, | ||
| "root: 100k chars": { framework, root: Buffer.alloc(100_000, "a").toString() }, | ||
| // The same string check applies to the other optional string options. | ||
| "plugins[0].name: 123": { framework, plugins: [{ name: 123, setup() {} }] }, | ||
| "serverComponents.serverRuntimeImportSource: 123": serverComponents({ serverRuntimeImportSource: 123 }), | ||
| "serverComponents.serverRegisterClientReferenceExport: 123": serverComponents({ | ||
| serverRuntimeImportSource: "./server.ts", | ||
| serverRegisterClientReferenceExport: 123, | ||
| }), | ||
| }; | ||
|
|
||
| const results = {}; | ||
| for (const [name, app] of Object.entries(apps)) { | ||
| try { | ||
| const server = Bun.serve({ port: 0, development: true, app, fetch: () => new Response("") }); | ||
| server.stop(true); | ||
| results[name] = "accepted"; | ||
| } catch (e) { | ||
| results[name] = e.message; | ||
| } | ||
| } | ||
| console.log(JSON.stringify(results)); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "check.ts"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).toBe(""); | ||
| expect(JSON.parse(stdout)).toEqual({ | ||
| "root: null": "accepted", | ||
| "root: 123": 'The "root" property must be of type string, got number', | ||
| "root: 100k chars": "'app.root' is too long", | ||
| "plugins[0].name: 123": 'The "name" property must be of type string, got number', | ||
| "serverComponents.serverRuntimeImportSource: 123": | ||
| 'The "serverRuntimeImportSource" property must be of type string, got number', | ||
| "serverComponents.serverRegisterClientReferenceExport: 123": | ||
| 'The "serverRegisterClientReferenceExport" property must be of type string, got number', | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); | ||
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.