Skip to content

Commit 76dbb68

Browse files
authored
fix(librarian): canonicalize entries when constructing keep set (#3235)
This uses `filepath.Rel` when constructing the set of files to keep, given that we'll be using the result of `filepath.Rel` when testing that set. Fixes #3234
1 parent ab13a7a commit 76dbb68

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

internal/librarian/generate.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,15 @@ func cleanOutput(dir string, keep []string) error {
334334
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
335335
return fmt.Errorf("%s: file %q in keep list does not exist", dir, k)
336336
}
337-
keepSet[k] = true
337+
// Effectively get a canonical relative path. While in most cases
338+
// this will be equal to k, it might not be - in particular,
339+
// on Windows the directory separator in paths returned by Rel
340+
// will be a backslash.
341+
rel, err := filepath.Rel(dir, path)
342+
if err != nil {
343+
return err
344+
}
345+
keepSet[rel] = true
338346
}
339347
return filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
340348
if err != nil {

internal/librarian/generate_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,16 @@ func TestCleanOutput(t *testing.T) {
380380
keep: []string{"src/operation.rs", "src/endpoint.rs"},
381381
want: []string{"src/endpoint.rs", "src/operation.rs"},
382382
},
383+
{
384+
// While it would definitely be odd to use "./" here, the
385+
// most common case for canonicalizing is for Windows where
386+
// the directory separator is a backslash. This test ensures
387+
// the logic is tested even on Unix.
388+
name: "keep entries are canonicalized",
389+
files: []string{"Cargo.toml", "README.md", "src/lib.rs"},
390+
keep: []string{"./Cargo.toml"},
391+
want: []string{"Cargo.toml"},
392+
},
383393
} {
384394
t.Run(test.name, func(t *testing.T) {
385395
dir := t.TempDir()

0 commit comments

Comments
 (0)