Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.2.0] Allows --query_file to be used for cquery and aquery too. #17823

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,14 @@ public AspectResolutionModeConverter() {
+ "will be merged together and their labels concatenated. This option is only "
+ "applicable to --output=graph.")
public boolean graphFactored;

@Option(
name = "query_file",
defaultValue = "",
documentationCategory = OptionDocumentationCategory.QUERY,
effectTags = {OptionEffectTag.CHANGES_INPUTS},
help =
"If set, query will read the query from the file named here, rather than on the command "
+ "line. It is an error to specify a file here as well as a command-line query.")
public String queryFile;
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,6 @@ public enum OrderOutput {
)
public boolean strictTestSuite;

@Option(
name = "query_file",
defaultValue = "",
documentationCategory = OptionDocumentationCategory.QUERY,
effectTags = {OptionEffectTag.CHANGES_INPUTS},
help =
"If set, query will read the query from the file named here, rather than on the command "
+ "line. It is an error to specify a file here as well as a command-line query."
)
public String queryFile;

@Option(
name = "experimental_graphless_query",
defaultValue = "auto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.
package com.google.devtools.build.lib.runtime.commands;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -101,15 +100,13 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
InterruptedFailureDetails.detailedExitCode(errorMessage));
}

// When querying for the state of Skyframe, it's possible to omit the query expression.
if (options.getResidue().isEmpty() && !queryCurrentSkyframeState) {
String message =
"Missing query expression. Use the 'help aquery' command for syntax and help.";
env.getReporter().handle(Event.error(message));
return createFailureResult(message, Code.COMMAND_LINE_EXPRESSION_MISSING);
String query = null;
try {
query = QueryOptionHelper.readQuery(aqueryOptions, options, env, queryCurrentSkyframeState);
} catch (QueryException e) {
return BlazeCommandResult.failureDetail(e.getFailureDetail());
}

String query = Joiner.on(' ').join(options.getResidue());
ImmutableMap<String, QueryFunction> functions = getFunctionsMap(env);

// Query expression might be null in the case of --skyframe_state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/profiler/memory:allocationtracker",
"//src/main/java/com/google/devtools/build/lib/query2",
"//src/main/java/com/google/devtools/build/lib/query2/common:abstract-blaze-query-env",
"//src/main/java/com/google/devtools/build/lib/query2/common:options",
"//src/main/java/com/google/devtools/build/lib/query2/common:universe-scope",
"//src/main/java/com/google/devtools/build/lib/query2/engine",
"//src/main/java/com/google/devtools/build/lib/query2/query/output",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.
package com.google.devtools.build.lib.runtime.commands;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.config.CoreOptions.IncludeConfigFragmentsEnum;
import com.google.devtools.build.lib.buildtool.BuildRequest;
Expand All @@ -27,6 +26,7 @@
import com.google.devtools.build.lib.query2.cquery.ConfiguredTargetQueryEnvironment;
import com.google.devtools.build.lib.query2.cquery.CqueryOptions;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction;
import com.google.devtools.build.lib.query2.engine.QueryException;
import com.google.devtools.build.lib.query2.engine.QueryExpression;
import com.google.devtools.build.lib.query2.engine.QueryParser;
import com.google.devtools.build.lib.query2.engine.QuerySyntaxException;
Expand Down Expand Up @@ -133,13 +133,15 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
InterruptedFailureDetails.detailedExitCode(errorMessage));
}

