Skip to content

port-selector --lock PORT silently locks port belonging to another directory #76

@dapi

Description

@dapi

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:

  1. Порт отвязывается от старой директории
  2. Порт привязывается к текущей директории
  3. Порт блокируется
  4. Выводится предупреждение о переназначении:
    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

  1. Добавить флаг --force в main.go
  2. В lockSpecificPort() проверять alloc.Directory != cwd
  3. Если директория отличается и нет --force — ошибка
  4. Если --force — переназначить порт через store.SetAllocationWithName()
  5. Добавить тесты на все edge cases
  6. Обновить help и README

Environment

  • port-selector version: 0.9.3
  • OS: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions