Skip to content

Issues with windows UNC paths in FilepathGlob method #111

Description

@blewis12

On Windows, FilepathGlob can return file paths that are not valid UNC paths, even when the input pattern is a normal UNC glob such as \\server\share\logs\*.log. The leading \ that identifies a UNC volume is effectively reduced to a single , so callers treat the path as drive-relative (e.g. C:\server\share\logs\*.log). We're using version 4.10.0 of doublestar.

Breaking down exactly what happens in FilepathGlob that causes this:

  1. filepath.Clean / filepath.ToSlash: Normalizes the pattern; on Windows a UNC glob becomes a slash form whose directory prefix starts with //server/share/....

  2. SplitPattern: Splits that string into a base (still with the UNC-style) and the glob fragment passed to Glob.

  3. Glob on os.DirFS(base): Walks the tree and returns relative match paths (names under base).

  4. path.Join(base, matches[i]): Recombines each hit with base. path.Join applies path.Clean, which collapses the leading // (UNC in slash form) to a single /.

  5. filepath.FromSlash(...): after step 4 the path no longer has a proper \-UNC root, so you get one leading \ instead of \, and callers treat it as drive-relative (e.g. \server\share... → C:\server\share...).

I could also reproduce step 4 with this test:

func TestDoublestarUtils_FilepathGlobPreservesUNC(t *testing.T) {
	const uncGlobPattern = `\\server\share\logs\*.log`

	base := "//server/share/logs"
	matches := []string{"app.log"}

	for i := range matches {
		matches[i] = filepath.FromSlash(path.Join(base, matches[i]))
	}

	wantSlashNormalizedUNC := "//server/share/logs/app.log"
	gotSlash := filepath.ToSlash(matches[0])
	if gotSlash != wantSlashNormalizedUNC {
		t.Fatalf("%s: expected %q but got %q", uncGlobPattern, wantSlashNormalizedUNC, gotSlash)
	}
}

Is this intended behavior? Or a bug? I couldn't find any related issues or documentation about this, happy to discuss as well

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions