Program: 110908
Project: 110908
Buggy source:
/** 
 * Looks in the directory specified by   {@code recursivePkgKey} for a package, does some workas specified by  {@link Visitor} if such a package exists, then recursively does work in eachnon-excluded subdirectory as specified by  {@link #getSkyKeyForSubdirectory}, and finally aggregates the   {@link Visitor} value along with values from each subdirectory as specifiedby  {@link #aggregateWithSubdirectorySkyValues}, and returns that aggregation. <p>Returns null if   {@code env.valuesMissing()} is true, checked after each call to one of{@link RecursiveDirectoryTraversalFunction}'s abstract methods that were given   {@code env}. (And after each of   {@code visitDirectory}'s own uses of   {@code env}, of course.)
 */
TReturn visitDirectory(RecursivePkgKey recursivePkgKey,Environment env){
  RootedPath rootedPath=recursivePkgKey.getRootedPath();
  BlacklistedPackagePrefixesValue blacklist=(BlacklistedPackagePrefixesValue)env.getValue(BlacklistedPackagePrefixesValue.key());
  if (blacklist == null) {
    return null;
  }
  Set<PathFragment> excludedPaths=Sets.union(recursivePkgKey.getExcludedPaths(),blacklist.getPatterns());
  Path root=rootedPath.getRoot();
  PathFragment rootRelativePath=rootedPath.getRelativePath();
  SkyKey fileKey=FileValue.key(rootedPath);
  FileValue fileValue;
  try {
    fileValue=(FileValue)env.getValueOrThrow(fileKey,InconsistentFilesystemException.class,FileSymlinkException.class,IOException.class);
  }
 catch (  InconsistentFilesystemException|FileSymlinkException|IOException e) {
    return reportErrorAndReturn("String_Node_Str",e,rootRelativePath,env.getListener());
  }
  if (fileValue == null) {
    return null;
  }
  if (!fileValue.isDirectory()) {
    return getEmptyReturn();
  }
  PackageIdentifier packageId=PackageIdentifier.create(recursivePkgKey.getRepository(),rootRelativePath);
  if ((packageId.getRepository().isDefault() || packageId.getRepository().isMain()) && fileValue.isSymlink() && fileValue.getUnresolvedLinkTarget().startsWith(directories.getOutputBase().asFragment())) {
    return getEmptyReturn();
  }
  SkyKey pkgLookupKey=PackageLookupValue.key(packageId);
  SkyKey dirListingKey=DirectoryListingValue.key(rootedPath);
  Map<SkyKey,ValueOrException4<NoSuchPackageException,InconsistentFilesystemException,FileSymlinkException,IOException>> pkgLookupAndDirectoryListingDeps=env.getValuesOrThrow(ImmutableList.of(pkgLookupKey,dirListingKey),NoSuchPackageException.class,InconsistentFilesystemException.class,FileSymlinkException.class,IOException.class);
  if (env.valuesMissing()) {
    return null;
  }
  PackageLookupValue pkgLookupValue;
  try {
    pkgLookupValue=(PackageLookupValue)Preconditions.checkNotNull(pkgLookupAndDirectoryListingDeps.get(pkgLookupKey).get(),"String_Node_Str",recursivePkgKey,pkgLookupKey);
  }
 catch (  NoSuchPackageException|InconsistentFilesystemException e) {
    return reportErrorAndReturn("String_Node_Str",e,rootRelativePath,env.getListener());
  }
catch (  IOException|FileSymlinkException e) {
    throw new IllegalStateException(e);
  }
  TVisitor visitor=getInitialVisitor();
  DirectoryListingValue dirListingValue;
  try {
    dirListingValue=(DirectoryListingValue)Preconditions.checkNotNull(pkgLookupAndDirectoryListingDeps.get(dirListingKey).get(),"String_Node_Str",recursivePkgKey,dirListingKey);
  }
 catch (  InconsistentFilesystemException|IOException e) {
    return reportErrorAndReturn("String_Node_Str",e,rootRelativePath,env.getListener());
  }
catch (  FileSymlinkException e) {
    throw new IllegalStateException("String_Node_Str" + rootedPath + "String_Node_Str");
  }
catch (  NoSuchPackageException e) {
    throw new IllegalStateException(e);
  }
  boolean followSymlinks=shouldFollowSymlinksWhenTraversing(dirListingValue.getDirents());
  List<SkyKey> childDeps=new ArrayList<>();
  for (  Dirent dirent : dirListingValue.getDirents()) {
    Type type=dirent.getType();
    if (type != Type.DIRECTORY && (type != Type.SYMLINK || (type == Type.SYMLINK && !followSymlinks))) {
      continue;
    }
    String basename=dirent.getName();
    if (rootRelativePath.equals(PathFragment.EMPTY_FRAGMENT) && PathPackageLocator.DEFAULT_TOP_LEVEL_EXCLUDES.contains(basename)) {
      continue;
    }
    PathFragment subdirectory=rootRelativePath.getRelative(basename);
    if (excludedPaths.contains(subdirectory)) {
      continue;
    }
    ImmutableSet<PathFragment> excludedSubdirectoriesBeneathThisSubdirectory=PathFragment.filterPathsStartingWith(excludedPaths,subdirectory);
    RootedPath subdirectoryRootedPath=RootedPath.toRootedPath(root,subdirectory);
    childDeps.add(getSkyKeyForSubdirectory(recursivePkgKey.getRepository(),subdirectoryRootedPath,excludedSubdirectoriesBeneathThisSubdirectory));
  }
  Map<SkyKey,SkyValue> subdirectorySkyValues;
  if (pkgLookupValue.packageExists() && pkgLookupValue.getRoot().equals(root)) {
    SkyKey packageKey=PackageValue.key(packageId);
    Map<SkyKey,ValueOrException<NoSuchPackageException>> dependentSkyValues=env.getValuesOrThrow(Iterables.concat(childDeps,ImmutableList.of(packageKey)),NoSuchPackageException.class);
    if (env.valuesMissing()) {
      return null;
    }
    Package pkg=null;
    try {
      PackageValue pkgValue=(PackageValue)dependentSkyValues.get(packageKey).get();
      if (pkgValue == null) {
        return null;
      }
      pkg=pkgValue.getPackage();
      if (pkg.containsErrors()) {
        env.getListener().handle(Event.error("String_Node_Str" + rootRelativePath.getPathString()));
      }
    }
 catch (    NoSuchPackageException e) {
      env.getListener().handle(Event.error("String_Node_Str" + rootRelativePath.getPathString()));
    }
    if (pkg != null) {
      visitor.visitPackageValue(pkg,env);
      if (env.valuesMissing()) {
        return null;
      }
    }
    subdirectorySkyValues=Maps.transformValues(Maps.filterKeys(dependentSkyValues,Predicates.not(Predicates.equalTo(packageKey))),GET_SKYVALUE);
  }
 else {
    subdirectorySkyValues=env.getValues(childDeps);
  }
  if (env.valuesMissing()) {
    return null;
  }
  return aggregateWithSubdirectorySkyValues(visitor,subdirectorySkyValues);
}

