From bae176dd520fe033ea54930884e205253b5873a2 Mon Sep 17 00:00:00 2001 From: tanzhengwei Date: Thu, 10 Dec 2020 02:47:25 -0800 Subject: [PATCH] Wire up proto and xml output formatters to output locations like the location output formatters. In order to align proto and xml output formatters with the fix for https://github.com/bazelbuild/bazel/issues/8900 (where the location of source files should be its actual location but not its location in the BUILD file). PiperOrigin-RevId: 346743348 --- .../query/output/ProtoOutputFormatter.java | 5 +- .../query/output/XmlOutputFormatter.java | 4 +- .../shell/integration/bazel_query_test.sh | 49 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java index 5e389fdf3bd742..bfd643e1d02b2e 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java @@ -104,6 +104,7 @@ public class ProtoOutputFormatter extends AbstractUnorderedFormatter { private AspectResolver aspectResolver; private DependencyFilter dependencyFilter; private boolean relativeLocations; + private boolean displaySourceFileLocation; private boolean includeDefaultValues = true; private Predicate ruleAttributePredicate = Predicates.alwaysTrue(); private boolean flattenSelects = true; @@ -128,6 +129,7 @@ public void setOptions( this.aspectResolver = aspectResolver; this.dependencyFilter = FormatUtils.getDependencyFilter(options); this.relativeLocations = options.relativeLocations; + this.displaySourceFileLocation = options.displaySourceFileLocation; this.includeDefaultValues = options.protoIncludeDefaultValues; this.ruleAttributePredicate = newAttributePredicate(options.protoOutputRuleAttributes); this.flattenSelects = options.protoFlattenSelects; @@ -302,7 +304,8 @@ public Build.Target toTargetProtoBuffer(Target target, Object extraDataForAttrHa .setName(label.toString()); if (includeLocations) { - input.setLocation(FormatUtils.getLocation(target, relativeLocations)); + input.setLocation( + FormatUtils.getLocation(target, relativeLocations, displaySourceFileLocation)); } if (inputFile.getName().equals("BUILD")) { diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java index 0e1bb7b6697bf9..52243c4bd6829b 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java @@ -64,6 +64,7 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter { private AspectResolver aspectResolver; private DependencyFilter dependencyFilter; private boolean relativeLocations; + private boolean displaySourceFileLocation; private QueryOptions queryOptions; @Override @@ -85,6 +86,7 @@ public void setOptions( this.aspectResolver = aspectResolver; this.dependencyFilter = FormatUtils.getDependencyFilter(options); this.relativeLocations = options.relativeLocations; + this.displaySourceFileLocation = options.displaySourceFileLocation; Preconditions.checkArgument(options instanceof QueryOptions); this.queryOptions = (QueryOptions) options; @@ -240,7 +242,7 @@ private Element createTargetElement(Document doc, Target target) } elem.setAttribute("name", target.getLabel().toString()); - String location = FormatUtils.getLocation(target, relativeLocations); + String location = FormatUtils.getLocation(target, relativeLocations, displaySourceFileLocation); if (!queryOptions.xmlLineNumbers) { int firstColon = location.indexOf(':'); if (firstColon != -1) { diff --git a/src/test/shell/integration/bazel_query_test.sh b/src/test/shell/integration/bazel_query_test.sh index dc2cf063dd61e9..0ed2599e2614b1 100755 --- a/src/test/shell/integration/bazel_query_test.sh +++ b/src/test/shell/integration/bazel_query_test.sh @@ -551,6 +551,55 @@ EOF expect_not_log "^${TEST_TMPDIR}/.*/foo/BUILD:[0-9]*:[0-9]*" } +function test_proto_output_source_files() { + rm -rf foo + mkdir -p foo + cat > foo/BUILD <& $TEST_log || fail "Expected success" + + expect_log "${TEST_TMPDIR}/.*/foo/main.py:1:1" $TEST_log + expect_not_log "${TEST_TMPDIR}/.*/foo/BUILD:[0-9]*:[0-9]*" $TEST_log + + bazel query --output=proto \ + --noincompatible_display_source_file_location \ + '//foo:main.py' >& $TEST_log || fail "Expected success" + expect_log "${TEST_TMPDIR}/.*/foo/BUILD:[0-9]*:[0-9]*" $TEST_log + expect_not_log "${TEST_TMPDIR}/.*/foo/main.py:1:1" $TEST_log +} + +function test_xml_output_source_files() { + rm -rf foo + mkdir -p foo + cat > foo/BUILD <& $TEST_log || fail "Expected success" + expect_log "location=\"${TEST_TMPDIR}/.*/foo/main.py:1:1" + expect_not_log "location=\"${TEST_TMPDIR}/.*/foo/BUILD:[0-9]*:[0-9]*" + + bazel query --output=xml \ + --noincompatible_display_source_file_location \ + '//foo:main.py' >& $TEST_log || fail "Expected success" + expect_log "location=\"${TEST_TMPDIR}/.*/foo/BUILD:[0-9]*:[0-9]*" + expect_not_log "location=\"${TEST_TMPDIR}/.*/foo/main.py:1:1" +} + function test_subdirectory_named_external() { mkdir -p foo/external foo/bar cat > foo/external/BUILD <