Add server running status to vite-port-demo info hook#33
Conversation
Show whether the dev server is running or stopped next to the URL using lsof to check if the port is listening. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis change enhances the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@examples/vite-port-demo/scripts/show-info.sh`:
- Around line 16-25: The is_port_running check currently uses lsof -i ":$1"
which matches non-listening endpoints and hides lsof absence; update
is_port_running to first verify lsof exists (command -v lsof) and if missing
either error out or fall back to ss, and when using lsof restrict to listening
TCP sockets with flags like -iTCP -sTCP:LISTEN -P -n to avoid name-resolution
and false positives; update format_status to rely on the corrected
is_port_running behavior.
🧹 Nitpick comments (1)
examples/vite-port-demo/scripts/show-info.sh (1)
12-15: Emit ANSI only on TTY and preferprintfoverecho -e.
echo -eisn’t portable, and raw ANSI escapes are noisy when output is piped or logged. Consider TTY-gating the ANSI codes and usingprintf.♻️ Suggested update
-# ANSI codes for bold -BOLD='\033[1m' -RESET='\033[0m' +# ANSI codes for bold (only when stdout is a TTY) +if [ -t 1 ]; then + BOLD=$'\033[1m' + RESET=$'\033[0m' +else + BOLD='' + RESET='' +fi @@ - echo -e "(${BOLD}running${RESET})" + printf '(%srunning%s)' "$BOLD" "$RESET" @@ - echo "(stopped)" + printf '(stopped)' @@ -echo -e "URL: http://localhost:$PORT $(format_status "$PORT")" +printf 'URL: http://localhost:%s %s\n' "$PORT" "$(format_status "$PORT")"Also applies to: 21-32
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
examples/vite-port-demo/scripts/show-info.sh
🧰 Additional context used
🧬 Code graph analysis (1)
examples/vite-port-demo/scripts/show-info.sh (1)
examples/vite-port-demo/vite.config.js (1)
env(3-10)
✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.
| # Check if a port is listening | ||
| is_port_running() { | ||
| lsof -i ":$1" >/dev/null 2>&1 | ||
| } | ||
|
|
||
| # Format status with optional bold | ||
| format_status() { | ||
| local port=$1 | ||
| if is_port_running "$port"; then | ||
| echo -e "(${BOLD}running${RESET})" |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
cat -n examples/vite-port-demo/scripts/show-info.sh | head -40Repository: agarcher/wt
Length of output: 1008
🏁 Script executed:
# Verify lsof behavior with different flags
# Test what flags work for filtering LISTEN sockets
man lsof 2>/dev/null | grep -A 5 "LISTEN" || echo "Man pages not available, will use online documentation"Repository: agarcher/wt
Length of output: 109
🌐 Web query:
lsof -i flag behavior matching outbound connections vs listening ports
💡 Result:
Short answer
- lsof -i selects Internet (network) sockets; -i :port or -i
@host:port matches sockets whose local OR remote address uses that port/address. [1][2] - That means lsof -i :34002 will show both a process LISTENing on 34002 and any established/outbound socket whose local (ephemeral) or remote endpoint is 34002. [1][3]
- To restrict to listening sockets use protocol/state filters: lsof -iTCP -sTCP:LISTEN (or lsof -i -sTCP:LISTEN). To restrict to TCP-only use -iTCP; to see only established use -sTCP:ESTABLISHED. [3][4]
Why outbound connections appear
- An outbound TCP connection has a local ephemeral port. lsof reports that socket with its local:remote pair, so the ephemeral local port can appear as “:34002” and will prevent another process from binding that same port while the socket is in use. This is normal OS behavior. [5]
Useful examples
- Show all Internet sockets: sudo lsof -i. [1]
- Show sockets using port 80 (local or remote): sudo lsof -i :80. [1][6]
- Show only TCP listeners: sudo lsof -iTCP -sTCP:LISTEN -n -P. [3][7]
- Show only established TCP connections: sudo lsof -iTCP -sTCP:ESTABLISHED -n -P. [3]
Sources
[1] lsof manual/FAQ and LinuxCommandLibrary.
[2] lsof options doc (readthedocs).
[3] lsof manpage (man7).
[4] lsof manpage examples for -sTCP:LISTEN.
[5] ServerFault Q&A explaining outbound ephemeral ports blocking binds.
[6] lsof tutorial / examples.
[7] Practical examples from lsof docs.
🌐 Web query:
lsof -sTCP:LISTEN filter documentation
💡 Result:
Summary: -s selects socket state filters. Syntax: -sPROTO:state-list (protocol name, colon, comma/space-separated states). Use with -i to restrict to network files. You can negate states with ^. Example: lsof -iTCP -sTCP:LISTEN lists only TCP sockets in LISTEN state; lsof -iUDP -sUDP:^Idle lists UDP sockets excluding Idle. State names vary by OS. [1][2]
Sources:
- lsof manpage (state filter syntax, need for -i, negation, examples). [1]
- lsof usage examples (common examples including -sTCP:LISTEN). [2]
Tighten lsof check and handle missing dependency.
lsof -i ":$1" matches both listening sockets and outbound connections using that port as a local or remote endpoint, producing false "running" statuses. Also, if lsof is not installed, the check silently fails (stderr suppressed) and the script incorrectly reports "stopped." Restrict to listening sockets and verify lsof availability.
🔧 Suggested update
is_port_running() {
- lsof -i ":$1" >/dev/null 2>&1
+ command -v lsof >/dev/null 2>&1 || return 1
+ lsof -iTCP:"$1" -sTCP:LISTEN -P -n >/dev/null 2>&1
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| # Check if a port is listening | |
| is_port_running() { | |
| lsof -i ":$1" >/dev/null 2>&1 | |
| } | |
| # Format status with optional bold | |
| format_status() { | |
| local port=$1 | |
| if is_port_running "$port"; then | |
| echo -e "(${BOLD}running${RESET})" | |
| # Check if a port is listening | |
| is_port_running() { | |
| command -v lsof >/dev/null 2>&1 || return 1 | |
| lsof -iTCP:"$1" -sTCP:LISTEN -P -n >/dev/null 2>&1 | |
| } |
🤖 Prompt for AI Agents
In `@examples/vite-port-demo/scripts/show-info.sh` around lines 16 - 25, The
is_port_running check currently uses lsof -i ":$1" which matches non-listening
endpoints and hides lsof absence; update is_port_running to first verify lsof
exists (command -v lsof) and if missing either error out or fall back to ss, and
when using lsof restrict to listening TCP sockets with flags like -iTCP
-sTCP:LISTEN -P -n to avoid name-resolution and false positives; update
format_status to rely on the corrected is_port_running behavior.
Summary
lsofto check if the port is listeningTest plan
wt infoon a worktree with the vite server running - should show "(running)" in boldwt infoon a worktree with the vite server stopped - should show "(stopped)"🤖 Generated with Claude Code
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.