Search in sources :

Example 6 with BuildFileParseException

use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.

the class AuditRulesCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    ProjectFilesystem projectFilesystem = params.getCell().getFilesystem();
    try (ProjectBuildFileParser parser = params.getCell().createBuildFileParser(new ConstructorArgMarshaller(new DefaultTypeCoercerFactory(params.getObjectMapper())), params.getConsole(), params.getBuckEventBus(), /* ignoreBuckAutodepsFiles */
    false)) {
        PrintStream out = params.getConsole().getStdOut();
        for (String pathToBuildFile : getArguments()) {
            if (!json) {
                // Print a comment with the path to the build file.
                out.printf("# %s\n\n", pathToBuildFile);
            }
            // Resolve the path specified by the user.
            Path path = Paths.get(pathToBuildFile);
            if (!path.isAbsolute()) {
                Path root = projectFilesystem.getRootPath();
                path = root.resolve(path);
            }
            // Parse the rules from the build file.
            List<Map<String, Object>> rawRules;
            try {
                rawRules = parser.getAll(path);
            } catch (BuildFileParseException e) {
                throw new HumanReadableException(e);
            }
            // Format and print the rules from the raw data, filtered by type.
            final ImmutableSet<String> types = getTypes();
            Predicate<String> includeType = type -> types.isEmpty() || types.contains(type);
            printRulesToStdout(params, rawRules, includeType);
        }
    } catch (BuildFileParseException e) {
        throw new HumanReadableException("Unable to create parser");
    }
    return 0;
}
Also used : Path(java.nio.file.Path) SortedSet(java.util.SortedSet) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HashMap(java.util.HashMap) MoreStrings(com.facebook.buck.util.MoreStrings) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) Escaper(com.facebook.buck.util.Escaper) BuckPyFunction(com.facebook.buck.rules.BuckPyFunction) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) PrintStream(java.io.PrintStream) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) HumanReadableException(com.facebook.buck.util.HumanReadableException) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) List(java.util.List) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Predicate(com.google.common.base.Predicate) Paths(java.nio.file.Paths) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PrintStream(java.io.PrintStream) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) HumanReadableException(com.facebook.buck.util.HumanReadableException) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with BuildFileParseException

use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.

the class BuildCommand method executeDistributedBuild.

private int executeDistributedBuild(final CommandRunnerParams params, ActionAndTargetGraphs graphs, final WeightedListeningExecutorService executorService) throws IOException, InterruptedException {
    // Distributed builds serialize and send the unversioned target graph,
    // and then deserialize and version remotely.
    TargetGraphAndBuildTargets targetGraphAndBuildTargets = graphs.unversionedTargetGraph;
    ProjectFilesystem filesystem = params.getCell().getFilesystem();
    FileHashCache fileHashCache = params.getFileHashCache();
    DistBuildTypeCoercerFactory typeCoercerFactory = new DistBuildTypeCoercerFactory(params.getObjectMapper());
    ParserTargetNodeFactory<TargetNode<?, ?>> parserTargetNodeFactory = DefaultParserTargetNodeFactory.createForDistributedBuild(new ConstructorArgMarshaller(typeCoercerFactory), new TargetNodeFactory(typeCoercerFactory));
    DistBuildTargetGraphCodec targetGraphCodec = new DistBuildTargetGraphCodec(params.getObjectMapper(), parserTargetNodeFactory, new Function<TargetNode<?, ?>, Map<String, Object>>() {

        @Nullable
        @Override
        public Map<String, Object> apply(TargetNode<?, ?> input) {
            try {
                return params.getParser().getRawTargetNode(params.getBuckEventBus(), params.getCell().getCell(input.getBuildTarget()), false, /* enableProfiling */
                executorService, input);
            } catch (BuildFileParseException e) {
                throw new RuntimeException(e);
            }
        }
    }, targetGraphAndBuildTargets.getBuildTargets().stream().map(t -> t.getFullyQualifiedName()).collect(Collectors.toSet()));
    BuildJobState jobState = computeDistributedBuildJobState(targetGraphCodec, params, targetGraphAndBuildTargets, graphs.actionGraph, executorService);
    if (distributedBuildStateFile != null) {
        Path stateDumpPath = Paths.get(distributedBuildStateFile);
        BuildJobStateSerializer.serialize(jobState, filesystem.newFileOutputStream(stateDumpPath));
        return 0;
    } else {
        BuckVersion buckVersion = getBuckVersion();
        Preconditions.checkArgument(params.getInvocationInfo().isPresent());
        try (DistBuildService service = DistBuildFactory.newDistBuildService(params);
            DistBuildLogStateTracker distBuildLogStateTracker = DistBuildFactory.newDistBuildLogStateTracker(params.getInvocationInfo().get().getLogDirectoryPath(), filesystem)) {
            DistBuildClientExecutor build = new DistBuildClientExecutor(jobState, service, distBuildLogStateTracker, 1000, /* millisBetweenStatusPoll */
            buckVersion);
            int exitCode = build.executeAndPrintFailuresToEventBus(executorService, filesystem, fileHashCache, params.getBuckEventBus());
            // TODO(shivanker): Add a flag to disable building, and only fetch from the cache.
            if (exitCode == 0) {
                exitCode = executeLocalBuild(params, graphs.actionGraph, executorService);
            }
            return exitCode;
        }
    }
}
Also used : Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) FileHashCache(com.facebook.buck.util.cache.FileHashCache) TargetNode(com.facebook.buck.rules.TargetNode) BuildJobState(com.facebook.buck.distributed.thrift.BuildJobState) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) DistBuildService(com.facebook.buck.distributed.DistBuildService) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) BuckVersion(com.facebook.buck.distributed.thrift.BuckVersion) TargetNodeFactory(com.facebook.buck.rules.TargetNodeFactory) DefaultParserTargetNodeFactory(com.facebook.buck.parser.DefaultParserTargetNodeFactory) ParserTargetNodeFactory(com.facebook.buck.parser.ParserTargetNodeFactory) DistBuildClientExecutor(com.facebook.buck.distributed.DistBuildClientExecutor) DistBuildTargetGraphCodec(com.facebook.buck.distributed.DistBuildTargetGraphCodec) DistBuildLogStateTracker(com.facebook.buck.distributed.DistBuildLogStateTracker) DistBuildTypeCoercerFactory(com.facebook.buck.distributed.DistBuildTypeCoercerFactory) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) TargetGraphAndBuildTargets(com.facebook.buck.rules.TargetGraphAndBuildTargets) Nullable(javax.annotation.Nullable)