Fixed version (ground truth):
/** 
 * Looks in the directory specified by   {@code recursivePkgKey} for a package, does some workas specified by  {@link Visitor} if such a package exists, then recursively does work in eachnon-excluded subdirectory as specified by  {@link #getSkyKeyForSubdirectory}, and finally aggregates the   {@link Visitor} value along with values from each subdirectory as specifiedby  {@link #aggregateWithSubdirectorySkyValues}, and returns that aggregation. <p>Returns null if   {@code env.valuesMissing()} is true, checked after each call to one of{@link RecursiveDirectoryTraversalFunction}'s abstract methods that were given   {@code env}. (And after each of   {@code visitDirectory}'s own uses of   {@code env}, of course.)
 */
TReturn visitDirectory(RecursivePkgKey recursivePkgKey,Environment env){
  RootedPath rootedPath=recursivePkgKey.getRootedPath();
  ImmutableSet<PathFragment> excludedPaths=recursivePkgKey.getExcludedPaths();
  Path root=rootedPath.getRoot();
  PathFragment rootRelativePath=rootedPath.getRelativePath();
  SkyKey fileKey=FileValue.key(rootedPath);
  FileValue fileValue;
  try {
    fileValue=(FileValue)env.getValueOrThrow(fileKey,InconsistentFilesystemException.class,FileSymlinkException.class,IOException.class);
  }
 catch (  InconsistentFilesystemException|FileSymlinkException|IOException e) {
    return reportErrorAndReturn("String_Node_Str",e,rootRelativePath,env.getListener());
  }
  if (fileValue == null) {
    return null;
  }
  if (!fileValue.isDirectory()) {
    return getEmptyReturn();
  }
  PackageIdentifier packageId=PackageIdentifier.create(recursivePkgKey.getRepository(),rootRelativePath);
  if ((packageId.getRepository().isDefault() || packageId.getRepository().isMain()) && fileValue.isSymlink() && fileValue.getUnresolvedLinkTarget().startsWith(directories.getOutputBase().asFragment())) {
    return getEmptyReturn();
  }
  SkyKey pkgLookupKey=PackageLookupValue.key(packageId);
  SkyKey dirListingKey=DirectoryListingValue.key(rootedPath);
  Map<SkyKey,ValueOrException4<NoSuchPackageException,InconsistentFilesystemException,FileSymlinkException,IOException>> pkgLookupAndDirectoryListingDeps=env.getValuesOrThrow(ImmutableList.of(pkgLookupKey,dirListingKey),NoSuchPackageException.class,InconsistentFilesystemException.class,FileSymlinkException.class,IOException.class);
  if (env.valuesMissing()) {
    return null;
  }
  PackageLookupValue pkgLookupValue;
  try {
    pkgLookupValue=(PackageLookupValue)Preconditions.checkNotNull(pkgLookupAndDirectoryListingDeps.get(pkgLookupKey).get(),"String_Node_Str",recursivePkgKey,pkgLookupKey);
  }
 catch (  NoSuchPackageException|InconsistentFilesystemException e) {
    return reportErrorAndReturn("String_Node_Str",e,rootRelativePath,env.getListener());
  }
catch (  IOException|FileSymlinkException e) {
    throw new IllegalStateException(e);
  }
  TVisitor visitor=getInitialVisitor();
  DirectoryListingValue dirListingValue;
  try {
    dirListingValue=(DirectoryListingValue)Preconditions.checkNotNull(pkgLookupAndDirectoryListingDeps.get(dirListingKey).get(),"String_Node_Str",recursivePkgKey,dirListingKey);
  }
 catch (  InconsistentFilesystemException|IOException e) {
    return reportErrorAndReturn("String_Node_Str",e,rootRelativePath,env.getListener());
  }
catch (  FileSymlinkException e) {
    throw new IllegalStateException("String_Node_Str" + rootedPath + "String_Node_Str");
  }
catch (  NoSuchPackageException e) {
    throw new IllegalStateException(e);
  }
  boolean followSymlinks=shouldFollowSymlinksWhenTraversing(dirListingValue.getDirents());
  List<SkyKey> childDeps=new ArrayList<>();
  for (  Dirent dirent : dirListingValue.getDirents()) {
    Type type=dirent.getType();
    if (type != Type.DIRECTORY && (type != Type.SYMLINK || (type == Type.SYMLINK && !followSymlinks))) {
      continue;
    }
    String basename=dirent.getName();
    if (rootRelativePath.equals(PathFragment.EMPTY_FRAGMENT) && PathPackageLocator.DEFAULT_TOP_LEVEL_EXCLUDES.contains(basename)) {
      continue;
    }
    PathFragment subdirectory=rootRelativePath.getRelative(basename);
    if (excludedPaths.contains(subdirectory)) {
      continue;
    }
    ImmutableSet<PathFragment> excludedSubdirectoriesBeneathThisSubdirectory=PathFragment.filterPathsStartingWith(excludedPaths,subdirectory);
    RootedPath subdirectoryRootedPath=RootedPath.toRootedPath(root,subdirectory);
    childDeps.add(getSkyKeyForSubdirectory(recursivePkgKey.getRepository(),subdirectoryRootedPath,excludedSubdirectoriesBeneathThisSubdirectory));
  }
  Map<SkyKey,SkyValue> subdirectorySkyValues;
  if (pkgLookupValue.packageExists() && pkgLookupValue.getRoot().equals(root)) {
    SkyKey packageKey=PackageValue.key(packageId);
    Map<SkyKey,ValueOrException<NoSuchPackageException>> dependentSkyValues=env.getValuesOrThrow(Iterables.concat(childDeps,ImmutableList.of(packageKey)),NoSuchPackageException.class);
    if (env.valuesMissing()) {
      return null;
    }
    Package pkg=null;
    try {
      PackageValue pkgValue=(PackageValue)dependentSkyValues.get(packageKey).get();
      if (pkgValue == null) {
        return null;
      }
      pkg=pkgValue.getPackage();
      if (pkg.containsErrors()) {
        env.getListener().handle(Event.error("String_Node_Str" + rootRelativePath.getPathString()));
      }
    }
 catch (    NoSuchPackageException e) {
      env.getListener().handle(Event.error("String_Node_Str" + rootRelativePath.getPathString()));
    }
    if (pkg != null) {
      visitor.visitPackageValue(pkg,env);
      if (env.valuesMissing()) {
        return null;
      }
    }
    subdirectorySkyValues=Maps.transformValues(Maps.filterKeys(dependentSkyValues,Predicates.not(Predicates.equalTo(packageKey))),GET_SKYVALUE);
  }
 else {
    subdirectorySkyValues=env.getValues(childDeps);
  }
  if (env.valuesMissing()) {
    return null;
  }
  return aggregateWithSubdirectorySkyValues(visitor,subdirectorySkyValues);
}