Description
When running port-selector --lock PORT where PORT is already allocated to a different directory, the command silently locks that port without warning. This is confusing because the user expects the port to be assigned to their current directory.
Steps to Reproduce
# Port 3006 is allocated to ~/code/worktrees/feature-155
$ port-selector --list | grep 3006
3006 ~/code/worktrees/feature-155 free - - - 2026-01-04
# From a DIFFERENT directory
$ cd ~/code/merchantly/main
$ port-selector
3048
# User tries to lock port 3006 for current directory
$ port-selector --lock 3006
Locked port 3006 for 'main' # ← No warning!
# User expects port-selector to return 3006 now
$ port-selector
3048 # ← Still returns 3048, not 3006!
# Check allocations - port 3006 is still assigned to the OTHER directory
$ port-selector --list | grep 3006
3006 ~/code/worktrees/feature-155 free yes - - - 2026-01-04
# ^^^ locked but wrong directory!
Решение: Option A — Ошибка + --force
При попытке заблокировать порт, принадлежащий другой директории — выдавать ошибку с предложением использовать --force:
error: port 3006 is allocated to ~/code/worktrees/feature-155
use --lock 3006 --force to reassign it to current directory
Поведение с --force
При указании --force:
- Порт отвязывается от старой директории
- Порт привязывается к текущей директории
- Порт блокируется
- Выводится предупреждение о переназначении:
warning: port 3006 was allocated to ~/code/worktrees/feature-155
Reassigned and locked port 3006 for 'main'
Edge Cases
| Сценарий |
Поведение |
| Порт принадлежит текущей директории |
Просто лочим, без ошибки |
| Порт принадлежит другой директории, без --force |
Ошибка + подсказка про --force |
| Порт принадлежит другой директории, с --force |
Warning + переназначение + лок |
| Порт не аллоцирован |
Аллоцируем + лочим (текущее поведение) |
| Порт вне диапазона |
Ошибка (текущее поведение) |
| Порт занят процессом |
Ошибка с информацией о процессе (текущее поведение) |
Root Cause
In lockSpecificPort() function (cmd/port-selector/main.go), when the port is already allocated:
alloc := store.FindByPort(portArg)
if alloc != nil {
// Port already allocated - update its lock status
if !store.SetLockedByPort(portArg, locked) {
return 0, ...
}
return portArg, nil // ← Returns success without checking directory!
}
The code doesn't verify that the port belongs to the current directory before locking it.
Implementation Plan
- Добавить флаг
--force в main.go
- В
lockSpecificPort() проверять alloc.Directory != cwd
- Если директория отличается и нет
--force — ошибка
- Если
--force — переназначить порт через store.SetAllocationWithName()
- Добавить тесты на все edge cases
- Обновить help и README
Environment
- port-selector version: 0.9.3
- OS: Linux
Description
When running
port-selector --lock PORTwhere PORT is already allocated to a different directory, the command silently locks that port without warning. This is confusing because the user expects the port to be assigned to their current directory.Steps to Reproduce
Решение: Option A — Ошибка + --force
При попытке заблокировать порт, принадлежащий другой директории — выдавать ошибку с предложением использовать
--force:Поведение с --force
При указании
--force:Edge Cases
Root Cause
In
lockSpecificPort()function (cmd/port-selector/main.go), when the port is already allocated:The code doesn't verify that the port belongs to the current directory before locking it.
Implementation Plan
--forceв main.golockSpecificPort()проверятьalloc.Directory != cwd--force— ошибка--force— переназначить порт черезstore.SetAllocationWithName()Environment