Example 8 with BuildFileParseException

use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.

the class AuditFlavorsCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    ImmutableSet<BuildTarget> targets = getArgumentsFormattedAsBuildTargets(params.getBuckConfig()).stream().map(input -> BuildTargetParser.INSTANCE.parse(input, BuildTargetPatternParser.fullyQualified(), params.getCell().getCellPathResolver())).collect(MoreCollectors.toImmutableSet());
    if (targets.isEmpty()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("Please specify at least one build target."));
        return 1;
    }
    ImmutableList.Builder<TargetNode<?, ?>> builder = ImmutableList.builder();
    try (CommandThreadManager pool = new CommandThreadManager("Audit", getConcurrencyLimit(params.getBuckConfig()))) {
        for (BuildTarget target : targets) {
            TargetNode<?, ?> targetNode = params.getParser().getTargetNode(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), target);
            builder.add(targetNode);
        }
    } catch (BuildFileParseException | BuildTargetException e) {
        params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
        return 1;
    }
    ImmutableList<TargetNode<?, ?>> targetNodes = builder.build();
    if (shouldGenerateJsonOutput()) {
        printJsonFlavors(targetNodes, params);
    } else {
        printFlavors(targetNodes, params);
    }
    return 0;
}
Also used : MoreExceptions(com.facebook.buck.util.MoreExceptions) RichStream(com.facebook.buck.util.RichStream) Flavored(com.facebook.buck.model.Flavored) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) FlavorDomain(com.facebook.buck.model.FlavorDomain) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ImmutableList(com.google.common.collect.ImmutableList) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSet(com.google.common.collect.ImmutableSet) TargetNode(com.facebook.buck.rules.TargetNode) BuildTargetException(com.facebook.buck.model.BuildTargetException) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) BuildTarget(com.facebook.buck.model.BuildTarget) DirtyPrintStreamDecorator(com.facebook.buck.util.DirtyPrintStreamDecorator) List(java.util.List) TreeMap(java.util.TreeMap) UserFlavor(com.facebook.buck.model.UserFlavor) Optional(java.util.Optional) SortedMap(java.util.SortedMap) Description(com.facebook.buck.rules.Description) BuildTargetException(com.facebook.buck.model.BuildTargetException) TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableList(com.google.common.collect.ImmutableList) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Example 9 with BuildFileParseException

