Files
beads/internal/utils/path.go
Steve Yegge 22756509cc Refactor: Extract path canonicalization into utils.CanonicalizePath()
Extracts duplicated path canonicalization logic (filepath.Abs + EvalSymlinks)
into a reusable helper function utils.CanonicalizePath() in internal/utils/path.go.

Changes:
- Add internal/utils/path.go with CanonicalizePath() function
- Add comprehensive tests in internal/utils/path_test.go
- Replace inline canonicalization in beads.go:131-140
- Replace inline canonicalization in cmd/bd/main.go:446-454
- Replace inline canonicalization in cmd/bd/nodb.go:25-33

The new helper maintains identical behavior:
1. Converts path to absolute form via filepath.Abs
2. Resolves symlinks via filepath.EvalSymlinks
3. Falls back gracefully on errors (returns absPath if EvalSymlinks fails,
   returns original path if Abs fails)

Fixes bd-efe8

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-02 19:11:08 -08:00

35 lines
972 B
Go

// Package utils provides utility functions for issue ID parsing and path handling.
package utils
import (
"path/filepath"
)
// CanonicalizePath converts a path to its canonical form by:
// 1. Converting to absolute path
// 2. Resolving symlinks
//
// If either step fails, it falls back to the best available form:
// - If symlink resolution fails, returns absolute path
// - If absolute path conversion fails, returns original path
//
// This function is used to ensure consistent path handling across the codebase,
// particularly for BEADS_DIR environment variable processing.
func CanonicalizePath(path string) string {
// Try to get absolute path
absPath, err := filepath.Abs(path)
if err != nil {
// If we can't get absolute path, return original
return path
}
// Try to resolve symlinks
canonical, err := filepath.EvalSymlinks(absPath)
if err != nil {
// If we can't resolve symlinks, return absolute path
return absPath
}
return canonical
}