-
Notifications
You must be signed in to change notification settings - Fork 522
add automatic port detection for Tauri dev server #695
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
base: main
Are you sure you want to change the base?
add automatic port detection for Tauri dev server #695
Conversation
- Add tauri-dev-auto-port.js for dynamic port selection - Scan ports 5173-5182 to find available port - Update package.json to use auto-port script - Prevent conflicts when running multiple dev instances Features: - Automatic port conflict resolution - Supports multiple simultaneous dev environments - Graceful fallback if all ports occupied - Environment variables set correctly (VITE_PORT, TAURI_MODE) Resolves issue where multiple dev environments couldn't run simultaneously due to hardcoded port configuration
|
|
WalkthroughAdds an npm script and Node.js utility that probe ports 5173–5182, set environment variables (VITE_PORT, TAURI_CLI_PORT, TAURI_DEV_HOST), and launch the Tauri development server with the detected available port. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ 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 |
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
frontend/scripts/tauri-dev-auto-port.js (1)
87-99: Clarify environment variable usage and subprocess invocation.A few observations:
- Line 88: Setting
VITE_PORTonprocess.envhas no effect since it's set after the spawn environment is constructed. However, line 98 does pass it correctly.- The
beforeDevCommandexplicitly sets--port, soVITE_PORTmay be redundant unless consumed elsewhere.- Line 95: Spawning
npm run tauri devcreates a chain (node → npm → tauri → npm → vite). Consider invokingtauridirectly vianpxto reduce indirection.Consider simplifying the spawn:
- const tauri = spawn('npm', ['run', 'tauri', 'dev'], { + const tauri = spawn('npx', ['tauri', 'dev'], { stdio: 'inherit', shell: true, env: { ...process.env, VITE_PORT: port.toString() } });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
frontend/package.json(1 hunks)frontend/scripts/tauri-dev-auto-port.js(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
frontend/scripts/tauri-dev-auto-port.js (1)
scripts/setup.js (1)
__dirname(8-8)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Backend Tests
- GitHub Check: Tauri Build Check (macos-latest, --target aarch64-apple-darwin)
- GitHub Check: Tauri Build Check (ubuntu-22.04)
- GitHub Check: Tauri Build Check (windows-latest)
🔇 Additional comments (3)
frontend/package.json (1)
13-13: LGTM!The new script entry correctly references the auto-port detection script and follows the existing naming conventions.
frontend/scripts/tauri-dev-auto-port.js (2)
39-46: LGTM!The port scanning logic is clean and correctly returns the first available port in the range.
106-109: LGTM!The top-level error handler correctly catches and reports any uncaught errors from the main function.
- Use --dev-url and --port CLI flags - Remove config file modification to prevent git conflicts - Improve error handling with specific warnings - Bind explicitly to localhost for security Addresses CodeRabbit feedback
|
|
1 similar comment
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
frontend/scripts/tauri-dev-auto-port.js(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Backend Tests
- GitHub Check: Tauri Build Check (macos-latest, --target aarch64-apple-darwin)
- GitHub Check: Tauri Build Check (ubuntu-22.04)
- GitHub Check: Tauri Build Check (windows-latest)
🔇 Additional comments (5)
frontend/scripts/tauri-dev-auto-port.js (5)
1-6: LGTM! Well-defined constants.The port range (5173-5182) provides sufficient options for multiple simultaneous dev instances while staying within Vite's typical port range.
13-33: LGTM! Robust port checking implementation.The error handling improvements from the previous review have been properly implemented. The explicit localhost binding and detailed error logging make this function reliable.
39-46: LGTM! Clean port discovery logic.The sequential scan approach is straightforward and the null return value provides a clear signal when all ports are occupied.
104-107: LGTM! Proper top-level error handling.The catch block ensures unhandled errors in main() are logged and result in a non-zero exit code.
69-92: No issues found with the Tauri CLI configuration.The flags and environment variables are correctly configured:
--dev-url http://localhost:${port}points Tauri to your external Vite dev server--port ${port}configures Tauri's built-in static file server (a separate feature)VITE_PORTtells Vite which port to useTAURI_CLI_PORTand--portare redundant but harmless (env var alternative to the flag)TAURI_DEV_HOST='localhost'is automatically managed by Tauri CLI; explicitly setting it is benignThese are not conflicting configurations—they serve distinct purposes for the dev server setup.
| if (!port) { | ||
| console.error( | ||
| `\n❌ No available ports found in range ${PORT_RANGE_START}-${PORT_RANGE_END}`, | ||
| ); | ||
| console.error( | ||
| 'Please free up a port or manually configure the port in tauri.conf.json', | ||
| ); | ||
| process.exit(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the error message to reflect the current approach.
The error message suggests manually configuring tauri.conf.json, but the PR's goal is to avoid modifying that file. Consider updating the message to suggest alternatives like closing other dev servers or adjusting the port range in this script.
Apply this diff:
if (!port) {
console.error(
`\n❌ No available ports found in range ${PORT_RANGE_START}-${PORT_RANGE_END}`,
);
console.error(
- 'Please free up a port or manually configure the port in tauri.conf.json',
+ 'Please close other dev servers or expand the PORT_RANGE in this script',
);
process.exit(1);
}🤖 Prompt for AI Agents
In frontend/scripts/tauri-dev-auto-port.js around lines 56 to 63, the error text
incorrectly tells users to edit tauri.conf.json; update the messages to reflect
the new non-invasive approach by telling users to close other running dev
servers or adjust the PORT_RANGE_START/PORT_RANGE_END in this script (or run
with a different port), and replace the two console.error calls accordingly so
they guide users to those alternatives instead of editing tauri.conf.json.
Added tauri-dev-auto-port.js for dynamic port selection
Features:
Resolves issue where multiple dev environments couldn't run simultaneously due to hardcoded port configuration
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.