Program: 110814
Project: 110814
Buggy source:
/** 
 * Constructs a   {@link Package} object for the given package using legacy package loading.Note that the returned package may be in error. <p>May return null if the computation has to be restarted. <p>Exactly one of  {@code replacementContents} and {@code buildFileValue} will benon- {@code null}. The former indicates that we have a faux BUILD file with the given contents and the latter indicates that we have a legitimate BUILD file and should actually do preprocessing.
 */
@Nullable private CacheEntryWithGlobDeps<Package.Builder> loadPackage(Package externalPkg,@Nullable String replacementContents,PackageIdentifier packageId,Path buildFilePath,@Nullable FileValue buildFileValue,RuleVisibility defaultVisibility,List<Statement> preludeStatements,Path packageRoot,Environment env) throws InterruptedException, PackageFunctionException {
  CacheEntryWithGlobDeps<Package.Builder> packageFunctionCacheEntry=packageFunctionCache.getIfPresent(packageId);
  if (packageFunctionCacheEntry == null) {
    profiler.startTask(ProfilerTask.CREATE_PACKAGE,packageId.toString());
    if (packageProgress != null) {
      packageProgress.startReadPackage(packageId);
    }
    try {
      CacheEntryWithGlobDeps<AstAfterPreprocessing> astCacheEntry=astCache.getIfPresent(packageId);
      if (astCacheEntry == null) {
        if (showLoadingProgress.get()) {
          env.getListener().handle(Event.progress("String_Node_Str" + packageId));
        }
        LegacyGlobber legacyGlobber=packageFactory.createLegacyGlobberThatDoesntSort(buildFilePath.getParentDirectory(),packageId,packageLocator);
        SkyframeHybridGlobber skyframeGlobber=new SkyframeHybridGlobber(packageId,packageRoot,env,legacyGlobber);
        Preprocessor.Result preprocessingResult;
        if (replacementContents == null) {
          Preconditions.checkNotNull(buildFileValue,packageId);
          byte[] buildFileBytes;
          try {
            buildFileBytes=buildFileValue.isSpecialFile() ? FileSystemUtils.readContent(buildFilePath) : FileSystemUtils.readWithKnownFileSize(buildFilePath,buildFileValue.getSize());
          }
 catch (          IOException e) {
            throw new PackageFunctionException(new BuildFileContainsErrorsException(packageId,e.getMessage()),Transience.TRANSIENT);
          }
          try {
            preprocessingResult=packageFactory.preprocess(buildFilePath,packageId,buildFileBytes,skyframeGlobber);
          }
 catch (          IOException e) {
            throw new PackageFunctionException(new BuildFileContainsErrorsException(packageId,"String_Node_Str" + e.getMessage(),e),Transience.TRANSIENT);
          }
        }
 else {
          ParserInputSource replacementSource=ParserInputSource.create(replacementContents,buildFilePath.asFragment());
          preprocessingResult=Preprocessor.Result.noPreprocessing(replacementSource);
        }
        StoredEventHandler astParsingEventHandler=new StoredEventHandler();
        BuildFileAST ast=PackageFactory.parseBuildFile(packageId,preprocessingResult.result,preludeStatements,astParsingEventHandler);
        Set<SkyKey> globDepsRequested=skyframeGlobber.getGlobDepsRequested();
        LegacyGlobber legacyGlobberToStore=globDepsRequested.isEmpty() ? null : legacyGlobber;
        astCacheEntry=new CacheEntryWithGlobDeps<>(new AstAfterPreprocessing(preprocessingResult,ast,astParsingEventHandler),globDepsRequested,legacyGlobberToStore);
        astCache.put(packageId,astCacheEntry);
      }
      AstAfterPreprocessing astAfterPreprocessing=astCacheEntry.value;
      Set<SkyKey> globDepsRequestedDuringPreprocessing=astCacheEntry.globDepKeys;
      SkylarkImportResult importResult;
      try {
        importResult=fetchImportsFromBuildFile(buildFilePath,packageId,astAfterPreprocessing.ast,env,skylarkImportLookupFunctionForInlining);
      }
 catch (      PackageFunctionException|InterruptedException e) {
        astCache.invalidate(packageId);
        throw e;
      }
      if (importResult == null) {
        return null;
      }
      astCache.invalidate(packageId);
      LegacyGlobber legacyGlobber=astCacheEntry.legacyGlobber != null ? astCacheEntry.legacyGlobber : packageFactory.createLegacyGlobber(buildFilePath.getParentDirectory(),packageId,packageLocator);
      SkyframeHybridGlobber skyframeGlobber=new SkyframeHybridGlobber(packageId,packageRoot,env,legacyGlobber);
      Package.Builder pkgBuilder=packageFactory.createPackageFromPreprocessingAst(externalPkg,packageId,buildFilePath,astAfterPreprocessing,importResult.importMap,importResult.fileDependencies,defaultVisibility,skyframeGlobber);
      Set<SkyKey> globDepsRequested=ImmutableSet.<SkyKey>builder().addAll(globDepsRequestedDuringPreprocessing).addAll(skyframeGlobber.getGlobDepsRequested()).build();
      packageFunctionCacheEntry=new CacheEntryWithGlobDeps<>(pkgBuilder,globDepsRequested,null);
      numPackagesLoaded.incrementAndGet();
      if (packageProgress != null) {
        packageProgress.doneReadPackage(packageId);
      }
      packageFunctionCache.put(packageId,packageFunctionCacheEntry);
    }
  finally {
      profiler.completeTask(ProfilerTask.CREATE_PACKAGE);
    }
  }
  return packageFunctionCacheEntry;
}

