Search in sources :

Example 1 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.

the class ProjectFileSupport method handleProjectFiles.

/**
   * Reads any project files specified on the command line and updates the options parser
   * accordingly. If project files cannot be read or if they contain unparsable options, or if they
   * are not enabled, then it throws an exception instead.
   */
public static void handleProjectFiles(CommandEnvironment env, OptionsParser optionsParser, String command) throws OptionsParsingException {
    BlazeRuntime runtime = env.getRuntime();
    List<String> targets = optionsParser.getResidue();
    ProjectFile.Provider projectFileProvider = runtime.getProjectFileProvider();
    if (projectFileProvider != null && !targets.isEmpty() && targets.get(0).startsWith(PROJECT_FILE_PREFIX)) {
        if (targets.size() > 1) {
            throw new OptionsParsingException("Cannot handle more than one +<file> argument yet");
        }
        if (!optionsParser.getOptions(CommonCommandOptions.class).allowProjectFiles) {
            throw new OptionsParsingException("project file support is not enabled");
        }
        // TODO(bazel-team): This is currently treated as a path relative to the workspace - if the
        // cwd is a subdirectory of the workspace, that will be surprising, and we should interpret it
        // relative to the cwd instead.
        PathFragment projectFilePath = new PathFragment(targets.get(0).substring(1));
        List<Path> packagePath = PathPackageLocator.create(env.getOutputBase(), optionsParser.getOptions(PackageCacheOptions.class).packagePath, env.getReporter(), env.getWorkspace(), env.getWorkingDirectory()).getPathEntries();
        ProjectFile projectFile = projectFileProvider.getProjectFile(env.getWorkingDirectory(), packagePath, projectFilePath);
        env.getReporter().handle(Event.info("Using " + projectFile.getName()));
        optionsParser.parse(OptionPriority.RC_FILE, projectFile.getName(), projectFile.getCommandLineFor(command));
        env.getEventBus().post(new GotProjectFileEvent(projectFile.getName()));
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) ProjectFile(com.google.devtools.build.lib.runtime.ProjectFile) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 2 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.

the class TestCommand method editOptions.

@Override
public void editOptions(CommandEnvironment env, OptionsParser optionsParser) throws AbruptExitException {
    TestOutputFormat testOutput = optionsParser.getOptions(ExecutionOptions.class).testOutput;
    try {
        if (testOutput == TestStrategy.TestOutputFormat.STREAMED) {
            env.getReporter().handle(Event.warn("Streamed test output requested. All tests will be run locally, without sharding, " + "one at a time"));
            optionsParser.parse(OptionPriority.SOFTWARE_REQUIREMENT, "streamed output requires locally run tests, without sharding", ImmutableList.of("--test_sharding_strategy=disabled", "--test_strategy=exclusive"));
        }
    } catch (OptionsParsingException e) {
        throw new IllegalStateException("Known options failed to parse", e);
    }
}
Also used : ExecutionOptions(com.google.devtools.build.lib.exec.ExecutionOptions) TestOutputFormat(com.google.devtools.build.lib.exec.TestStrategy.TestOutputFormat) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException)

Example 3 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.

the class ArtifactLocationConverter method convert.

@Override
public ArtifactLocation convert(String input) throws OptionsParsingException {
    Iterator<String> values = SPLITTER.split(input).iterator();
    try {
        Path rootExecutionPathFragment = pathConverter.convert(values.next());
        Path relPath = pathConverter.convert(values.next());
        // Last value is optional to maintain compatibility with the native aspect
        boolean isExternal = false;
        if (values.hasNext()) {
            isExternal = values.next().equals("1");
        }
        if (values.hasNext()) {
            throw new OptionsParsingException(INVALID_FORMAT);
        }
        boolean isSource = rootExecutionPathFragment.toString().isEmpty();
        return ArtifactLocation.newBuilder().setRootExecutionPathFragment(rootExecutionPathFragment.toString()).setRelativePath(relPath.toString()).setIsSource(isSource).setIsExternal(isExternal).build();
    } catch (OptionsParsingException | NoSuchElementException e) {
        throw new OptionsParsingException(INVALID_FORMAT);
    }
}
Also used : Path(java.nio.file.Path) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.

the class CommandEnvironment method beforeCommand.

/**
   * Hook method called by the BlazeCommandDispatcher prior to the dispatch of
   * each command.
   *
   * @param options The CommonCommandOptions used by every command.
   * @throws AbruptExitException if this command is unsuitable to be run as specified
   */
