Not all List implementations support sort()
ImmutableList and Collections.unmodifiableList() will throw an
UnsupportedOperationException if trying to sort.
Note that specifically in the case of the current implementation of
runGlobOperation, an empty matches list is always an ImmutableList (so we
should avoid sorting an empty matches list as the fast case).
@@ -736,8 +736,13 @@ public class StarlarkNativeModule implements StarlarkNativeModuleApi {
List<String> matches =
runGlobOperation(
context, thread, includes, excludes, Globber.Operation.SUBPACKAGES, allowEmpty);
+ if (!matches.isEmpty()) {
+ try {
matches.sort(naturalOrder());
-
+ } catch (UnsupportedOperationException e) {
+ matches = ImmutableList.sortedCopyOf(naturalOrder(), matches);
+ }
+ }
return StarlarkList.copyOf(thread.mutability(), matches);
}
