Search in sources :

Example 1 with NameConflictException

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

the class WorkspaceFileFunction method compute.

@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws WorkspaceFileFunctionException, InterruptedException {
    WorkspaceFileKey key = (WorkspaceFileKey) skyKey.argument();
    RootedPath workspaceRoot = key.getPath();
    WorkspaceASTValue workspaceASTValue = (WorkspaceASTValue) env.getValue(WorkspaceASTValue.key(workspaceRoot));
    if (workspaceASTValue == null) {
        return null;
    }
    Path repoWorkspace = workspaceRoot.getRoot().getRelative(workspaceRoot.getRelativePath());
    Package.Builder builder = packageFactory.newExternalPackageBuilder(repoWorkspace, ruleClassProvider.getRunfilesPrefix());
    if (workspaceASTValue.getASTs().isEmpty()) {
        return new WorkspaceFileValue(// resulting package
        builder.build(), // list of imports
        ImmutableMap.<String, Extension>of(), // list of symbol bindings
        ImmutableMap.<String, Object>of(), // Workspace root
        workspaceRoot, // first fragment, idx = 0
        0, // last fragment
        false);
    }
    WorkspaceFactory parser;
    try (Mutability mutability = Mutability.create("workspace %s", repoWorkspace)) {
        parser = new WorkspaceFactory(builder, ruleClassProvider, packageFactory.getEnvironmentExtensions(), mutability, key.getIndex() == 0, directories.getEmbeddedBinariesRoot(), directories.getWorkspace());
        if (key.getIndex() > 0) {
            WorkspaceFileValue prevValue = (WorkspaceFileValue) env.getValue(WorkspaceFileValue.key(key.getPath(), key.getIndex() - 1));
            if (prevValue == null) {
                return null;
            }
            if (prevValue.next() == null) {
                return prevValue;
            }
            parser.setParent(prevValue.getPackage(), prevValue.getImportMap(), prevValue.getBindings());
        }
        BuildFileAST ast = workspaceASTValue.getASTs().get(key.getIndex());
        PackageFunction.SkylarkImportResult importResult = PackageFunction.fetchImportsFromBuildFile(repoWorkspace, rootPackage, ast, env, null);
        if (importResult == null) {
            return null;
        }
        parser.execute(ast, importResult.importMap);
    } catch (NoSuchPackageException e) {
        throw new WorkspaceFileFunctionException(e, Transience.PERSISTENT);
    } catch (NameConflictException e) {
        throw new WorkspaceFileFunctionException(e, Transience.PERSISTENT);
    }
    return new WorkspaceFileValue(builder.build(), parser.getImportMap(), parser.getVariableBindings(), workspaceRoot, key.getIndex(), key.getIndex() < workspaceASTValue.getASTs().size() - 1);
}
Also used : RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) WorkspaceFactory(com.google.devtools.build.lib.packages.WorkspaceFactory) Mutability(com.google.devtools.build.lib.syntax.Mutability) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) WorkspaceFileKey(com.google.devtools.build.lib.skyframe.WorkspaceFileValue.WorkspaceFileKey) Package(com.google.devtools.build.lib.packages.Package) BuildFileAST(com.google.devtools.build.lib.syntax.BuildFileAST)

Example 2 with NameConflictException

use of com.google.devtools.build.lib.packages.Package.NameConflictException 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)

Aggregations

Package (com.google.devtools.build.lib.packages.Package)2 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)2 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 AggregatingAttributeMapper (com.google.devtools.build.lib.packages.AggregatingAttributeMapper)1 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)1 ErrorDeterminingRepositoryException (com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException)1 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)1 Rule (com.google.devtools.build.lib.packages.Rule)1 WorkspaceFactory (com.google.devtools.build.lib.packages.WorkspaceFactory)1 LocalRepositoryRule (com.google.devtools.build.lib.rules.repository.LocalRepositoryRule)1 PackageFunctionException (com.google.devtools.build.lib.skyframe.PackageFunction.PackageFunctionException)1 WorkspaceFileKey (com.google.devtools.build.lib.skyframe.WorkspaceFileValue.WorkspaceFileKey)1 BuildFileAST (com.google.devtools.build.lib.syntax.BuildFileAST)1 Mutability (com.google.devtools.build.lib.syntax.Mutability)1 Path (com.google.devtools.build.lib.vfs.Path)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 SkyKey (com.google.devtools.build.skyframe.SkyKey)1