Search in sources :

Example 1 with Console

use of com.facebook.buck.util.Console in project buck by facebook.

the class DistBuildStatusCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    StampedeId stampedeId = getStampedeId();
    Console console = params.getConsole();
    ObjectMapper objectMapper = params.getObjectMapper().copy();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    try (DistBuildService service = DistBuildFactory.newDistBuildService(params)) {
        BuildJob buildJob = service.getCurrentBuildJobState(getStampedeId());
        objectMapper.writeValue(console.getStdOut(), buildJob);
        console.getStdOut().println();
        console.printSuccess(String.format("Successfully fetched the build status for [%s].", stampedeId));
        return 0;
    }
}
Also used : StampedeId(com.facebook.buck.distributed.thrift.StampedeId) Console(com.facebook.buck.util.Console) BuildJob(com.facebook.buck.distributed.thrift.BuildJob) DistBuildService(com.facebook.buck.distributed.DistBuildService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Console

use of com.facebook.buck.util.Console in project buck by facebook.

the class Main method runMainThenExit.

/* Define all error handling surrounding main command */
private void runMainThenExit(String[] args, Optional<NGContext> context, final long initTimestamp) {
    installUncaughtExceptionHandler(context);
    Path projectRoot = Paths.get(".");
    int exitCode = FAIL_EXIT_CODE;
    BuildId buildId = getBuildId(context);
    // Only post an overflow event if Watchman indicates a fresh instance event
    // after our initial query.
    WatchmanWatcher.FreshInstanceAction watchmanFreshInstanceAction = daemon == null ? WatchmanWatcher.FreshInstanceAction.NONE : WatchmanWatcher.FreshInstanceAction.POST_OVERFLOW_EVENT;
    // Get the client environment, either from this process or from the Nailgun context.
    ImmutableMap<String, String> clientEnvironment = getClientEnvironment(context);
    try {
        CommandMode commandMode = CommandMode.RELEASE;
        exitCode = runMainWithExitCode(buildId, projectRoot, context, clientEnvironment, commandMode, watchmanFreshInstanceAction, initTimestamp, args);
    } catch (IOException e) {
        if (e.getMessage().startsWith("No space left on device")) {
            (new Console(Verbosity.STANDARD_INFORMATION, stdOut, stdErr, new Ansi(AnsiEnvironmentChecking.environmentSupportsAnsiEscapes(platform, clientEnvironment)))).printBuildFailure(e.getMessage());
        } else {
            LOG.error(e);
        }
    } catch (HumanReadableException e) {
        Console console = new Console(Verbosity.STANDARD_INFORMATION, stdOut, stdErr, new Ansi(AnsiEnvironmentChecking.environmentSupportsAnsiEscapes(platform, clientEnvironment)));
        console.printBuildFailure(e.getHumanReadableErrorMessage());
    } catch (InterruptionFailedException e) {
        // Command could not be interrupted.
        if (context.isPresent()) {
            // Exit process to halt command execution.
            context.get().getNGServer().shutdown(true);
        }
    } catch (BuckIsDyingException e) {
        LOG.warn(e, "Fallout because buck was already dying");
    } catch (Throwable t) {
        LOG.error(t, "Uncaught exception at top level");
    } finally {
        LOG.debug("Done.");
        LogConfig.flushLogs();
        // Exit explicitly so that non-daemon threads (of which we use many) don't
        // keep the VM alive.
        System.exit(exitCode);
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Path(java.nio.file.Path) CommandMode(com.facebook.buck.util.environment.CommandMode) IOException(java.io.IOException) WatchmanWatcher(com.facebook.buck.util.WatchmanWatcher) InterruptionFailedException(com.facebook.buck.util.InterruptionFailedException) BuildId(com.facebook.buck.model.BuildId) BuckIsDyingException(com.facebook.buck.util.BuckIsDyingException) HumanReadableException(com.facebook.buck.util.HumanReadableException) Console(com.facebook.buck.util.Console) Ansi(com.facebook.buck.util.Ansi)

Example 3 with Console

use of com.facebook.buck.util.Console in project buck by facebook.

the class JavaDepsFinder method findDepsForBuildFiles.

private DepsForBuildFiles findDepsForBuildFiles(final TargetGraph graph, final DependencyInfo dependencyInfo, final Console console) {
    // For the rules that expect to have their deps generated, look through all of their required
    // symbols and try to find the build rule that provides each symbols. Store these build rules in
    // the depsForBuildFiles data structure.
    //
    // Currently, we process each rule with autodeps=True on a single thread. See the class overview
    // for DepsForBuildFiles about what it would take to do this work in a multi-threaded way.
    DepsForBuildFiles depsForBuildFiles = new DepsForBuildFiles();
    for (final TargetNode<?, ?> rule : dependencyInfo.rulesWithAutodeps) {
        final Set<BuildTarget> providedDeps = dependencyInfo.rulesWithAutodepsToProvidedDeps.get(rule);
        final Predicate<TargetNode<?, ?>> isVisibleDepNotAlreadyInProvidedDeps = provider -> provider.isVisibleTo(graph, rule) && !providedDeps.contains(provider.getBuildTarget());
        final boolean isJavaTestRule = rule.getDescription() instanceof JavaTestDescription;
        for (DependencyType type : DependencyType.values()) {
            HashMultimap<TargetNode<?, ?>, String> ruleToSymbolsMap;
            switch(type) {
                case DEPS:
                    ruleToSymbolsMap = dependencyInfo.ruleToRequiredSymbols;
                    break;
                case EXPORTED_DEPS:
                    ruleToSymbolsMap = dependencyInfo.ruleToExportedSymbols;
                    break;
                default:
                    throw new IllegalStateException("Unrecognized type: " + type);
            }
            final DependencyType typeOfDepToAdd;
            if (isJavaTestRule) {
                // java_test rules do not honor exported_deps: add all dependencies to the ordinary deps.
                typeOfDepToAdd = DependencyType.DEPS;
            } else {
                typeOfDepToAdd = type;
            }
            for (String requiredSymbol : ruleToSymbolsMap.get(rule)) {
                BuildTarget provider = findProviderForSymbolFromBuckConfig(requiredSymbol);
                if (provider != null) {
                    depsForBuildFiles.addDep(rule.getBuildTarget(), provider, typeOfDepToAdd);
                    continue;
                }
                Set<TargetNode<?, ?>> providers = dependencyInfo.symbolToProviders.get(requiredSymbol);
                SortedSet<TargetNode<?, ?>> candidateProviders = providers.stream().filter(isVisibleDepNotAlreadyInProvidedDeps).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
                int numCandidates = candidateProviders.size();
                if (numCandidates == 1) {
                    depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(candidateProviders).getBuildTarget(), typeOfDepToAdd);
                } else if (numCandidates > 1) {
                    // Warn the user that there is an ambiguity. This could be very common with macros that
                    // generate multiple versions of a java_library() with the same sources.
                    // If numProviders is 0, then hopefully the dep is provided by something the user
                    // hardcoded in the BUCK file.
                    console.printErrorText(String.format("WARNING: Multiple providers for %s: %s. " + "Consider adding entry to .buckconfig to eliminate ambiguity:\n" + "[autodeps]\n" + "java-package-mappings = %s => %s", requiredSymbol, Joiner.on(", ").join(candidateProviders), requiredSymbol, Iterables.getFirst(candidateProviders, null)));
                } else {
                    // If there aren't any candidates, then see if there is a visible rule that can provide
                    // the symbol via its exported_deps. We make this a secondary check because we prefer to
                    // depend on the rule that defines the symbol directly rather than one of possibly many
                    // rules that provides it via its exported_deps.
                    ImmutableSortedSet<TargetNode<?, ?>> newCandidates = providers.stream().flatMap(candidate -> dependencyInfo.ruleToRulesThatExportIt.get(candidate).stream()).filter(ruleThatExportsCandidate -> ruleThatExportsCandidate.isVisibleTo(graph, rule)).collect(MoreCollectors.toImmutableSortedSet(Comparator.<TargetNode<?, ?>>naturalOrder()));
                    int numNewCandidates = newCandidates.size();
                    if (numNewCandidates == 1) {
                        depsForBuildFiles.addDep(rule.getBuildTarget(), Iterables.getOnlyElement(newCandidates).getBuildTarget(), typeOfDepToAdd);
                    } else if (numNewCandidates > 1) {
                        console.printErrorText(String.format("WARNING: No providers found for '%s' for build rule %s, " + "but there are multiple rules that export a rule to provide %s: %s", requiredSymbol, rule.getBuildTarget(), requiredSymbol, Joiner.on(", ").join(newCandidates)));
                    }
                // In the case that numNewCandidates is 0, we assume that the user is taking
                // responsibility for declaring a provider for the symbol by hardcoding it in the deps.
                }
            }
        }
    }
    return depsForBuildFiles;
}
Also used : Iterables(com.google.common.collect.Iterables) BuildRuleType(com.facebook.buck.rules.BuildRuleType) CellPathResolver(com.facebook.buck.rules.CellPathResolver) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SortedSet(java.util.SortedSet) JavaBuckConfig(com.facebook.buck.jvm.java.JavaBuckConfig) ExecutionContext(com.facebook.buck.step.ExecutionContext) HashSet(java.util.HashSet) AndroidLibraryDescription(com.facebook.buck.android.AndroidLibraryDescription) BuckConfig(com.facebook.buck.cli.BuckConfig) HashMultimap(com.google.common.collect.HashMultimap) PrebuiltJarDescription(com.facebook.buck.jvm.java.PrebuiltJarDescription) JavaFileParser(com.facebook.buck.jvm.java.JavaFileParser) BuildTargetPatternParser(com.facebook.buck.parser.BuildTargetPatternParser) BuildTargetParser(com.facebook.buck.parser.BuildTargetParser) Map(java.util.Map) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) Splitter(com.google.common.base.Splitter) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) DepsForBuildFiles(com.facebook.buck.autodeps.DepsForBuildFiles) Predicate(java.util.function.Predicate) TargetGraph(com.facebook.buck.rules.TargetGraph) TargetNode(com.facebook.buck.rules.TargetNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CharMatcher(com.google.common.base.CharMatcher) Set(java.util.Set) JavacOptions(com.facebook.buck.jvm.java.JavacOptions) Console(com.facebook.buck.util.Console) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription) BuildResult(com.facebook.buck.rules.BuildResult) Futures(com.google.common.util.concurrent.Futures) BuildEngineBuildContext(com.facebook.buck.rules.BuildEngineBuildContext) Stream(java.util.stream.Stream) DependencyType(com.facebook.buck.autodeps.DepsForBuildFiles.DependencyType) BuildEngine(com.facebook.buck.rules.BuildEngine) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) Description(com.facebook.buck.rules.Description) Joiner(com.google.common.base.Joiner) TargetNode(com.facebook.buck.rules.TargetNode) DepsForBuildFiles(com.facebook.buck.autodeps.DepsForBuildFiles) BuildTarget(com.facebook.buck.model.BuildTarget) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) DependencyType(com.facebook.buck.autodeps.DepsForBuildFiles.DependencyType) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription)

