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

fix: expand_location binary target inputs #16381

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1133,7 +1133,7 @@ private static void checkDeprecated(String newApi, String oldApi, StarlarkSemant
}

/**
* Builds a map: Label -> List of files from the given labels
* Builds a map: Label -> List of runfiles from the given labels.
*
* @param knownLabels List of known labels
* @return Immutable map with immutable collections as values
Expand All @@ -1143,9 +1143,14 @@ public static ImmutableMap<Label, ImmutableCollection<Artifact>> makeLabelMap(
ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();

for (TransitiveInfoCollection current : knownLabels) {
Label label = AliasProvider.getDependencyLabel(current);
FilesToRunProvider filesToRun = current.getProvider(FilesToRunProvider.class);
f0rmiga marked this conversation as resolved.
Show resolved Hide resolved
f0rmiga marked this conversation as resolved.
Show resolved Hide resolved
Artifact executableArtifact = filesToRun.getExecutable();
builder.put(
AliasProvider.getDependencyLabel(current),
current.getProvider(FileProvider.class).getFilesToBuild().toList());
label,
executableArtifact != null
? ImmutableList.of(executableArtifact)
: filesToRun.getFilesToRun().toList());
}

return builder.buildOrThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,41 @@ public void testExpandLocationWithShortPaths() throws Exception {
assertThat(loc).isEqualTo("foo/libjl.jar");
}

/** Asserts that a custom rule has the same behaviour as native rules when
* expanding executable target locations.
*/
@Test
public void testExpandLocationExecutableTargets() throws Exception {
f0rmiga marked this conversation as resolved.
Show resolved Hide resolved
scratch.file(
"test/defs.bzl",
"def _impl(ctx):",
" env = {}",
" for k, v in ctx.attr.env.items():",
" env[k] = ctx.expand_location(v, targets = ctx.attr.data)",
" return [DefaultInfo()]",
"expand_location_env_rule = rule(",
" implementation = _impl,",
" attrs = {",
" 'data': attr.label_list(allow_files=True),",
" 'env': attr.string_dict(),",
" }",
")");

scratch.file("test/main.py", "");

scratch.file(
"test/BUILD",
"load('//test:defs.bzl', 'expand_location_env_rule')",
"py_binary(name = 'main', srcs = ['main.py'])",
f0rmiga marked this conversation as resolved.
Show resolved Hide resolved
"expand_location_env_rule(",
" name = 'expand_location_env',",
" data = [':main'],",
" env = {'MAIN_EXECPATH': '$(execpath :main)'},",
")");

assertThat(getConfiguredTarget("//test:expand_location_env")).isNotNull();
}

/** Regression test to check that expand_location allows ${var} and $$. */
@Test
public void testExpandLocationWithDollarSignsAndCurlys() throws Exception {
Expand Down