Skip to content

Commit bd4d5d8

Browse files
authored
Merge pull request #81 from dapi/fix/list-docker-directory
fix: show allocation directory in --list instead of Docker Cwd
2 parents 50a2186 + abce2ac commit bd4d5d8

3 files changed

Lines changed: 21 additions & 35 deletions

File tree

cmd/port-selector/main.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,17 @@ func runWithName(name string) error {
379379
// ALWAYS return the same port for (directory, name) - port is stable per directory
380380
if existing := store.FindByDirectoryAndName(cwd, name); existing != nil {
381381
debug.Printf("main", "found existing allocation for name %s: port %d (locked=%v)", name, existing.Port, existing.Locked)
382+
383+
// Warn if the port is busy (occupied by another process)
384+
if !port.IsPortFree(existing.Port) {
385+
procInfo := port.GetPortProcess(existing.Port)
386+
if procInfo != nil && procInfo.Name != "" {
387+
fmt.Fprintf(os.Stderr, "warning: port %d is busy (%s); use --forget to get a new port\n", existing.Port, procInfo.Name)
388+
} else {
389+
fmt.Fprintf(os.Stderr, "warning: port %d is busy; use --forget to get a new port\n", existing.Port)
390+
}
391+
}
392+
382393
// Update last_used timestamp for the specific port being issued
383394
if !store.UpdateLastUsedByPort(existing.Port) {
384395
debug.Printf("main", "warning: UpdateLastUsedByPort failed for port %d", existing.Port)
@@ -798,18 +809,7 @@ func runList() error {
798809
maxDirLen := 0
799810

800811
for i, alloc := range allAllocs {
801-
directory := alloc.Directory
802-
803-
// Check if port is busy and has Docker info
804-
if !port.IsPortFree(alloc.Port) {
805-
if procInfo := port.GetPortProcess(alloc.Port); procInfo != nil {
806-
if procInfo.ContainerID != "" && procInfo.Cwd != "" && procInfo.Cwd != "/" {
807-
directory = procInfo.Cwd
808-
}
809-
}
810-
}
811-
812-
shortDir := pathutil.ShortenHomePath(directory)
812+
shortDir := pathutil.ShortenHomePath(alloc.Directory)
813813
allDirectories[i] = shortDir
814814

815815
if len(shortDir) > maxDirLen {
@@ -828,7 +828,6 @@ func runList() error {
828828
username := "-"
829829
pid := "-"
830830
process := "-"
831-
directory := alloc.Directory
832831

833832
// Determine SOURCE and use saved external info for external allocations
834833
source := "free"
@@ -874,20 +873,12 @@ func runList() error {
874873
} else if procInfo.ContainerID != "" {
875874
// Docker container detected via fallback
876875
process = "docker-proxy"
877-
if procInfo.Cwd != "" && procInfo.Cwd != "/" {
878-
directory = procInfo.Cwd
879-
}
880876
} else {
881877
// Have user but no PID and no Docker - mark incomplete only if no saved name
882878
if alloc.ProcessName == "" {
883879
hasIncompleteInfo = true
884880
}
885881
}
886-
887-
// Use live Docker directory if available and better than saved
888-
if procInfo.ContainerID != "" && procInfo.Cwd != "" && procInfo.Cwd != "/" {
889-
directory = procInfo.Cwd
890-
}
891882
}
892883
}
893884

@@ -903,10 +894,6 @@ func runList() error {
903894

904895
// Get pre-calculated directory string and truncate if needed
905896
shortDir := allDirectories[i]
906-
// If directory was updated by Docker check, re-shorten it
907-
if directory != alloc.Directory {
908-
shortDir = pathutil.ShortenHomePath(directory)
909-
}
910897
// Cap at 40 characters maximum
911898
if len(shortDir) > maxDirWidth {
912899
shortDir = truncateDirectoryPath(shortDir, maxDirWidth)

cmd/port-selector/main_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"bytes"
5-
"fmt"
65
"net"
76
"os"
87
"os/exec"
@@ -1227,8 +1226,6 @@ func TestPortSelector_ReturnsSamePortEvenWhenBusy(t *testing.T) {
12271226
t.Logf("Initial port: %s", initialPort)
12281227

12291228
// Step 2: Simulate user's service running on that port
1230-
portNum := 0
1231-
fmt.Sscanf(initialPort, "%d", &portNum)
12321229
ln, err := net.Listen("tcp", ":"+initialPort)
12331230
if err != nil {
12341231
t.Skipf("could not occupy port %s for test: %v", initialPort, err)
@@ -1254,6 +1251,15 @@ func TestPortSelector_ReturnsSamePortEvenWhenBusy(t *testing.T) {
12541251
t.Errorf("BUG REPRODUCED: expected same port %s, got different port %s", initialPort, secondPort)
12551252
t.Errorf("Port should be stable for the same directory, even when busy")
12561253
}
1254+
1255+
// Step 5: Verify warning is printed to stderr when port is busy
1256+
stderrStr := stderr2.String()
1257+
if !strings.Contains(stderrStr, "warning: port") || !strings.Contains(stderrStr, "is busy") {
1258+
t.Errorf("expected 'warning: port ... is busy' in stderr, got: %q", stderrStr)
1259+
}
1260+
if !strings.Contains(stderrStr, "--forget") {
1261+
t.Errorf("expected '--forget' hint in stderr warning, got: %q", stderrStr)
1262+
}
12571263
}
12581264

12591265
func TestPortSelector_PortStabilityAcrossMultipleCalls(t *testing.T) {

internal/allocations/allocations_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,10 +2543,6 @@ func TestSetAllocation_PreservesLockedPorts(t *testing.T) {
25432543

25442544
// Tests for issue #77: FindByDirectoryAndNameWithPriority
25452545

2546-
2547-
2548-
2549-
25502546
// Tests for UnlockOtherLockedPorts
25512547

25522548
func TestUnlockOtherLockedPorts(t *testing.T) {
@@ -2630,9 +2626,6 @@ func TestUnlockOtherLockedPorts_EmptyStore(t *testing.T) {
26302626
}
26312627
}
26322628

2633-
2634-
2635-
26362629
func TestRefreshExternalAllocations_KeepsActive(t *testing.T) {
26372630
store := NewStore()
26382631
now := time.Now().UTC()

0 commit comments

Comments
 (0)