Skip to content

Commit

Permalink
Add the target path of an UnresolvedSymlink action to the aquery results
Browse files Browse the repository at this point in the history
Android converts bazel actions to a ninja build graph by querying the
actions with aquery, but was unable to convert UnresolvedSymlink actions
because the target path wasn't included in the aquery output.

RELNOTES: Added the target path of an UnresolvedSymlink action to the aquery results
PiperOrigin-RevId: 545726666
Change-Id: Ie106e5f5800abd035469f3572a8504e86dc7d3b4
  • Loading branch information
Googler authored and copybara-github committed Jul 5, 2023
1 parent 691b48b commit df82dcd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ protected String getRawProgressMessage() {
return progressMessage;
}

public PathFragment getTarget() {
return target;
}

private static DetailedExitCode createDetailedExitCode(String message, Code detailedCode) {
return DetailedExitCode.of(
FailureDetail.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.devtools.build.lib.analysis.actions.ParameterFileWriteAction;
import com.google.devtools.build.lib.analysis.actions.Substitution;
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction;
import com.google.devtools.build.lib.analysis.starlark.UnresolvedSymlinkAction;
import com.google.devtools.build.lib.buildeventstream.BuildEvent;
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
Expand Down Expand Up @@ -345,6 +346,13 @@ private void writeAction(ActionAnalysisMetadata action, PrintStream printStream)
.append("]\n");
}

if (action instanceof UnresolvedSymlinkAction) {
stringBuilder
.append(" UnresolvedSymlinkTarget: ")
.append(((UnresolvedSymlinkAction) action).getTarget())
.append("\n");
}

stringBuilder.append('\n');

printStream.write(stringBuilder.toString().getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction;
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionException;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.starlark.UnresolvedSymlinkAction;
import com.google.devtools.build.lib.buildeventstream.BuildEvent;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
Expand Down Expand Up @@ -201,6 +202,11 @@ private void dumpSingleAction(ConfiguredTarget configuredTarget, ActionAnalysisM
actionBuilder.setFileContents(contents);
}

if (action instanceof UnresolvedSymlinkAction) {
actionBuilder.setUnresolvedSymlinkTarget(
((UnresolvedSymlinkAction) action).getTarget().toString());
}

// Include the content of param files in output.
if (includeParamFiles) {
// Assumption: if an Action takes a params file as an input, it will be used
Expand Down
4 changes: 4 additions & 0 deletions src/main/protobuf/analysis_v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ message Action {
// The contents of the file for the actions.write() action
// (guarded by the --include_file_write_contents flag).
string file_contents = 17;

// The target of the symlink created by UnresolvedSymlink actions.
// For regular Symlink actions, the target is represented as an input.
string unresolved_symlink_target = 18;
}

// Represents a single target (without configuration information) that is
Expand Down
40 changes: 40 additions & 0 deletions src/test/shell/integration/aquery_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,46 @@ EOF
fi
}

function test_unresolved_symlinks() {
local pkg="${FUNCNAME[0]}"
mkdir -p "$pkg" || fail "mkdir -p $pkg"
touch "$pkg/foo.sh"
cat > "$pkg/unresolved_symlink.bzl" <<'EOF'
def _impl(ctx):
out = ctx.actions.declare_symlink(ctx.label.name)
ctx.actions.symlink(
output = out,
target_path = ctx.attr.path
)
return [
DefaultInfo(files = depset([out]))
]
unresolved_symlink = rule(
implementation = _impl,
attrs = {
"path": attr.string(),
},
)
EOF
cat > "$pkg/BUILD" <<'EOF'
load(":unresolved_symlink.bzl", "unresolved_symlink")
unresolved_symlink(
name = "foo",
path = "bar/baz.txt",
)
EOF
bazel aquery --output=textproto \
"//$pkg:foo" >output 2> "$TEST_log" || fail "Expected success"
cat output >> "$TEST_log"
assert_contains "^unresolved_symlink_target:.*bar/baz.txt" output

bazel aquery --output=text "//$pkg:foo" | \
sed -nr '/Mnemonic: UnresolvedSymlink/,/^ *$/p' >output \
2> "$TEST_log" || fail "Expected success"
cat output >> "$TEST_log"
assert_contains "^ *UnresolvedSymlinkTarget:.*bar/baz.txt" output
}

function test_does_not_fail_horribly_with_file() {
rm -rf peach
mkdir -p peach
Expand Down

0 comments on commit df82dcd

Please sign in to comment.