Program: 110745
Project: 110745
Buggy source:
/** 
 * Loads the external package and then calls the selector to find matching rules.
 * @param env the environment to use for lookups
 * @param returnFirst whether to return only the first rule found
 * @param selector the function to call to load rules
 */
@Nullable private static Set<Rule> getRules(Environment env,boolean returnFirst,Function<Package,Iterable<Rule>> selector) throws ExternalPackageException, InterruptedException {
  SkyKey packageLookupKey=PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER);
  PackageLookupValue packageLookupValue=(PackageLookupValue)env.getValue(packageLookupKey);
  if (packageLookupValue == null) {
    return null;
  }
  RootedPath workspacePath=packageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
  Set<Rule> rules=new HashSet<>();
  SkyKey workspaceKey=WorkspaceFileValue.key(workspacePath);
  do {
    WorkspaceFileValue value=(WorkspaceFileValue)env.getValue(workspaceKey);
    if (value == null) {
      return null;
    }
    Package externalPackage=value.getPackage();
    if (externalPackage.containsErrors()) {
      Event.replayEventsOn(env.getListener(),externalPackage.getEvents());
      throw new ExternalPackageException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER,"String_Node_Str"),Transience.PERSISTENT);
    }
    Iterable<Rule> results=selector.apply(externalPackage);
    if (results != null) {
      Iterables.addAll(rules,results);
      if (returnFirst && !rules.isEmpty()) {
        return ImmutableSet.of(Iterables.getFirst(results,null));
      }
    }
    workspaceKey=value.next();
  }
 while (workspaceKey != null);
  return rules;
}

Fixed version (ground truth):
/** 
 * Loads the external package and then calls the selector to find matching rules.
 * @param env the environment to use for lookups
 * @param returnFirst whether to return only the first rule found
 * @param selector the function to call to load rules
 */
@Nullable private static List<Rule> getRules(Environment env,boolean returnFirst,Function<Package,Iterable<Rule>> selector) throws ExternalPackageException, InterruptedException {
  SkyKey packageLookupKey=PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER);
  PackageLookupValue packageLookupValue=(PackageLookupValue)env.getValue(packageLookupKey);
  if (packageLookupValue == null) {
    return null;
  }
  RootedPath workspacePath=packageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
  List<Rule> rules=ImmutableList.of();
  SkyKey workspaceKey=WorkspaceFileValue.key(workspacePath);
  do {
    WorkspaceFileValue value=(WorkspaceFileValue)env.getValue(workspaceKey);
    if (value == null) {
      return null;
    }
    Package externalPackage=value.getPackage();
    if (externalPackage.containsErrors()) {
      Event.replayEventsOn(env.getListener(),externalPackage.getEvents());
      throw new ExternalPackageException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER,"String_Node_Str"),Transience.PERSISTENT);
    }
    Iterable<Rule> results=selector.apply(externalPackage);
    if (results != null) {
      rules=ImmutableList.copyOf(results);
      if (returnFirst && !rules.isEmpty()) {
        return ImmutableList.of(Iterables.getFirst(results,null));
      }
    }
    workspaceKey=value.next();
  }
 while (workspaceKey != null);
  return rules;
}