Example 4 with Console

use of com.facebook.buck.util.Console in project buck by facebook.

the class BuildCommandTest method testGenerateBuildReportForConsole.

@Test
public void testGenerateBuildReportForConsole() {
    String expectedReport = "OK   //fake:rule1 " + "BUILT_LOCALLY " + MorePaths.pathWithPlatformSeparators("buck-out/gen/fake/rule1.txt") + "\n" + "FAIL //fake:rule2\n" + "OK   //fake:rule3 FETCHED_FROM_CACHE\n" + "FAIL //fake:rule4\n";
    String observedReport = new BuildReport(buildExecutionResult, resolver).generateForConsole(new Console(Verbosity.STANDARD_INFORMATION, new CapturingPrintStream(), new CapturingPrintStream(), Ansi.forceTty()));
    assertEquals(expectedReport, observedReport);
}
Also used : BuildReport(com.facebook.buck.command.BuildReport) CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) Console(com.facebook.buck.util.Console) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 5 with Console

use of com.facebook.buck.util.Console in project buck by facebook.

the class RageCommandIntegrationTest method testExtraInfo.

@Test
public void testExtraInfo() throws Exception {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "interactive_report", temporaryFolder);
    workspace.setUp();
    RageConfig rageConfig = createRageConfig(0, "python, extra.py", RageProtocolVersion.SIMPLE);
    ProjectFilesystem filesystem = new ProjectFilesystem(temporaryFolder.getRoot());
    Console console = new TestConsole();
    CapturingDefectReporter defectReporter = new CapturingDefectReporter();
    AutomatedReport automatedReport = new AutomatedReport(defectReporter, filesystem, ObjectMappers.newDefaultInstance(), new TestConsole(), TestBuildEnvironmentDescription.INSTANCE, VcsInfoCollector.create(new NoOpCmdLineInterface()), rageConfig, new DefaultExtraInfoCollector(rageConfig, filesystem, new DefaultProcessExecutor(console)));
    automatedReport.collectAndSubmitResult();
    DefectReport defectReport = defectReporter.getDefectReport();
    assertThat(defectReport.getExtraInfo(), Matchers.equalTo(Optional.of("Extra\n")));
    assertThat(FluentIterable.from(defectReport.getIncludedPaths()).transform(Object::toString), Matchers.hasItem(Matchers.endsWith("extra.txt")));
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) NoOpCmdLineInterface(com.facebook.buck.util.versioncontrol.NoOpCmdLineInterface) Console(com.facebook.buck.util.Console) TestConsole(com.facebook.buck.testutil.TestConsole) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Aggregations

Console (com.facebook.buck.util.Console)12 Test (org.junit.Test)3 DistBuildService (com.facebook.buck.distributed.DistBuildService)2 BuckEventBus (com.facebook.buck.event.BuckEventBus)2 WebServer (com.facebook.buck.httpserver.WebServer)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 TestConsole (com.facebook.buck.testutil.TestConsole)2 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)2 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)2 HumanReadableException (com.facebook.buck.util.HumanReadableException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 Map (java.util.Map)2 IDevice (com.android.ddmlib.IDevice)1 AndroidBuckConfig (com.facebook.buck.android.AndroidBuckConfig)1 AndroidDirectoryResolver (com.facebook.buck.android.AndroidDirectoryResolver)1 AndroidLibraryDescription (com.facebook.buck.android.AndroidLibraryDescription)1 AndroidPlatformTarget (com.facebook.buck.android.AndroidPlatformTarget)1