Search in sources :

Example 6 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class GraphBackedRecursivePackageProvider method isPackage.

@Override
public boolean isPackage(ExtendedEventHandler eventHandler, PackageIdentifier packageName) throws InterruptedException {
    SkyKey packageLookupKey = PackageLookupValue.key(packageName);
    PackageLookupValue packageLookupValue = (PackageLookupValue) graph.getValue(packageLookupKey);
    if (packageLookupValue == null) {
        // Package lookups can't depend on Skyframe cycles.
        Preconditions.checkState(!graph.isCycle(packageLookupKey), packageLookupKey);
        Exception exception = graph.getException(packageLookupKey);
        if (exception == null) {
            // package, because the SkyQuery environment has already loaded the universe.
            return false;
        } else {
            if (exception instanceof NoSuchPackageException || exception instanceof InconsistentFilesystemException) {
                eventHandler.handle(Event.error(exception.getMessage()));
                return false;
            } else {
                throw new IllegalStateException("During package lookup for '" + packageName + "', got unexpected exception type", exception);
            }
        }
    }
    return packageLookupValue.packageExists();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException)

Example 7 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class GraphBackedRecursivePackageProvider method bulkGetPackages.

@Override
public Map<PackageIdentifier, Package> bulkGetPackages(Iterable<PackageIdentifier> pkgIds) throws NoSuchPackageException, InterruptedException {
    Set<SkyKey> pkgKeys = ImmutableSet.copyOf(PackageValue.keys(pkgIds));
    ImmutableMap.Builder<PackageIdentifier, Package> pkgResults = ImmutableMap.builder();
    Map<SkyKey, SkyValue> packages = graph.getSuccessfulValues(pkgKeys);
    for (Map.Entry<SkyKey, SkyValue> pkgEntry : packages.entrySet()) {
        PackageIdentifier pkgId = (PackageIdentifier) pkgEntry.getKey().argument();
        PackageValue pkgValue = (PackageValue) pkgEntry.getValue();
        pkgResults.put(pkgId, Preconditions.checkNotNull(pkgValue.getPackage(), pkgId));
    }
    SetView<SkyKey> unknownKeys = Sets.difference(pkgKeys, packages.keySet());
    if (!Iterables.isEmpty(unknownKeys)) {
        LOGGER.warning("Unable to find " + unknownKeys + " in the batch lookup of " + pkgKeys + ". Successfully looked up " + packages.keySet());
    }
    for (Map.Entry<SkyKey, Exception> missingOrExceptionEntry : graph.getMissingAndExceptions(unknownKeys).entrySet()) {
        PackageIdentifier pkgIdentifier = (PackageIdentifier) missingOrExceptionEntry.getKey().argument();
        Exception exception = missingOrExceptionEntry.getValue();
        if (exception == null) {
            // package, because the SkyQuery environment has already loaded the universe.
            throw new BuildFileNotFoundException(pkgIdentifier, "Package not found");
        }
        Throwables.propagateIfInstanceOf(exception, NoSuchPackageException.class);
        Throwables.propagate(exception);
    }
    return pkgResults.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) ImmutableMap(com.google.common.collect.ImmutableMap) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) SkyValue(com.google.devtools.build.skyframe.SkyValue) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Package(com.google.devtools.build.lib.packages.Package) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class PackageErrorFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws PackageErrorFunctionException, InterruptedException {
    PackageIdentifier packageIdentifier = (PackageIdentifier) skyKey.argument();
    try {
        SkyKey packageKey = PackageValue.key(packageIdentifier);
        // Callers must have tried to load the package already and gotten the package successfully.
        Package pkg = ((PackageValue) env.getValueOrThrow(packageKey, NoSuchPackageException.class)).getPackage();
        Preconditions.checkState(pkg.containsErrors(), skyKey);
        throw new PackageErrorFunctionException(new BuildFileContainsErrorsException(packageIdentifier), Transience.PERSISTENT);
    } catch (NoSuchPackageException e) {
        throw new IllegalStateException("Function should not have been called on package with exception", e);
    }
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Package(com.google.devtools.build.lib.packages.Package) Nullable(javax.annotation.Nullable)

Example 9 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class PackageFunction method loadPackage.

/**
   * 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 be
   * non-{@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(String workspaceName, @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("Loading package: " + packageId));
                }
                // We use a LegacyGlobber that doesn't sort the matches for each individual glob pattern,
                // since we want to sort the final result anyway.
                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) {
                        // transient.
                        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, "preprocessing failed" + 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);
                // If no globs were fetched during preprocessing, then there's no need to reuse the
                // legacy globber instance during BUILD file evaluation since the performance argument
                // below does not apply.
                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);
            // If a legacy globber was used to evaluate globs during preprocessing, it's important that
            // we reuse that globber during BUILD file evaluation for performance, in the case that
            // globs were fetched lazily during preprocessing. See Preprocessor.Factory#considersGlobs.
            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(workspaceName, 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;
}
Also used : ParserInputSource(com.google.devtools.build.lib.syntax.ParserInputSource) SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) LegacyGlobber(com.google.devtools.build.lib.packages.PackageFactory.LegacyGlobber) AstAfterPreprocessing(com.google.devtools.build.lib.packages.Preprocessor.AstAfterPreprocessing) IOException(java.io.IOException) StoredEventHandler(com.google.devtools.build.lib.events.StoredEventHandler) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Preprocessor(com.google.devtools.build.lib.packages.Preprocessor) Package(com.google.devtools.build.lib.packages.Package) BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST) Nullable(javax.annotation.Nullable)

Example 10 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class TargetPatternPhaseFunction method compute.

@Override
public TargetPatternPhaseValue compute(SkyKey key, Environment env) throws InterruptedException {
    TargetPatternList options = (TargetPatternList) key.argument();
    PackageValue packageValue = null;
    boolean workspaceError = false;
    try {
        packageValue = (PackageValue) env.getValueOrThrow(PackageValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER), NoSuchPackageException.class);
    } catch (NoSuchPackageException e) {
        env.getListener().handle(Event.error(e.getMessage()));
        workspaceError = true;
    }
    if (env.valuesMissing()) {
        return null;
    }
    String workspaceName = "";
    if (!workspaceError) {
        workspaceName = packageValue.getPackage().getWorkspaceName();
    }
    // Determine targets to build:
    ResolvedTargets<Target> targets = getTargetsToBuild(env, options);
    // If the --build_tests_only option was specified or we want to run tests, we need to determine
    // the list of targets to test. For that, we remove manual tests and apply the command-line
    // filters. Also, if --build_tests_only is specified, then the list of filtered targets will be
    // set as build list as well.
    ResolvedTargets<Target> testTargets = null;
    if (options.getDetermineTests() || options.getBuildTestsOnly()) {
        testTargets = determineTests(env, options.getTargetPatterns(), options.getOffset(), options.getTestFilter());
        Preconditions.checkState(env.valuesMissing() || (testTargets != null));
    }
    Map<Label, SkyKey> testExpansionKeys = new LinkedHashMap<>();
    if (targets != null) {
        for (Target target : targets.getTargets()) {
            if (TargetUtils.isTestSuiteRule(target)) {
                Label label = target.getLabel();
                SkyKey testExpansionKey = TestSuiteExpansionValue.key(ImmutableSet.of(label));
                testExpansionKeys.put(label, testExpansionKey);
            }
        }
    }
    Map<SkyKey, SkyValue> expandedTests = env.getValues(testExpansionKeys.values());
    if (env.valuesMissing()) {
        return null;
    }
    ImmutableSet<Target> filteredTargets = targets.getFilteredTargets();
    ImmutableSet<Target> testsToRun = null;
    ImmutableSet<Target> testFilteredTargets = ImmutableSet.of();
    if (testTargets != null) {
        // Parse the targets to get the tests.
        if (testTargets.getTargets().isEmpty() && !testTargets.getFilteredTargets().isEmpty()) {
            env.getListener().handle(Event.warn("All specified test targets were excluded by filters"));
        }
        if (options.getBuildTestsOnly()) {
            // Replace original targets to build with test targets, so that only targets that are
            // actually going to be built are loaded in the loading phase. Note that this has a side
            // effect that any test_suite target requested to be built is replaced by the set of *_test
            // targets it represents; for example, this affects the status and the summary reports.
            Set<Target> allFilteredTargets = new HashSet<>();
            allFilteredTargets.addAll(targets.getTargets());
            allFilteredTargets.addAll(targets.getFilteredTargets());
            allFilteredTargets.removeAll(testTargets.getTargets());
            allFilteredTargets.addAll(testTargets.getFilteredTargets());
            testFilteredTargets = ImmutableSet.copyOf(allFilteredTargets);
            filteredTargets = ImmutableSet.of();
            targets = ResolvedTargets.<Target>builder().merge(testTargets).mergeError(targets.hasError()).build();
            if (options.getDetermineTests()) {
                testsToRun = testTargets.getTargets();
            }
        } else /*if (determineTests)*/
        {
            testsToRun = testTargets.getTargets();
            targets = ResolvedTargets.<Target>builder().merge(targets).addAll(testsToRun).mergeError(testTargets.hasError()).build();
        // filteredTargets is correct in this case - it cannot contain tests that got back in
        // through test_suite expansion, because the test determination would also filter those out.
        // However, that's not obvious, and it might be better to explicitly recompute it.
        }
        if (testsToRun != null) {
            // Note that testsToRun can still be null here, if buildTestsOnly && !shouldRunTests.
            if (!targets.getTargets().containsAll(testsToRun)) {
                throw new IllegalStateException(String.format("Internal consistency check failed; some targets are scheduled for test execution " + "but not for building (%s)", Sets.difference(testsToRun, targets.getTargets())));
            }
        }
    }
    if (targets.hasError()) {
        env.getListener().handle(Event.warn("Target pattern parsing failed."));
    }
    LoadingPhaseRunner.maybeReportDeprecation(env.getListener(), targets.getTargets());
    boolean preExpansionError = targets.hasError();
    ResolvedTargets.Builder<Target> expandedTargetsBuilder = ResolvedTargets.builder();
    for (Target target : targets.getTargets()) {
        if (TargetUtils.isTestSuiteRule(target)) {
            SkyKey expansionKey = Preconditions.checkNotNull(testExpansionKeys.get(target.getLabel()));
            TestSuiteExpansionValue testExpansion = (TestSuiteExpansionValue) expandedTests.get(expansionKey);
            expandedTargetsBuilder.merge(testExpansion.getTargets());
        } else {
            expandedTargetsBuilder.add(target);
        }
    }
    ResolvedTargets<Target> expandedTargets = expandedTargetsBuilder.build();
    Set<Target> testSuiteTargets = Sets.difference(targets.getTargets(), expandedTargets.getTargets());
    return new TargetPatternPhaseValue(expandedTargets.getTargets(), testsToRun, preExpansionError, expandedTargets.hasError() || workspaceError, filteredTargets, testFilteredTargets, targets.getTargets(), ImmutableSet.copyOf(testSuiteTargets), workspaceName);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Label(com.google.devtools.build.lib.cmdline.Label) LinkedHashMap(java.util.LinkedHashMap) SkyValue(com.google.devtools.build.skyframe.SkyValue) Target(com.google.devtools.build.lib.packages.Target) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets) TargetPatternList(com.google.devtools.build.lib.skyframe.TargetPatternPhaseValue.TargetPatternList) HashSet(java.util.HashSet)

Aggregations

NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)27 SkyKey (com.google.devtools.build.skyframe.SkyKey)21 Package (com.google.devtools.build.lib.packages.Package)13 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)11 Label (com.google.devtools.build.lib.cmdline.Label)10 Target (com.google.devtools.build.lib.packages.Target)8 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)7 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)6 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)6 Nullable (javax.annotation.Nullable)6 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)5 IOException (java.io.IOException)5 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 SkyValue (com.google.devtools.build.skyframe.SkyValue)4 ValueOrException2 (com.google.devtools.build.skyframe.ValueOrException2)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Path (com.google.devtools.build.lib.vfs.Path)3 Map (java.util.Map)3 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)2 Attribute (com.google.devtools.build.lib.packages.Attribute)2