void beforeCommand(Command command, OptionsParser optionsParser, CommonCommandOptions options, long execStartTimeNanos, long waitTimeInMs) throws AbruptExitException {
    commandStartTime -= options.startupTime;
    if (runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).watchFS) {
        try {
            // TODO(ulfjack): Get rid of the startup option and drop this code.
            optionsParser.parse("--watchfs");
        } catch (OptionsParsingException e) {
            // This should never happen.
            throw new IllegalStateException(e);
        }
    }
    this.commandName = command.name();
    this.options = optionsParser;
    eventBus.post(new GotOptionsEvent(runtime.getStartupOptionsProvider(), optionsParser));
    throwPendingException();
    outputService = null;
    BlazeModule outputModule = null;
    if (command.builds()) {
        for (BlazeModule module : runtime.getBlazeModules()) {
            OutputService moduleService = module.getOutputService();
            if (moduleService != null) {
                if (outputService != null) {
                    throw new IllegalStateException(String.format("More than one module (%s and %s) returns an output service", module.getClass(), outputModule.getClass()));
                }
                outputService = moduleService;
                outputModule = module;
            }
        }
    }
    SkyframeExecutor skyframeExecutor = getSkyframeExecutor();
    skyframeExecutor.setOutputService(outputService);
    // Ensure that the working directory will be under the workspace directory.
    Path workspace = getWorkspace();
    Path workingDirectory;
    if (inWorkspace()) {
        workingDirectory = workspace.getRelative(options.clientCwd);
    } else {
        workspace = FileSystemUtils.getWorkingDirectory(getDirectories().getFileSystem());
        workingDirectory = workspace;
    }
    this.relativeWorkingDirectory = workingDirectory.relativeTo(workspace);
    this.workingDirectory = workingDirectory;
    updateClientEnv(options.clientEnv);
    // Fail fast in the case where a Blaze command forgets to install the package path correctly.
    skyframeExecutor.setActive(false);
    // Let skyframe figure out if it needs to store graph edges for this build.
    skyframeExecutor.decideKeepIncrementalState(runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).batch, optionsParser.getOptions(BuildView.Options.class));
    // Start the performance and memory profilers.
    runtime.beforeCommand(this, options, execStartTimeNanos);
    // actionClientEnv contains the environment where values from actionEnvironment are
    // overridden.
    actionClientEnv.clear();
    actionClientEnv.putAll(clientEnv);
    if (command.builds()) {
        Map<String, String> testEnv = new TreeMap<>();
        for (Map.Entry<String, String> entry : optionsParser.getOptions(BuildConfiguration.Options.class).testEnvironment) {
            testEnv.put(entry.getKey(), entry.getValue());
        }
        // for inheritence.
        for (Map.Entry<String, String> entry : optionsParser.getOptions(BuildConfiguration.Options.class).actionEnvironment) {
            if (entry.getValue() == null) {
                visibleClientEnv.add(entry.getKey());
            } else {
                visibleClientEnv.remove(entry.getKey());
                actionClientEnv.put(entry.getKey(), entry.getValue());
            }
        }
        try {
            for (Map.Entry<String, String> entry : testEnv.entrySet()) {
                if (entry.getValue() == null) {
                    String clientValue = clientEnv.get(entry.getKey());
                    if (clientValue != null) {
                        optionsParser.parse(OptionPriority.SOFTWARE_REQUIREMENT, "test environment variable from client environment", ImmutableList.of("--test_env=" + entry.getKey() + "=" + clientEnv.get(entry.getKey())));
                    }
                }
            }
        } catch (OptionsParsingException e) {
            throw new IllegalStateException(e);
        }
    }
    eventBus.post(new CommandStartEvent(command.name(), getCommandId(), getClientEnv(), workingDirectory, getDirectories(), waitTimeInMs + options.waitTime));
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) TreeMap(java.util.TreeMap) SkyframeExecutor(com.google.devtools.build.lib.skyframe.SkyframeExecutor) OutputService(com.google.devtools.build.lib.exec.OutputService) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 5 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.

the class CanonicalizeCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    Options canonicalizeOptions = options.getOptions(Options.class);
    String commandName = canonicalizeOptions.forCommand;
    BlazeCommand command = runtime.getCommandMap().get(commandName);
    if (command == null) {
        env.getReporter().handle(Event.error("Not a valid command: '" + commandName + "' (should be one of " + Joiner.on(", ").join(runtime.getCommandMap().keySet()) + ")"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    Collection<Class<? extends OptionsBase>> optionsClasses = BlazeCommandUtils.getOptions(command.getClass(), runtime.getBlazeModules(), runtime.getRuleClassProvider());
    try {
        OptionsParser parser = OptionsParser.newOptionsParser(optionsClasses);
        parser.setAllowResidue(false);
        parser.parse(options.getResidue());
        InvocationPolicyEnforcer invocationPolicyEnforcer = InvocationPolicyEnforcer.create(canonicalizeOptions.invocationPolicy);
        invocationPolicyEnforcer.enforce(parser, commandName);
        List<String> result = parser.canonicalize();
        for (String piece : result) {
            env.getReporter().getOutErr().printOutLn(piece);
        }
    } catch (OptionsParsingException e) {
        env.getReporter().handle(Event.error(e.getMessage()));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    return ExitCode.SUCCESS;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Aggregations

OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)26 InvocationPolicyEnforcer (com.google.devtools.build.lib.flags.InvocationPolicyEnforcer)15 InvocationPolicy (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy)11 Test (org.junit.Test)11 OptionsParser (com.google.devtools.common.options.OptionsParser)6 Path (com.google.devtools.build.lib.vfs.Path)3 PackageCacheOptions (com.google.devtools.build.lib.pkgcache.PackageCacheOptions)2 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)2 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)2 OptionsBase (com.google.devtools.common.options.OptionsBase)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Artifact (com.google.devtools.build.lib.actions.Artifact)1 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)1 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)1 FragmentOptions (com.google.devtools.build.lib.analysis.config.FragmentOptions)1 Label (com.google.devtools.build.lib.cmdline.Label)1