@@ -30,9 +30,8 @@ String canonicalize(String path) => _couldBeCaseInsensitive
3030
3131/// Returns `path` with the case updated to match the path's case on disk.
3232///
33- /// This only updates `path` 's basename. It always returns `path` as-is on
34- /// operating systems other than Windows or Mac OS, since they almost never use
35- /// case-insensitive filesystems.
33+ /// This always returns `path` as-is on operating systems other than Windows or
34+ /// Mac OS, since they almost never use case-insensitive filesystems.
3635String _realCasePath (String path) {
3736 // TODO(nweiz): Use an SDK function for this when dart-lang/sdk#35370 and/or
3837 // nodejs/node#24942 are fixed, or at least use FFI functions.
@@ -47,14 +46,33 @@ String _realCasePath(String path) {
4746 }
4847 }
4948
50- String helper (String path) {
49+ String helper (String path, [ String ? realPath] ) {
5150 var dirname = p.dirname (path);
5251 if (dirname == path) return path;
5352
5453 return _realCaseCache.putIfAbsent (path, () {
54+ // If the path isn't a symlink, we can use the libraries' `realpath()`
55+ // functions to get its actual basename much more efficiently than listing
56+ // all its siblings.
57+ if (! linkExists (path)) {
58+ // Don't recompute the real path if it was already computed for a child
59+ // and we haven't seen any symlinks between that child and this directory.
60+ String realPathNonNull;
61+ try {
62+ realPathNonNull = realPath ?? realpath (path);
63+ } on FileSystemException {
64+ // If we can't get the realpath, that probably means the file doesn't
65+ // exist. Rather than throwing an error about symlink resolution,
66+ // return the non-existent path and let it throw whatever use-time
67+ // error it's going to throw.
68+ return path;
69+ }
70+ return p.join (helper (dirname, p.dirname (realPathNonNull)),
71+ p.basename (realPathNonNull));
72+ }
73+
5574 var realDirname = helper (dirname);
5675 var basename = p.basename (path);
57-
5876 try {
5977 var matches = listDir (realDirname)
6078 .where (
0 commit comments