Skip to content

Commit ddb2936

Browse files
committed
dofs: treat empty find pattern as no pattern
find compiled the pattern argument into a regex whenever it was not undefined. An empty string is defined, so find(db, dir, "") produced the regex ^$, which matches only empty relative paths and returned no results instead of every entry. Guard the compile step on a truthy pattern so an empty string behaves like an omitted pattern and walks the whole tree. Add a regression test covering the empty-pattern case.
1 parent f9e2ec4 commit ddb2936

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/dofs/src/fs/find.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ describe("find", () => {
2727
});
2828
});
2929

30+
it("treats an empty pattern like no pattern and returns every entry", async () => {
31+
await withDB(async (db) => {
32+
mkdir(db, "/a/b", { recursive: true }, () => 0);
33+
await writeFile(db, "/a/x.ts", "", {}, () => 0);
34+
await writeFile(db, "/a/b/y.md", "", {}, () => 0);
35+
const entries = find(db, "/", "").sort((p, q) => p.path.localeCompare(q.path));
36+
expect(entries).toEqual([
37+
{ path: "/a", type: "dir" },
38+
{ path: "/a/b", type: "dir" },
39+
{ path: "/a/b/y.md", type: "file" },
40+
{ path: "/a/x.ts", type: "file" },
41+
]);
42+
});
43+
});
44+
3045
it("matches a single-level glob *.ts within the directory only", async () => {
3146
await withDB(async (db) => {
3247
mkdir(db, "/a/b", { recursive: true }, () => 0);

packages/dofs/src/fs/find.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export function find(db: Database, directory: string, pattern?: string): Workspa
2525
}
2626

2727
const out: WorkspaceFoundEntry[] = [];
28-
const regex = pattern !== undefined ? compileGlob(pattern) : undefined;
28+
// An empty pattern is equivalent to no pattern: walk and return
29+
// everything rather than compiling it into `^$`, which would match
30+
// only empty relative paths and yield no results.
31+
const regex = pattern ? compileGlob(pattern) : undefined;
2932

3033
walk(db, node.inode, canonical, out);
3134

0 commit comments

Comments
 (0)