if (options.getResidue().isEmpty()) {
String message =
"Missing query expression. Use the 'help cquery' command for syntax and help.";
env.getReporter().handle(Event.error(message));
return createFailureResult(message, Code.COMMAND_LINE_EXPRESSION_MISSING);
String query = null;
try {
query =
QueryOptionHelper.readQuery(
options.getOptions(CqueryOptions.class), options, env, /* allowEmptyQuery= */ false);
} catch (QueryException e) {
return BlazeCommandResult.failureDetail(e.getFailureDetail());
}
String query = Joiner.on(' ').join(options.getResidue());

HashMap<String, QueryFunction> functions = new HashMap<>();
for (QueryFunction queryFunction : ConfiguredTargetQueryEnvironment.FUNCTIONS) {
functions.put(queryFunction.getName(), queryFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import static com.google.devtools.build.lib.packages.Rule.ALL_LABELS;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.NoBuildEvent;
import com.google.devtools.build.lib.analysis.NoBuildRequestFinishedEvent;
Expand All @@ -32,6 +31,7 @@
import com.google.devtools.build.lib.query2.engine.QueryEnvironment;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting;
import com.google.devtools.build.lib.query2.engine.QueryEvalResult;
import com.google.devtools.build.lib.query2.engine.QueryException;
import com.google.devtools.build.lib.query2.query.output.OutputFormatter;
import com.google.devtools.build.lib.query2.query.output.OutputFormatters;
import com.google.devtools.build.lib.query2.query.output.QueryOptions;
Expand All @@ -57,13 +57,9 @@
import com.google.devtools.build.lib.util.Either;
import com.google.devtools.build.lib.util.ExitCode;
import com.google.devtools.build.lib.util.InterruptedFailureDetails;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.skyframe.WalkableGraph;
import com.google.devtools.common.options.OptionsParsingResult;
import com.google.devtools.common.options.TriState;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.function.Function;

Expand Down Expand Up @@ -125,33 +121,11 @@ private BlazeCommandResult execInternal(CommandEnvironment env, OptionsParsingRe
return BlazeCommandResult.detailedExitCode(e.getDetailedExitCode());
}

String query;
if (!options.getResidue().isEmpty()) {
if (!queryOptions.queryFile.isEmpty()) {
return reportAndCreateFailureResult(
env,
"Command-line query and --query_file cannot both be specified",
Query.Code.QUERY_FILE_WITH_COMMAND_LINE_EXPRESSION);
}
query = Joiner.on(' ').join(options.getResidue());
} else if (!queryOptions.queryFile.isEmpty()) {
// Works for absolute or relative query file.
Path residuePath = env.getWorkingDirectory().getRelative(queryOptions.queryFile);
try {
query = new String(FileSystemUtils.readContent(residuePath), StandardCharsets.UTF_8);
} catch (IOException e) {
return reportAndCreateFailureResult(
env,
"I/O error reading from " + residuePath.getPathString(),
Query.Code.QUERY_FILE_READ_FAILURE);
}
} else {
return reportAndCreateFailureResult(
env,
String.format(
"missing query expression. Type '%s help query' for syntax and help",
runtime.getProductName()),
Query.Code.COMMAND_LINE_EXPRESSION_MISSING);
String query = null;
try {
query = QueryOptionHelper.readQuery(queryOptions, options, env, /* allowEmptyQuery =*/ false);
} catch (QueryException e) {
return BlazeCommandResult.failureDetail(e.getFailureDetail());
}

Iterable<OutputFormatter> formatters = runtime.getQueryOutputFormatters();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2022 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package com.google.devtools.build.lib.runtime.commands;
package com.google.devtools.build.lib.runtime.commands;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Joiner;
import com.google.devtools.build.lib.query2.common.CommonQueryOptions;
import com.google.devtools.build.lib.query2.engine.QueryException;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.server.FailureDetails.Query;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.common.options.OptionsParsingResult;
import java.io.IOException;

/**
* Reads the query for query, cquery and aquery using the --query_file option or from the residue of
* the command line.
*/
public final class QueryOptionHelper {

public static String readQuery(
CommonQueryOptions queryOptions,
OptionsParsingResult options,
CommandEnvironment env,
boolean allowEmptyQuery)
throws QueryException {
String query = "";
if (!options.getResidue().isEmpty()) {
if (!queryOptions.queryFile.isEmpty()) {
throw new QueryException(
"Command-line query and --query_file cannot both be specified",
Query.Code.QUERY_FILE_WITH_COMMAND_LINE_EXPRESSION);
}
query = Joiner.on(' ').join(options.getResidue());
} else if (!queryOptions.queryFile.isEmpty()) {
// Works for absolute or relative query file.
Path residuePath = env.getWorkingDirectory().getRelative(queryOptions.queryFile);
try {
query = new String(FileSystemUtils.readContent(residuePath), UTF_8);
} catch (IOException unused) {
throw new QueryException(
"I/O error reading from " + residuePath.getPathString(),
Query.Code.QUERY_FILE_READ_FAILURE);
}
} else {
// When querying for the state of Skyframe, it's possible to omit the query expression.
if (!allowEmptyQuery) {
throw new QueryException(
String.format(
"missing query expression. Type '%s help query' for syntax and help",
env.getRuntime().getProductName()),
Query.Code.COMMAND_LINE_EXPRESSION_MISSING);
}
}
return query;
}

private QueryOptionHelper() {}
}
19 changes: 19 additions & 0 deletions src/test/shell/integration/aquery_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,25 @@ EOF
fi
}

function test_does_not_fail_horribly_with_file() {
rm -rf peach
mkdir -p peach
cat > "peach/BUILD" <<'EOF'
genrule(
name = "bar",
srcs = ["dummy.txt"],
outs = ["bar_out.txt"],
cmd = "echo unused > bar_out.txt",
)
EOF

echo "//peach:bar" > query_file
bazel aquery --query_file=query_file > $TEST_log

expect_log "Target: //peach:bar" "look in $TEST_log"
expect_log "ActionKey:"
}

function test_cpp_compile_action_env() {
local pkg="${FUNCNAME[0]}"
mkdir -p "$pkg"
Expand Down
15 changes: 15 additions & 0 deletions src/test/shell/integration/configured_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,21 @@ EOF
expect_not_log "QueryException"
}

function test_does_not_fail_horribly_with_file() {
rm -rf peach
mkdir -p peach
cat > peach/BUILD <<EOF
sh_library(name='brighton', deps=[':harken'])
sh_library(name='harken')
EOF

echo "deps(//peach:brighton)" > query_file
bazel cquery --query_file=query_file > $TEST_log

expect_log "//peach:brighton"
expect_log "//peach:harken"
}

function test_files_include_source_files() {
local -r pkg=$FUNCNAME
mkdir -p $pkg
Expand Down