Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 4d34b6d

Browse files
authored
[et] Simplify path canonicalisation logic (#52275)
In order to canonicalise paths, previously we were doing an iterative computation to resolve symlinks to a canonical path, directory by directory. This was because on macOS and other BSDs, readlink doesn't support the `-f` (follow symlinks) option. However, macOS and other BSD-based systems *do* bundle the `realpath` utility, which resolves symlinks. This patch is stacked on top of #52274 and is the second commit (dec38dd). ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [X] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I signed the [CLA]. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat
1 parent f22fb59 commit 4d34b6d

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

bin/et

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,19 @@ set -e
99
# Needed because if it is set, cd may print the path it changed to.
1010
unset CDPATH
1111

12-
# On Mac OS, readlink -f doesn't work, so follow_links traverses the path one
13-
# link at a time, and then cds into the link destination and find out where it
14-
# ends up.
15-
#
16-
# The function is enclosed in a subshell to avoid changing the working directory
17-
# of the caller.
18-
function follow_links() (
19-
cd -P "$(dirname -- "$1")"
20-
file="$PWD/$(basename -- "$1")"
21-
while [[ -h "$file" ]]; do
22-
cd -P "$(dirname -- "$file")"
23-
file="$(readlink -- "$file")"
24-
cd -P "$(dirname -- "$file")"
25-
file="$PWD/$(basename -- "$file")"
26-
done
27-
echo "$file"
28-
)
12+
# Returns the canonical path for its argument, with any symlinks resolved.
13+
function canonical_path() {
14+
if [[ -x "$(which realpath)" ]]; then
15+
realpath -q -- "$1"
16+
elif [[ -x "$(which readlink)" ]]; then
17+
readlink -f -- "$1"
18+
else
19+
echo "The host platform is not supported by this tool"
20+
exit 1
21+
fi
22+
}
2923

30-
SCRIPT_DIR="$(dirname -- "$(follow_links "${BASH_SOURCE[0]}")")"
24+
SCRIPT_DIR="$(dirname -- "$(canonical_path "${BASH_SOURCE[0]}")")"
3125
ENGINE_DIR="$(cd "$SCRIPT_DIR/.."; pwd -P)"
3226

3327
case "$(uname -s)" in

0 commit comments

Comments
 (0)