use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.
the class AuditClasspathCommand method runWithoutHelp.
@Override
public int runWithoutHelp(final CommandRunnerParams params) throws IOException, InterruptedException {
// Create a TargetGraph that is composed of the transitive closure of all of the dependent
// BuildRules for the specified BuildTargets.
final 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;
}
TargetGraph targetGraph;
try (CommandThreadManager pool = new CommandThreadManager("Audit", getConcurrencyLimit(params.getBuckConfig()))) {
targetGraph = params.getParser().buildTargetGraph(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), targets);
} catch (BuildFileParseException | BuildTargetException e) {
params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
return 1;
}
try {
if (shouldGenerateDotOutput()) {
return printDotOutput(params, targetGraph);
} else if (shouldGenerateJsonOutput()) {
return printJsonClasspath(params, targetGraph, targets);
} else {
return printClasspath(params, targetGraph, targets);
}
} catch (NoSuchBuildTargetException | VersionException e) {
throw new HumanReadableException(e, MoreExceptions.getHumanReadableOrLocalizedMessage(e));
}
}
use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.
the class BuckQueryEnvironment method getFileOwners.
@Override
public ImmutableSet<QueryTarget> getFileOwners(ImmutableList<String> files, ListeningExecutorService executor) throws InterruptedException, QueryException {
try {
BuildFileTree buildFileTree = Preconditions.checkNotNull(buildFileTrees.get(rootCell));
OwnersReport report = ownersReportBuilder.build(buildFileTree, executor, files);
return getTargetsFromTargetNodes(report.owners.keySet());
} catch (BuildFileParseException | IOException e) {
throw new QueryException(e, "Could not parse build targets.\n%s", e.getMessage());
}
}
use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.
the class TestCommand method runWithoutHelp.
@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
LOG.debug("Running with arguments %s", getArguments());
try (CommandThreadManager pool = new CommandThreadManager("Test", getConcurrencyLimit(params.getBuckConfig()))) {
// Post the build started event, setting it to the Parser recorded start time if appropriate.
BuildEvent.Started started = BuildEvent.started(getArguments());
if (params.getParser().getParseStartTime().isPresent()) {
params.getBuckEventBus().post(started, params.getParser().getParseStartTime().get());
} else {
params.getBuckEventBus().post(started);
}
// The first step is to parse all of the build files. This will populate the parser and find
// all of the test rules.
TargetGraphAndBuildTargets targetGraphAndBuildTargets;
ParserConfig parserConfig = params.getBuckConfig().getView(ParserConfig.class);
try {
// If the user asked to run all of the tests, parse all of the build files looking for any
// test rules.
boolean ignoreBuckAutodepsFiles = false;
if (isRunAllTests()) {
targetGraphAndBuildTargets = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), ImmutableList.of(TargetNodePredicateSpec.of(input -> Description.getBuildRuleType(input.getDescription()).isTestRule(), BuildFileSpec.fromRecursivePath(Paths.get(""), params.getCell().getRoot()))), ignoreBuckAutodepsFiles, parserConfig.getDefaultFlavorsMode());
targetGraphAndBuildTargets = targetGraphAndBuildTargets.withBuildTargets(ImmutableSet.of());
// Otherwise, the user specified specific test targets to build and run, so build a graph
// around these.
} else {
LOG.debug("Parsing graph for arguments %s", getArguments());
targetGraphAndBuildTargets = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), parseArgumentsAsTargetNodeSpecs(params.getBuckConfig(), getArguments()), ignoreBuckAutodepsFiles, parserConfig.getDefaultFlavorsMode());
LOG.debug("Got explicit build targets %s", targetGraphAndBuildTargets.getBuildTargets());
ImmutableSet.Builder<BuildTarget> testTargetsBuilder = ImmutableSet.builder();
for (TargetNode<?, ?> node : targetGraphAndBuildTargets.getTargetGraph().getAll(targetGraphAndBuildTargets.getBuildTargets())) {
ImmutableSortedSet<BuildTarget> nodeTests = TargetNodes.getTestTargetsForNode(node);
if (!nodeTests.isEmpty()) {
LOG.debug("Got tests for target %s: %s", node.getBuildTarget(), nodeTests);
testTargetsBuilder.addAll(nodeTests);
}
}
ImmutableSet<BuildTarget> testTargets = testTargetsBuilder.build();
if (!testTargets.isEmpty()) {
LOG.debug("Got related test targets %s, building new target graph...", testTargets);
TargetGraph targetGraph = params.getParser().buildTargetGraph(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), Iterables.concat(targetGraphAndBuildTargets.getBuildTargets(), testTargets));
LOG.debug("Finished building new target graph with tests.");
targetGraphAndBuildTargets = targetGraphAndBuildTargets.withTargetGraph(targetGraph);
}
}
if (params.getBuckConfig().getBuildVersions()) {
targetGraphAndBuildTargets = toVersionedTargetGraph(params, targetGraphAndBuildTargets);
}
} catch (BuildTargetException | BuildFileParseException | VersionException e) {
params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
return 1;
}
ActionGraphAndResolver actionGraphAndResolver = Preconditions.checkNotNull(params.getActionGraphCache().getActionGraph(params.getBuckEventBus(), params.getBuckConfig().isActionGraphCheckingEnabled(), params.getBuckConfig().isSkipActionGraphCache(), targetGraphAndBuildTargets.getTargetGraph(), params.getBuckConfig().getKeySeed()));
// Look up all of the test rules in the action graph.
Iterable<TestRule> testRules = Iterables.filter(actionGraphAndResolver.getActionGraph().getNodes(), TestRule.class);
// the build.
if (!isBuildFiltered(params.getBuckConfig())) {
testRules = filterTestRules(params.getBuckConfig(), targetGraphAndBuildTargets.getBuildTargets(), testRules);
}
CachingBuildEngineBuckConfig cachingBuildEngineBuckConfig = params.getBuckConfig().getView(CachingBuildEngineBuckConfig.class);
try (CommandThreadManager artifactFetchService = getArtifactFetchService(params.getBuckConfig(), pool.getExecutor());
RuleKeyCacheScope<RuleKey> ruleKeyCacheScope = getDefaultRuleKeyCacheScope(params, new RuleKeyCacheRecycler.SettingsAffectingCache(params.getBuckConfig().getKeySeed(), actionGraphAndResolver.getActionGraph()))) {
LocalCachingBuildEngineDelegate localCachingBuildEngineDelegate = new LocalCachingBuildEngineDelegate(params.getFileHashCache());
CachingBuildEngine cachingBuildEngine = new CachingBuildEngine(new LocalCachingBuildEngineDelegate(params.getFileHashCache()), pool.getExecutor(), artifactFetchService == null ? pool.getExecutor() : artifactFetchService.getExecutor(), new DefaultStepRunner(), getBuildEngineMode().orElse(cachingBuildEngineBuckConfig.getBuildEngineMode()), cachingBuildEngineBuckConfig.getBuildDepFiles(), cachingBuildEngineBuckConfig.getBuildMaxDepFileCacheEntries(), cachingBuildEngineBuckConfig.getBuildArtifactCacheSizeLimit(), params.getObjectMapper(), actionGraphAndResolver.getResolver(), cachingBuildEngineBuckConfig.getResourceAwareSchedulingInfo(), new RuleKeyFactoryManager(params.getBuckConfig().getKeySeed(), fs -> localCachingBuildEngineDelegate.getFileHashCache(), actionGraphAndResolver.getResolver(), cachingBuildEngineBuckConfig.getBuildInputRuleKeyFileSizeLimit(), ruleKeyCacheScope.getCache()));
try (Build build = createBuild(params.getBuckConfig(), actionGraphAndResolver.getActionGraph(), actionGraphAndResolver.getResolver(), params.getCell(), params.getAndroidPlatformTargetSupplier(), cachingBuildEngine, params.getArtifactCacheFactory().newInstance(), params.getConsole(), params.getBuckEventBus(), getTargetDeviceOptional(), params.getPersistentWorkerPools(), params.getPlatform(), params.getEnvironment(), params.getObjectMapper(), params.getClock(), Optional.of(getAdbOptions(params.getBuckConfig())), Optional.of(getTargetDeviceOptions()), params.getExecutors())) {
// Build all of the test rules.
int exitCode = build.executeAndPrintFailuresToEventBus(RichStream.from(testRules).map(TestRule::getBuildTarget).collect(MoreCollectors.toImmutableList()), isKeepGoing(), params.getBuckEventBus(), params.getConsole(), getPathToBuildReport(params.getBuckConfig()));
params.getBuckEventBus().post(BuildEvent.finished(started, exitCode));
if (exitCode != 0) {
return exitCode;
}
// the filtering here, after we've done the build but before we run the tests.
if (isBuildFiltered(params.getBuckConfig())) {
testRules = filterTestRules(params.getBuckConfig(), targetGraphAndBuildTargets.getBuildTargets(), testRules);
}
// Once all of the rules are built, then run the tests.
Optional<ImmutableList<String>> externalTestRunner = params.getBuckConfig().getExternalTestRunner();
if (externalTestRunner.isPresent()) {
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(actionGraphAndResolver.getResolver()));
return runTestsExternal(params, build, externalTestRunner.get(), testRules, pathResolver);
}
return runTestsInternal(params, cachingBuildEngine, build, testRules);
}
}
}
}
use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.
the class UninstallCommand method runWithoutHelp.
@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
// Parse all of the build targets specified by the user.
BuildRuleResolver resolver;
ImmutableSet<BuildTarget> buildTargets;
try (CommandThreadManager pool = new CommandThreadManager("Uninstall", getConcurrencyLimit(params.getBuckConfig()))) {
TargetGraphAndBuildTargets result = params.getParser().buildTargetGraphForTargetNodeSpecs(params.getBuckEventBus(), params.getCell(), getEnableParserProfiling(), pool.getExecutor(), parseArgumentsAsTargetNodeSpecs(params.getBuckConfig(), getArguments()), /* ignoreBuckAutodepsFiles */
false);
buildTargets = result.getBuildTargets();
resolver = Preconditions.checkNotNull(params.getActionGraphCache().getActionGraph(params.getBuckEventBus(), params.getBuckConfig().isActionGraphCheckingEnabled(), params.getBuckConfig().isSkipActionGraphCache(), result.getTargetGraph(), params.getBuckConfig().getKeySeed())).getResolver();
} catch (BuildTargetException | BuildFileParseException e) {
params.getBuckEventBus().post(ConsoleEvent.severe(MoreExceptions.getHumanReadableOrLocalizedMessage(e)));
return 1;
}
// Make sure that only one build target is specified.
if (buildTargets.size() != 1) {
params.getBuckEventBus().post(ConsoleEvent.severe("Must specify exactly one android_binary() rule."));
return 1;
}
BuildTarget buildTarget = Iterables.get(buildTargets, 0);
// Find the android_binary() rule from the parse.
BuildRule buildRule;
try {
buildRule = resolver.requireRule(buildTarget);
} catch (NoSuchBuildTargetException e) {
throw new HumanReadableException(e.getHumanReadableErrorMessage());
}
if (!(buildRule instanceof HasInstallableApk)) {
params.getBuckEventBus().post(ConsoleEvent.severe(String.format("Specified rule %s must be of type android_binary() or apk_genrule() but was %s().\n", buildRule.getFullyQualifiedName(), buildRule.getType())));
return 1;
}
HasInstallableApk hasInstallableApk = (HasInstallableApk) buildRule;
// We need this in case adb isn't already running.
try (ExecutionContext context = createExecutionContext(params)) {
final AdbHelper adbHelper = new AdbHelper(adbOptions(params.getBuckConfig()), targetDeviceOptions(), context, params.getConsole(), params.getBuckEventBus(), params.getBuckConfig().getRestartAdbOnFailure());
// Find application package name from manifest and uninstall from matching devices.
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
String appId = AdbHelper.tryToExtractPackageNameFromManifest(pathResolver, hasInstallableApk.getApkInfo());
return adbHelper.uninstallApp(appId, uninstallOptions().shouldKeepUserData()) ? 0 : 1;
}
}
use of com.facebook.buck.json.BuildFileParseException in project buck by facebook.
the class JavaSymbolFinder method getTargetsForSourceFiles.
/**
* For all the possible BUCK files above each of the given source files, parse them to JSON to
* find the targets that actually include these source files, and return a map of them. We do this
* over a collection of source files, rather than a single file at a time, because instantiating
* the BUCK file parser is expensive. (It spawns a Python subprocess.)
*/
private ImmutableMultimap<Path, BuildTarget> getTargetsForSourceFiles(Collection<Path> sourceFilePaths) throws InterruptedException, IOException {
Map<Path, List<Map<String, Object>>> parsedBuildFiles = Maps.newHashMap();
ImmutableSetMultimap.Builder<Path, BuildTarget> sourceFileTargetsMultimap = ImmutableSetMultimap.builder();
try (ProjectBuildFileParser parser = projectBuildFileParserFactory.createParser(marshaller, console, environment, buckEventBus, /* ignoreBuckAutodepsFiles */
false)) {
for (Path sourceFile : sourceFilePaths) {
for (Path buckFile : possibleBuckFilesForSourceFile(sourceFile)) {
List<Map<String, Object>> rules;
// Avoid parsing the same BUCK file twice.
if (parsedBuildFiles.containsKey(buckFile)) {
rules = parsedBuildFiles.get(buckFile);
} else {
rules = parser.getAll(buckFile);
parsedBuildFiles.put(buckFile, rules);
}
for (Map<String, Object> ruleMap : rules) {
String type = (String) ruleMap.get(BuckPyFunction.TYPE_PROPERTY_NAME);
if (javaRuleTypes.contains(type)) {
@SuppressWarnings("unchecked") List<String> srcs = (List<String>) Preconditions.checkNotNull(ruleMap.get("srcs"));
if (isSourceFilePathInSrcsList(sourceFile, srcs, buckFile.getParent())) {
Path buckFileDir = buckFile.getParent();
String baseName = "//" + (buckFileDir != null ? MorePaths.pathWithUnixSeparators(buckFileDir) : "");
String shortName = (String) Preconditions.checkNotNull(ruleMap.get("name"));
sourceFileTargetsMultimap.put(sourceFile, BuildTarget.builder(projectFilesystem.getRootPath(), baseName, shortName).build());
}
}
}
}
}
} catch (BuildFileParseException e) {
buckEventBus.post(ThrowableConsoleEvent.create(e, "Error while searching for targets."));
}
return sourceFileTargetsMultimap.build();
}
Aggregations