Refactor glob pattern handling, ensure relative globs are resolved against SourceDir

This commit is contained in:
2025-12-19 16:28:25 +01:00
parent 12e0612b48
commit 4d0b91ed94

View File

@@ -91,10 +91,10 @@ func SplitPattern(pattern string) (string, string) {
static, remainingPattern := doublestar.SplitPattern(pattern)
splitPatternLogger.Trace("After split: static=%q, pattern=%q", static, remainingPattern)
// Resolve the static part to handle ~ expansion and make it absolute
// ResolvePath already normalizes to forward slashes
static = ResolvePath(static)
splitPatternLogger.Trace("Resolved static part: %q", static)
// Normalize to forward slashes but DON'T resolve relative to CWD
// Paths should already be resolved by the caller (AggregateGlobs, etc.)
static = filepath.ToSlash(static)
splitPatternLogger.Trace("Normalized static part: %q", static)
splitPatternLogger.Trace("Final static path: %q, Remaining pattern: %q", static, remainingPattern)
return static, remainingPattern
@@ -126,9 +126,22 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
for _, command := range commands {
associateFilesLogger.Debug("Checking command %q for file %q", command.Name, file)
for _, glob := range command.Files {
// SplitPattern now handles tilde expansion and path resolution
static, pattern := SplitPattern(glob)
associateFilesLogger.Trace("Glob parts for %q → static=%q pattern=%q", glob, static, pattern)
// Resolve glob relative to SourceDir if it's a relative path
var resolvedGlob string
if !filepath.IsAbs(glob) && command.SourceDir != "" {
resolvedGlob = filepath.Join(command.SourceDir, glob)
associateFilesLogger.Trace("Joined relative glob %q to %q using SourceDir %q", glob, resolvedGlob, command.SourceDir)
} else {
resolvedGlob = glob
}
// Make absolute and normalize
resolvedGlob = ResolvePath(resolvedGlob)
associateFilesLogger.Trace("Final resolved glob: %q", resolvedGlob)
// SplitPattern just splits, doesn't resolve
static, pattern := SplitPattern(resolvedGlob)
associateFilesLogger.Trace("Glob parts for %q → static=%q pattern=%q", resolvedGlob, static, pattern)
// Use resolved file for matching (already normalized to forward slashes by ResolvePath)
absFile := resolvedFile
@@ -181,15 +194,23 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
globs := make(map[string]struct{})
for _, command := range commands {
aggregateGlobsLogger.Debug("Processing command %q for glob patterns", command.Name)
aggregateGlobsLogger.Trace("Command SourceDir: %q", command.SourceDir)
for _, glob := range command.Files {
// Split the glob into static and pattern parts, then resolve ONLY the static part
static, pattern := SplitPattern(glob)
// Reconstruct the glob with resolved static part
resolvedGlob := static
if pattern != "" {
resolvedGlob += "/" + pattern
// If the glob is relative and we have a SourceDir, resolve relative to SourceDir
var resolvedGlob string
if !filepath.IsAbs(glob) && command.SourceDir != "" {
// Relative path - resolve relative to the TOML file's directory
resolvedGlob = filepath.Join(command.SourceDir, glob)
aggregateGlobsLogger.Trace("Joined relative glob %q to %q using SourceDir %q", glob, resolvedGlob, command.SourceDir)
} else {
// Absolute path or no SourceDir - use as-is
resolvedGlob = glob
}
aggregateGlobsLogger.Trace("Adding glob: %q (resolved to %q) [static=%s, pattern=%s]", glob, resolvedGlob, static, pattern)
// Make absolute and normalize (ResolvePath handles both)
resolvedGlob = ResolvePath(resolvedGlob)
aggregateGlobsLogger.Trace("Final resolved glob: %q", resolvedGlob)
globs[resolvedGlob] = struct{}{}
}
}