Search in sources :

Example 1 with ErrorDeterminingRepositoryException

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

the class LocalRepositoryLookupFunction method maybeCheckWorkspaceForRepository.

/**
   * Checks whether the directory exists and is a workspace root. Returns {@link Optional#absent()}
   * if Skyframe needs to re-run, {@link Optional#of(LocalRepositoryLookupValue)} otherwise.
   */
private Optional<LocalRepositoryLookupValue> maybeCheckWorkspaceForRepository(Environment env, final RootedPath directory) throws InterruptedException, LocalRepositoryLookupFunctionException {
    // Look up the main WORKSPACE file by the external package, to find all repositories.
    PackageLookupValue externalPackageLookupValue;
    try {
        externalPackageLookupValue = (PackageLookupValue) env.getValueOrThrow(PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
        if (externalPackageLookupValue == null) {
            return Optional.absent();
        }
    } catch (BuildFileNotFoundException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("BuildFileNotFoundException while loading the //external package", e), Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("InconsistentFilesystemException while loading the //external package", e), Transience.PERSISTENT);
    }
    RootedPath workspacePath = externalPackageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
    do {
        WorkspaceFileValue value;
        try {
            value = (WorkspaceFileValue) env.getValueOrThrow(workspaceKey, PackageFunctionException.class, NameConflictException.class);
            if (value == null) {
                return Optional.absent();
            }
        } catch (PackageFunctionException e) {
            // TODO(jcater): When WFF is rewritten to not throw a PFE, update this.
            throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("PackageFunctionException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
        } catch (NameConflictException e) {
            throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("NameConflictException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
        }
        Package externalPackage = value.getPackage();
        if (externalPackage.containsErrors()) {
            Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
        }
        // Find all local_repository rules in the WORKSPACE, and check if any have a "path" attribute
        // the same as the requested directory.
        Iterable<Rule> localRepositories = externalPackage.getRulesMatchingRuleClass(LocalRepositoryRule.NAME);
        Rule rule = Iterables.find(localRepositories, new Predicate<Rule>() {

            @Override
            public boolean apply(@Nullable Rule rule) {
                AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
                PathFragment pathAttr = new PathFragment(mapper.get("path", Type.STRING));
                return directory.getRelativePath().equals(pathAttr);
            }
        }, null);
        if (rule != null) {
            try {
                return Optional.of(LocalRepositoryLookupValue.success(RepositoryName.create("@" + rule.getName())));
            } catch (LabelSyntaxException e) {
                // validated.
                throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("LabelSyntaxException while creating the repository name from the rule " + rule.getName(), e), Transience.PERSISTENT);
            }
        }
        workspaceKey = value.next();
    // TODO(bazel-team): This loop can be quadratic in the number of load() statements, consider
    // rewriting or unrolling.
    } while (workspaceKey != null);
    return Optional.of(LocalRepositoryLookupValue.notFound());
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) ErrorDeterminingRepositoryException(com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) PackageFunctionException(com.google.devtools.build.lib.skyframe.PackageFunction.PackageFunctionException) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Package(com.google.devtools.build.lib.packages.Package) LocalRepositoryRule(com.google.devtools.build.lib.rules.repository.LocalRepositoryRule) Rule(com.google.devtools.build.lib.packages.Rule) AggregatingAttributeMapper(com.google.devtools.build.lib.packages.AggregatingAttributeMapper)

Example 2 with ErrorDeterminingRepositoryException

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

the class PackageLookupFunction method getPackageLookupValue.

private PackageLookupValue getPackageLookupValue(Environment env, Path packagePathEntry, PackageIdentifier packageIdentifier, BuildFileName buildFileName) throws InterruptedException, PackageLookupFunctionException {
    PathFragment buildFileFragment = buildFileName.getBuildFileFragment(packageIdentifier);
    RootedPath buildFileRootedPath = RootedPath.toRootedPath(packagePathEntry, buildFileFragment);
    if (crossRepositoryLabelViolationStrategy == CrossRepositoryLabelViolationStrategy.ERROR) {
        // Is this path part of a local repository?
        RootedPath currentPath = RootedPath.toRootedPath(packagePathEntry, buildFileFragment.getParentDirectory());
        SkyKey repositoryLookupKey = LocalRepositoryLookupValue.key(currentPath);
        // TODO(jcater): Consider parallelizing these lookups.
        LocalRepositoryLookupValue localRepository;
        try {
            localRepository = (LocalRepositoryLookupValue) env.getValueOrThrow(repositoryLookupKey, ErrorDeterminingRepositoryException.class);
            if (localRepository == null) {
                return null;
            }
        } catch (ErrorDeterminingRepositoryException e) {
            // TODO(katre): Improve the error message given here.
            throw new PackageLookupFunctionException(new BuildFileNotFoundException(packageIdentifier, "Unable to determine the local repository for directory " + currentPath.asPath().getPathString()), Transience.PERSISTENT);
        }
        if (localRepository.exists() && !localRepository.getRepository().equals(packageIdentifier.getRepository())) {
            // TODO(jcater): Work out the correct package name for this error message.
            return PackageLookupValue.invalidPackageName("Package crosses into repository " + localRepository.getRepository().getName());
        }
    // There's no local repository, keep going.
    } else {
        // Future-proof against adding future values to CrossRepositoryLabelViolationStrategy.
        Preconditions.checkState(crossRepositoryLabelViolationStrategy == CrossRepositoryLabelViolationStrategy.IGNORE, crossRepositoryLabelViolationStrategy);
    }
    // Check for the existence of the build file.
    FileValue fileValue = getFileValue(buildFileRootedPath, env, packageIdentifier);
    if (fileValue == null) {
        return null;
    }
    if (fileValue.isFile()) {
        return PackageLookupValue.success(buildFileRootedPath.getRoot(), buildFileName);
    }
    return PackageLookupValue.NO_BUILD_FILE_VALUE;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) ErrorDeterminingRepositoryException(com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RootedPath(com.google.devtools.build.lib.vfs.RootedPath)

Example 3 with ErrorDeterminingRepositoryException

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

the class LocalRepositoryLookupFunction method maybeGetWorkspaceFileExistence.

private Optional<Boolean> maybeGetWorkspaceFileExistence(Environment env, RootedPath directory) throws InterruptedException, LocalRepositoryLookupFunctionException {
    try {
        RootedPath workspaceRootedFile = RootedPath.toRootedPath(directory.getRoot(), directory.getRelativePath().getChild(PackageLookupValue.BuildFileName.WORKSPACE.getFilename()));
        FileValue workspaceFileValue = (FileValue) env.getValueOrThrow(FileValue.key(workspaceRootedFile), IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class);
        if (workspaceFileValue == null) {
            return Optional.absent();
        }
        if (workspaceFileValue.isDirectory()) {
            // There is a directory named WORKSPACE, ignore it for checking repository existence.
            return Optional.of(false);
        }
        return Optional.of(workspaceFileValue.exists());
    } catch (IOException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("IOException while checking if there is a WORKSPACE file in " + directory.asPath().getPathString(), e), Transience.PERSISTENT);
    } catch (FileSymlinkException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("FileSymlinkException while checking if there is a WORKSPACE file in " + directory.asPath().getPathString(), e), Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("InconsistentFilesystemException while checking if there is a WORKSPACE file in " + directory.asPath().getPathString(), e), Transience.PERSISTENT);
    }
}
Also used : ErrorDeterminingRepositoryException(com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException) IOException(java.io.IOException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath)

Aggregations

ErrorDeterminingRepositoryException (com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException)3 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)3 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)2 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)2 SkyKey (com.google.devtools.build.skyframe.SkyKey)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 AggregatingAttributeMapper (com.google.devtools.build.lib.packages.AggregatingAttributeMapper)1 Package (com.google.devtools.build.lib.packages.Package)1 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)1 Rule (com.google.devtools.build.lib.packages.Rule)1 LocalRepositoryRule (com.google.devtools.build.lib.rules.repository.LocalRepositoryRule)1 PackageFunctionException (com.google.devtools.build.lib.skyframe.PackageFunction.PackageFunctionException)1 IOException (java.io.IOException)1