Skip to content

Commit 9ff3554

Browse files
authored
fix: update find commands to support GNU and FreeBSD flavors (#804)
The arguments to GNU `find` and FreeBSD (MacOS) `find` are slightly different. This PR orders the options so that GNU `find` is happy. It also only uses options that are available in both flavors. Closes #800.
1 parent 2ba25de commit 9ff3554

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

swiftpkg/internal/repository_files.bzl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ def _list_files_under(
4343

4444
# Follow symlinks and report on the actual files.
4545
find_args = ["find", "-H", "-L", path]
46+
47+
# For GNU find, it is important for the global options (e.g. -maxdepth) to be
48+
# specified BEFORE other options like -type. Also, GNU find does not support -depth <level>.
49+
# So, we approximate it by using -mindepth and -maxdepth.
50+
if depth != None:
51+
depth_str = "{}".format(depth)
52+
find_args.extend(["-mindepth", depth_str, "-maxdepth", depth_str])
4653
if by_name != None:
4754
find_args.extend(["-name", by_name])
48-
if depth != None:
49-
find_args.extend(["-depth", "{}".format(depth)])
5055
exec_result = repository_ctx.execute(find_args, quiet = True)
5156
if exec_result.return_code != 0:
5257
fail("Failed to list files in %s. stderr:\n%s" % (path, exec_result.stderr))
@@ -79,13 +84,22 @@ def _list_directories_under(
7984
Returns:
8085
A `list` of path `string` values.
8186
"""
82-
find_args = ["find", path, "-type", "d"]
87+
find_args = ["find", path]
88+
89+
# For GNU find, it is important for the global options (e.g. -maxdepth) to be
90+
# specified BEFORE other options like -type. Also, GNU find does not support -depth <level>.
91+
# So, we approximate it by using -mindepth and -maxdepth.
92+
if depth != None:
93+
depth_str = "{}".format(depth)
94+
find_args.extend(["-mindepth", depth_str])
95+
if max_depth == None:
96+
find_args.extend(["-maxdepth", depth_str])
8397
if max_depth != None:
8498
find_args.extend(["-maxdepth", "%d" % (max_depth)])
99+
find_args.extend(["-type", "d"])
85100
if by_name != None:
86101
find_args.extend(["-name", by_name])
87-
if depth != None:
88-
find_args.extend(["-depth", "{}".format(depth)])
102+
89103
exec_result = repository_ctx.execute(find_args, quiet = True)
90104
if exec_result.return_code != 0:
91105
fail("Failed to list directories under %s. stderr:\n%s" % (path, exec_result.stderr))

0 commit comments

Comments
 (0)