Skip to content

Commit

Permalink
Wire up proto and xml output formatters to output locations like the …
Browse files Browse the repository at this point in the history
…location output formatters.

In order to align proto and xml output formatters with the fix for bazelbuild#8900 (where the location of source files should be its actual location but not its location in the BUILD file).

PiperOrigin-RevId: 346743348
  • Loading branch information
zhengwei143 authored and copybara-github committed Dec 10, 2020
1 parent 80ead8f commit bae176d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ruleAttributePredicate = Predicates.alwaysTrue();
private boolean flattenSelects = true;
Expand All @@ -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;
Expand Down Expand Up @@ -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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter {
private AspectResolver aspectResolver;
private DependencyFilter dependencyFilter;
private boolean relativeLocations;
private boolean displaySourceFileLocation;
private QueryOptions queryOptions;

@Override
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
49 changes: 49 additions & 0 deletions src/test/shell/integration/bazel_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
py_binary(
name = "main",
srcs = ["main.py"],
)
EOF
touch foo/main.py || fail "Could not touch foo/main.py"

bazel query --output=proto \
--incompatible_display_source_file_location \
'//foo:main.py' >& $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 <<EOF
py_binary(
name = "main",
srcs = ["main.py"],
)
EOF
touch foo/main.py || fail "Could not touch foo/main.py"

bazel query --output=xml \
--incompatible_display_source_file_location \
'//foo:main.py' >& $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 <<EOF
Expand Down

0 comments on commit bae176d

Please sign in to comment.