Skip to content

Commit 0a3ba71

Browse files
author
Eric Wheeler
committed
dev: dynamic Vite port detection for webview development
Implements a solution for the Vite port collision issue that allows easier development of Roo across multiple instances of VSCode in different Roo repository directories. This may also fix crashes and strange behavior between multiple running instances that would otherwise create port conflicts. This is solved using the following automated process: - When Vite automatically selects an alternative port, a custom plugin automatically writes the port to a '.vite-port' file in the repository root - ClineProvider automatically reads the port from this file, falling back gracefully to port 5173 if the file doesn't exist - No user intervention is necessary as the entire process is handled automatically - Added detailed logging for debugging - Added .vite-port to .gitignore The extension now connects to the correct Vite development server port automatically, even when the default port (5173) is already in use. Signed-off-by: Eric Wheeler <[email protected]>
1 parent 7261efb commit 0a3ba71

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ docs/_site/
3232

3333
#Logging
3434
logs
35+
36+
# Vite development
37+
.vite-port

src/core/webview/ClineProvider.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,26 @@ export class ClineProvider extends EventEmitter<ClineProviderEvents> implements
579579
}
580580

581581
private async getHMRHtmlContent(webview: vscode.Webview): Promise<string> {
582-
const localPort = "5173"
582+
// Try to read the port from the file
583+
let localPort = "5173" // Default fallback
584+
try {
585+
const fs = require("fs")
586+
const path = require("path")
587+
const portFilePath = path.resolve(__dirname, "../.vite-port")
588+
589+
if (fs.existsSync(portFilePath)) {
590+
localPort = fs.readFileSync(portFilePath, "utf8").trim()
591+
console.log(`[ClineProvider:Vite] Using Vite server port from ${portFilePath}: ${localPort}`)
592+
} else {
593+
console.log(
594+
`[ClineProvider:Vite] Port file not found at ${portFilePath}, using default port: ${localPort}`,
595+
)
596+
}
597+
} catch (err) {
598+
console.error("[ClineProvider:Vite] Failed to read Vite port file:", err)
599+
// Continue with default port if file reading fails
600+
}
601+
583602
const localServerUrl = `localhost:${localPort}`
584603

585604
// Check if local dev server is running.

webview-ui/vite.config.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
import path from "path"
2+
import fs from "fs"
23

34
import { defineConfig } from "vite"
45
import react from "@vitejs/plugin-react"
56
import tailwindcss from "@tailwindcss/vite"
67

8+
// Custom plugin to write the server port to a file
9+
const writePortToFile = () => {
10+
return {
11+
name: "write-port-to-file",
12+
configureServer(server) {
13+
// Write the port to a file when the server starts
14+
server.httpServer?.once("listening", () => {
15+
const address = server.httpServer.address()
16+
const port = typeof address === "object" && address ? address.port : null
17+
18+
if (port) {
19+
// Write to a file in the project root
20+
const portFilePath = path.resolve(__dirname, "../.vite-port")
21+
fs.writeFileSync(portFilePath, port.toString())
22+
console.log(`[Vite Plugin] Server started on port ${port}`)
23+
console.log(`[Vite Plugin] Port information written to ${portFilePath}`)
24+
} else {
25+
console.warn("[Vite Plugin] Could not determine server port")
26+
}
27+
})
28+
},
29+
}
30+
}
31+
732
// https://vitejs.dev/config/
833
export default defineConfig({
9-
plugins: [react(), tailwindcss()],
34+
plugins: [react(), tailwindcss(), writePortToFile()],
1035
resolve: {
1136
alias: {
1237
"@": path.resolve(__dirname, "./src"),

0 commit comments

Comments
 (0)