use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.

the class DaemonicParserState method invalidateBasedOn.

public void invalidateBasedOn(WatchEvent<?> event) {
    if (!WatchEvents.isPathChangeEvent(event)) {
        // Non-path change event, likely an overflow due to many change events: invalidate everything.
        LOG.debug("Received non-path change event %s, assuming overflow and checking caches.", event);
        if (invalidateAllCaches()) {
            LOG.warn("Invalidated cache on watch event %s.", event);
            cacheInvalidatedByWatchOverflowCounter.inc();
        }
        return;
    }
    filesChangedCounter.inc();
    Path path = (Path) event.context();
    try (AutoCloseableLock readLock = cellStateLock.readLock()) {
        for (DaemonicCellState state : cellPathToDaemonicState.values()) {
            try {
                // rule key change.  For parsing, these are the only events we need to care about.
                if (isPathCreateOrDeleteEvent(event)) {
                    Cell cell = state.getCell();
                    BuildFileTree buildFiles = buildFileTrees.get(cell);
                    if (path.endsWith(cell.getBuildFileName())) {
                        LOG.debug("Build file %s changed, invalidating build file tree for cell %s", path, cell);
                        // If a build file has been added or removed, reconstruct the build file tree.
                        buildFileTrees.invalidate(cell);
                    }
                    // "containing" {@code path} unless its filename matches a temp file pattern.
                    if (!isTempFile(cell, path)) {
                        invalidateContainingBuildFile(cell, buildFiles, path);
                    } else {
                        LOG.debug("Not invalidating the owning build file of %s because it is a temporary file.", state.getCellRoot().resolve(path).toAbsolutePath().toString());
                    }
                }
            } catch (ExecutionException | UncheckedExecutionException e) {
                try {
                    Throwables.throwIfInstanceOf(e, BuildFileParseException.class);
                    Throwables.throwIfUnchecked(e);
                    throw new RuntimeException(e);
                } catch (BuildFileParseException bfpe) {
                    LOG.warn("Unable to parse already parsed build file.", bfpe);
                }
            }
        }
    }
    invalidatePath(path);
}
Also used : Path(java.nio.file.Path) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) BuildFileTree(com.facebook.buck.model.BuildFileTree) FilesystemBackedBuildFileTree(com.facebook.buck.model.FilesystemBackedBuildFileTree) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Cell(com.facebook.buck.rules.Cell) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Example 10 with BuildFileParseException

use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.

the class ParsePipelineTest method recoversAfterSyntaxError.

@Test
public void recoversAfterSyntaxError() throws Exception {
    try (Fixture fixture = createSynchronousExecutionFixture("syntax_error")) {
        final Cell cell = fixture.getCell();
        try {
            fixture.getTargetNodeParsePipeline().getNode(cell, BuildTargetFactory.newInstance(cell.getFilesystem(), "//error:error"));
            Assert.fail("Expected BuildFileParseException");
        } catch (BuildFileParseException e) {
            assertThat(e.getMessage(), containsString("crash!"));
        }
        fixture.getTargetNodeParsePipeline().getNode(cell, BuildTargetFactory.newInstance(cell.getFilesystem(), "//correct:correct"));
    }
}
Also used : Cell(com.facebook.buck.rules.Cell) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Test(org.junit.Test)

Aggregations

BuildFileParseException (com.facebook.buck.json.BuildFileParseException)24 BuildTarget (com.facebook.buck.model.BuildTarget)14 BuildTargetException (com.facebook.buck.model.BuildTargetException)13 Path (java.nio.file.Path)13 IOException (java.io.IOException)12 TargetNode (com.facebook.buck.rules.TargetNode)11 HumanReadableException (com.facebook.buck.util.HumanReadableException)11 ImmutableSet (com.google.common.collect.ImmutableSet)11 ImmutableList (com.google.common.collect.ImmutableList)10 Cell (com.facebook.buck.rules.Cell)8 ImmutableMap (com.google.common.collect.ImmutableMap)8 Map (java.util.Map)8 Nullable (javax.annotation.Nullable)8 TargetGraph (com.facebook.buck.rules.TargetGraph)7 Lists (com.google.common.collect.Lists)7 List (java.util.List)7 Optional (java.util.Optional)7 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)6 ParserConfig (com.facebook.buck.parser.ParserConfig)6 TargetGraphAndBuildTargets (com.facebook.buck.rules.TargetGraphAndBuildTargets)6