fix(nuxi): keep cwd at project root during preview#1235
fix(nuxi): keep cwd at project root during preview#1235Davidson3556 wants to merge 2 commits intonuxt:mainfrom
Conversation
📦 Bundle Size Comparison📈 nuxi
📈 nuxt-cli
➡️ create-nuxt
|
commit: |
📝 WalkthroughWalkthroughThe preview command in the Nuxi CLI is modified to execute with a working directory set to the provided project root ( Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1235 +/- ##
=======================================
Coverage ? 45.42%
=======================================
Files ? 46
Lines ? 1070
Branches ? 311
=======================================
Hits ? 486
Misses ? 475
Partials ? 109 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/nuxi/src/commands/preview.ts (1)
154-157: Splitting command by space may fail for paths containing spaces.The
.split(' ')approach assumes no spaces in the command path or arguments. While typical Nitro commands likenode server/index.mjsare safe, paths with spaces would break. Consider using a shell-aware parser if this becomes an issue in practice.💡 Optional: More robust parsing (if needed later)
If paths with spaces become a concern, consider using a proper shell parsing library or storing command and args separately in
nitro.json:- const [cmd, ...cmdArgs] = nitroJSON.commands.preview.split(' ') + // If nitroJSON provides structured commands in future: + // const { cmd, args: cmdArgs } = nitroJSON.commands.preview + // For now, simple split is acceptable for typical node commands + const [cmd, ...cmdArgs] = nitroJSON.commands.preview.split(' ')🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/nuxi/src/commands/preview.ts` around lines 154 - 157, The preview command currently splits nitroJSON.commands.preview by simple spaces which breaks when the command or its args contain spaces; update the parsing logic that assigns const [cmd, ...cmdArgs] = nitroJSON.commands.preview.split(' ') to use a shell-aware parser (e.g., shell-quote or a small quoted-token parser) so quoted paths remain intact, then map the resulting cmdArgs when computing resolvedCmdArgs (the existsSync(join(outputPath, arg)) ? join(outputPath, arg) : arg logic) as before; ensure the primary command token (cmd) and subsequent args (cmdArgs/resolvedCmdArgs) are produced by the new parser so paths with spaces are preserved.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/nuxi/src/commands/preview.ts`:
- Around line 154-157: The preview command currently splits
nitroJSON.commands.preview by simple spaces which breaks when the command or its
args contain spaces; update the parsing logic that assigns const [cmd,
...cmdArgs] = nitroJSON.commands.preview.split(' ') to use a shell-aware parser
(e.g., shell-quote or a small quoted-token parser) so quoted paths remain
intact, then map the resulting cmdArgs when computing resolvedCmdArgs (the
existsSync(join(outputPath, arg)) ? join(outputPath, arg) : arg logic) as
before; ensure the primary command token (cmd) and subsequent args
(cmdArgs/resolvedCmdArgs) are produced by the new parser so paths with spaces
are preserved.
|
The runtimes.spec.ts failures on Windows are unrelated to this change — they're caused by a port conflict on the CI runner (port 3100 timeout). The same test suite passes for bun and deno runtimes, and the preview test in commands.spec.ts passes cleanly. |
🔗 Linked issue
resolves nuxt/nuxt#34397
📚 Description
nuxt previewwas spawning the server withcwdset to the.outputdirectory,which means
process.cwd()inside server handlers returns.outputinstead ofthe project root. This is inconsistent with running
node .output/server/index.mjsdirectly, which keeps the cwd at project root.
The fix keeps
cwdas the project root when spawning the preview server, andresolves the server entrypoint args to absolute paths using
outputPathso theywork without needing a chdir.