Fixed version (ground truth):
/** 
 * Constructs a   {@link Package} object for the given package using legacy package loading.Note that the returned package may be in error. <p>May return null if the computation has to be restarted. <p>Exactly one of  {@code replacementContents} and {@code buildFileValue} will benon- {@code null}. The former indicates that we have a faux BUILD file with the given contents and the latter indicates that we have a legitimate BUILD file and should actually do preprocessing.
 */
@Nullable private CacheEntryWithGlobDeps<Package.Builder> loadPackage(Package externalPkg,@Nullable String replacementContents,PackageIdentifier packageId,Path buildFilePath,@Nullable FileValue buildFileValue,RuleVisibility defaultVisibility,List<Statement> preludeStatements,Path packageRoot,Environment env) throws InterruptedException, PackageFunctionException {
  CacheEntryWithGlobDeps<Package.Builder> packageFunctionCacheEntry=packageFunctionCache.getIfPresent(packageId);
  if (packageFunctionCacheEntry == null) {
    profiler.startTask(ProfilerTask.CREATE_PACKAGE,packageId.toString());
    if (packageProgress != null) {
      packageProgress.startReadPackage(packageId);
    }
    try {
      CacheEntryWithGlobDeps<AstAfterPreprocessing> astCacheEntry=astCache.getIfPresent(packageId);
      if (astCacheEntry == null) {
        if (showLoadingProgress.get()) {
          env.getListener().handle(Event.progress("String_Node_Str" + packageId));
        }
        LegacyGlobber legacyGlobber=packageFactory.createLegacyGlobberThatDoesntSort(buildFilePath.getParentDirectory(),packageId,packageLocator);
        SkyframeHybridGlobber skyframeGlobber=new SkyframeHybridGlobber(packageId,packageRoot,env,legacyGlobber);
        Preprocessor.Result preprocessingResult;
        if (replacementContents == null) {
          Preconditions.checkNotNull(buildFileValue,packageId);
          byte[] buildFileBytes;
          try {
            buildFileBytes=buildFileValue.isSpecialFile() ? FileSystemUtils.readContent(buildFilePath) : FileSystemUtils.readWithKnownFileSize(buildFilePath,buildFileValue.getSize());
          }
 catch (          IOException e) {
            throw new PackageFunctionException(new BuildFileContainsErrorsException(packageId,e.getMessage()),Transience.TRANSIENT);
          }
          try {
            preprocessingResult=packageFactory.preprocess(buildFilePath,packageId,buildFileBytes,skyframeGlobber);
          }
 catch (          IOException e) {
            throw new PackageFunctionException(new BuildFileContainsErrorsException(packageId,"String_Node_Str" + e.getMessage(),e),Transience.TRANSIENT);
          }
        }
 else {
          ParserInputSource replacementSource=ParserInputSource.create(replacementContents,buildFilePath.asFragment());
          preprocessingResult=Preprocessor.Result.noPreprocessing(replacementSource);
        }
        StoredEventHandler astParsingEventHandler=new StoredEventHandler();
        BuildFileAST ast=PackageFactory.parseBuildFile(packageId,preprocessingResult.result,preludeStatements,astParsingEventHandler);
        Set<SkyKey> globDepsRequested=skyframeGlobber.getGlobDepsRequested();
        LegacyGlobber legacyGlobberToStore=globDepsRequested.isEmpty() ? null : legacyGlobber;
        astCacheEntry=new CacheEntryWithGlobDeps<>(new AstAfterPreprocessing(preprocessingResult,ast,astParsingEventHandler),globDepsRequested,legacyGlobberToStore);
        astCache.put(packageId,astCacheEntry);
      }
      AstAfterPreprocessing astAfterPreprocessing=astCacheEntry.value;
      Set<SkyKey> globDepsRequestedDuringPreprocessing=astCacheEntry.globDepKeys;
      SkylarkImportResult importResult;
      try {
        importResult=fetchImportsFromBuildFile(buildFilePath,packageId,astAfterPreprocessing.ast,env,skylarkImportLookupFunctionForInlining);
      }
 catch (      NoSuchPackageException e) {
        throw new PackageFunctionException(e,Transience.PERSISTENT);
      }
catch (      InterruptedException e) {
        astCache.invalidate(packageId);
        throw e;
      }
      if (importResult == null) {
        return null;
      }
      astCache.invalidate(packageId);
      LegacyGlobber legacyGlobber=astCacheEntry.legacyGlobber != null ? astCacheEntry.legacyGlobber : packageFactory.createLegacyGlobber(buildFilePath.getParentDirectory(),packageId,packageLocator);
      SkyframeHybridGlobber skyframeGlobber=new SkyframeHybridGlobber(packageId,packageRoot,env,legacyGlobber);
      Package.Builder pkgBuilder=packageFactory.createPackageFromPreprocessingAst(externalPkg,packageId,buildFilePath,astAfterPreprocessing,importResult.importMap,importResult.fileDependencies,defaultVisibility,skyframeGlobber);
      Set<SkyKey> globDepsRequested=ImmutableSet.<SkyKey>builder().addAll(globDepsRequestedDuringPreprocessing).addAll(skyframeGlobber.getGlobDepsRequested()).build();
      packageFunctionCacheEntry=new CacheEntryWithGlobDeps<>(pkgBuilder,globDepsRequested,null);
      numPackagesLoaded.incrementAndGet();
      if (packageProgress != null) {
        packageProgress.doneReadPackage(packageId);
      }
      packageFunctionCache.put(packageId,packageFunctionCacheEntry);
    }
  finally {
      profiler.completeTask(ProfilerTask.CREATE_PACKAGE);
    }
  }
  return packageFunctionCacheEntry;
}