Skip to content

fix: applyConfigDefaults uses full port resolution chain#2591

Merged
steveyegge merged 2 commits intosteveyegge:mainfrom
mrmaxsteel:fix/port-resolution-fallback
Mar 15, 2026
Merged

fix: applyConfigDefaults uses full port resolution chain#2591
steveyegge merged 2 commits intosteveyegge:mainfrom
mrmaxsteel:fix/port-resolution-fallback

Conversation

@mrmaxsteel
Copy link
Contributor

Summary

applyConfigDefaults() in store.go only checked env vars (BEADS_DOLT_SERVER_PORT / BEADS_DOLT_PORT) for port resolution. When no env var was set, cfg.ServerPort stayed at 0, and the MySQL driver defaulted to port 3307. If another project's Dolt was running on 3307, bd connected to the wrong database — causing PROJECT IDENTITY MISMATCH errors.

The fix: when cfg.ServerPort is still 0 after env var check, call doltserver.DefaultConfig(cfg.Path) to resolve using the full priority chain (port file > config.yaml > metadata.json > ephemeral). This function already existed and was used by open.go — it just wasn't wired into applyConfigDefaults().

Fixes #2590

The bug

Two independent port resolution paths existed:

Path Location Reads port file? Reads config.yaml? Reads metadata.json?
doltserver.DefaultConfig() doltserver.go:298 Yes Yes Yes
applyConfigDefaults() store.go:545 No No No

applyConfigDefaults() is called by New() (the main store constructor). So every bd command that opens a store used the incomplete resolution path.

The fix

6 lines added after the env var check in applyConfigDefaults():

if cfg.ServerPort == 0 && cfg.Path != "" {
    if resolved := doltserver.DefaultConfig(cfg.Path); resolved.Port > 0 {
        cfg.ServerPort = resolved.Port
    }
}

Reproduction

Port file (.beads/dolt-server.port): 13958
metadata.json dolt_server_port:      13958
config.yaml dolt.port:               13958
Server on 13958:                     PID 79454 (correct)
Server on 3307:                      PID 61645 (another project)

# BEFORE fix:
bd list → connects to 3307 → PROJECT IDENTITY MISMATCH
BEADS_DOLT_PORT=13958 bd list → works

# AFTER fix:
bd list → connects to 13958 → works (no env var needed)

Test plan

  • go build ./... compiles
  • go test ./internal/storage/dolt/ -run TestApplyConfigDefaults — 5 existing tests pass
  • Manual verification: bd list connects to port file port without env var override
  • Tests for port file fallback in applyConfigDefaults (would appreciate reviewer input on test approach — doltserver.DefaultConfig reads from filesystem)

🤖 Generated with Claude Code

mrmaxsteel and others added 2 commits March 14, 2026 15:02
When no BEADS_DOLT_SERVER_PORT env var is set, applyConfigDefaults()
left cfg.ServerPort at 0, which the MySQL driver defaulted to 3307.
If another project's Dolt was on 3307, bd connected to the wrong
database — causing PROJECT IDENTITY MISMATCH errors.

Now falls back to doltserver.DefaultConfig() which checks:
port file > config.yaml > metadata.json > ephemeral.

Fixes steveyegge#2590

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Contributor

@ktoulgaridis ktoulgaridis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All non-Embedded-Dolt checks pass (9/9). Embedded Dolt failures are systemic across all PRs and main. Clean 6-line fix wiring applyConfigDefaults() into the full port resolution chain (port file > config.yaml > metadata.json). Prevents cross-project connections when no env var is set. LGTM.

@garitar
Copy link
Contributor

garitar commented Mar 15, 2026

Thanks for pushing on this. While auditing #2590, I found a remaining CLI case that still fails even when applyConfigDefaults() is improved: if metadata.json sets dolt_data_dir outside .beads, bd list can derive the wrong config directory from filepath.Dir(dbPath), lose the repo-local metadata/port/database settings, and fall back to the wrong server.

I opened a focused follow-up PR here with a deterministic repro that fails on unpatched origin/main and passes with the fix: #2614

Your PR helped narrow the search area, so I wanted to cross-link the remaining case here rather than duplicate discussion. Thanks again for digging into this.

@steveyegge steveyegge merged commit 7017954 into steveyegge:main Mar 15, 2026
9 of 10 checks passed
@mrmaxsteel mrmaxsteel deleted the fix/port-resolution-fallback branch March 15, 2026 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bd ignores configured port, connects to default 3307

4 participants