Search in sources :

Example 6 with OptionsParsingException

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

the class InvocationPolicyEnforcerTest method testSetValueWithNoValueThrows.

@Test
public void testSetValueWithNoValueThrows() throws Exception {
    InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
    invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getSetValueBuilder();
    InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
    parser.parse("--test_string=user value");
    TestOptions testOptions = getTestOptions();
    assertEquals("user value", testOptions.testString);
    try {
        enforcer.enforce(parser, "build");
        fail();
    } catch (OptionsParsingException e) {
    // expected.
    }
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) Test(org.junit.Test)

Example 7 with OptionsParsingException

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

the class InvocationPolicyEnforcerTest method testDisallowValuesDisallowsMultipleValues.

@Test
public void testDisallowValuesDisallowsMultipleValues() throws Exception {
    InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
    invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getDisallowValuesBuilder().addDisallowedValues("foo").addDisallowedValues("bar");
    InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
    parser.parse("--test_multiple_string=baz", "--test_multiple_string=bar");
    // Option should be "baz" and "bar" as specified by the user.
    TestOptions testOptions = getTestOptions();
    assertThat(testOptions.testMultipleString).containsExactly("baz", "bar").inOrder();
    try {
        enforcer.enforce(parser, "build");
        fail();
    } catch (OptionsParsingException e) {
    // expected, since bar is disallowed.
    }
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) Test(org.junit.Test)

Example 8 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 9 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)

Example 10 with OptionsParsingException

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

the class ConfigSetting method matchesConfig.

/**
   * Given a list of [flagName, flagValue] pairs, returns true if flagName == flagValue for
   * every item in the list under this configuration, false otherwise.
   */
private boolean matchesConfig(Map<String, String> expectedSettings, BuildConfiguration config) throws OptionsParsingException {
    // Rather than returning fast when we find a mismatch, continue looking at the other flags
    // to check that they're indeed valid flag specifications.
    boolean foundMismatch = false;
    // Since OptionsParser instantiation involves reflection, let's try to minimize that happening.
    Map<Class<? extends OptionsBase>, OptionsParser> parserCache = new HashMap<>();
    for (Map.Entry<String, String> setting : expectedSettings.entrySet()) {
        String optionName = setting.getKey();
        String expectedRawValue = setting.getValue();
        Class<? extends OptionsBase> optionClass = config.getOptionClass(optionName);
        if (optionClass == null) {
            throw new OptionsParsingException("unknown option: '" + optionName + "'");
        }
        OptionsParser parser = parserCache.get(optionClass);
        if (parser == null) {
            parser = OptionsParser.newOptionsParser(optionClass);
            parserCache.put(optionClass, parser);
        }
        parser.parse("--" + optionName + "=" + expectedRawValue);
        Object expectedParsedValue = parser.getOptions(optionClass).asMap().get(optionName);
        if (!optionMatches(config, optionName, expectedParsedValue)) {
            foundMismatch = true;
        }
    }
    return !foundMismatch;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) HashMap(java.util.HashMap) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser) HashMap(java.util.HashMap) Map(java.util.Map)

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