Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(std/fs.walk): include symlink option #3464

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fs/expand_glob_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Deno.test("expandGlobWildcard", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
Expand All @@ -70,6 +71,7 @@ Deno.test("expandGlobParent", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
Expand Down Expand Up @@ -118,6 +120,7 @@ Deno.test("expandGlobGlobstarFalseWithGlob", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
Expand Down
20 changes: 18 additions & 2 deletions fs/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface WalkOptions {
includeFiles?: boolean;
/** @default {true} */
includeDirs?: boolean;
/** @default {true} */
includeSymlinks?: boolean;
/** @default {false} */
followSymlinks?: boolean;
exts?: string[];
Expand Down Expand Up @@ -84,6 +86,7 @@ export async function* walk(
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
includeSymlinks = true,
followSymlinks = false,
exts = undefined,
match = undefined,
Expand All @@ -108,7 +111,12 @@ export async function* walk(
let { isSymlink, isDirectory } = entry;

if (isSymlink) {
if (!followSymlinks) continue;
if (!followSymlinks) {
if (includeSymlinks && include(path, exts, match, skip)) {
yield { path, ...entry };
}
continue;
}
path = await Deno.realPath(path);
// Caveat emptor: don't assume |path| is not a symlink. realpath()
// resolves symlinks but another process can replace the file system
Expand All @@ -121,6 +129,7 @@ export async function* walk(
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
includeSymlinks,
followSymlinks,
exts,
match,
Expand All @@ -142,6 +151,7 @@ export function* walkSync(
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
includeSymlinks = true,
followSymlinks = false,
exts = undefined,
match = undefined,
Expand Down Expand Up @@ -171,7 +181,12 @@ export function* walkSync(
let { isSymlink, isDirectory } = entry;

if (isSymlink) {
if (!followSymlinks) continue;
if (!followSymlinks) {
if (includeSymlinks && include(path, exts, match, skip)) {
yield { path, ...entry };
}
continue;
}
path = Deno.realPathSync(path);
// Caveat emptor: don't assume |path| is not a symlink. realpath()
// resolves symlinks but another process can replace the file system
Expand All @@ -184,6 +199,7 @@ export function* walkSync(
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
includeSymlinks,
followSymlinks,
exts,
match,
Expand Down
6 changes: 6 additions & 0 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ Deno.test("[fs/walk] symlink", async () =>
followSymlinks: true,
}));

Deno.test("[fs/walk] symlink without followSymlink", async () => {
await assertWalkPaths("symlink", [".", "x", "y"], {
followSymlinks: false,
});
});

Deno.test("[fs/walk] non-existent root", async () => {
const root = resolve(testdataDir, "non_existent");
await assertRejects(
Expand Down