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

Adding Starlark dependencies to the package //external #14630

Closed
wants to merge 2 commits into from
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 @@ -1370,7 +1370,7 @@ FailureDetail getFailureDetail() {
return null;
}

Builder setStarlarkFileDependencies(ImmutableList<Label> starlarkFileDependencies) {
public Builder setStarlarkFileDependencies(ImmutableList<Label> starlarkFileDependencies) {
this.starlarkFileDependencies = starlarkFileDependencies;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ private static ImmutableList<Label> transitiveClosureOfLabels(
return ImmutableList.copyOf(set);
}

private static void transitiveClosureOfLabelsRec(
public static void transitiveClosureOfLabelsRec(
Set<Label> set, ImmutableMap<String, Module> loads) {
for (Module m : loads.values()) {
BazelModuleContext ctx = BazelModuleContext.of(m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.actions.FileValue;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.cmdline.Label;
Expand Down Expand Up @@ -58,6 +59,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import net.starlark.java.eval.Module;
import net.starlark.java.eval.Mutability;
import net.starlark.java.eval.Starlark;
Expand Down Expand Up @@ -325,14 +327,19 @@ public SkyValue compute(SkyKey skyKey, Environment env)
directories.getWorkspace(),
directories.getLocalJavabase(),
starlarkSemantics);
Set<Label> starlarkFileDependencies = Sets.newLinkedHashSet();
linzhp marked this conversation as resolved.
Show resolved Hide resolved
if (prevValue != null) {
starlarkFileDependencies = Sets.newLinkedHashSet(prevValue.getPackage()
.getStarlarkFileDependencies());
try {
parser.setParent(
prevValue.getPackage(), prevValue.getLoadedModules(), prevValue.getBindings());
} catch (NameConflictException e) {
throw new WorkspaceFileFunctionException(e, Transience.PERSISTENT);
}
}
PackageFactory.transitiveClosureOfLabelsRec(starlarkFileDependencies, loadedModules);
builder.setStarlarkFileDependencies(ImmutableList.copyOf(starlarkFileDependencies));
// Execute the partial files that comprise this chunk.
for (StarlarkFile partialFile : chunk) {
parser.execute(partialFile, loadedModules, key);
Expand Down
55 changes: 55 additions & 0 deletions src/test/py/bazel/query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,53 @@ def testSimpleQuery(self):
self._AssertQueryOutput('deps(//foo:top-rule, 1)', '//foo:top-rule',
'//foo:dep-rule')

def testBuildFilesForExternalRepos_Simple(self):
self.ScratchFile('WORKSPACE', [
'load("//:deps.bzl", "repos")',
'repos()',
])
self.ScratchFile('BUILD.bazel')
self.ScratchFile('deps.bzl', [
'def repos():',
' native.new_local_repository(',
' name = "io_bazel_rules_go",',
' path = ".",',
""" build_file_content = "exports_files(glob(['*.go']))",""",
' )',
])
self._AssertQueryOutputContains('buildfiles(//external:io_bazel_rules_go)',
'//external:WORKSPACE', '//:deps.bzl', '//:BUILD.bazel')

def testBuildFilesForExternalRepos_IndirectLoads(self):
linzhp marked this conversation as resolved.
Show resolved Hide resolved
self.ScratchFile('WORKSPACE', [
'load("//:deps.bzl", "repos")',
'repos()',
])
self.ScratchFile('BUILD.bazel')
self.ScratchFile('deps.bzl', [
'load("//:private_deps.bzl", "other_repos")',
'def repos():',
' native.new_local_repository(',
' name = "io_bazel_rules_go",',
' path = ".",',
""" build_file_content = "exports_files(glob(['*.go']))",""",
' )',
' other_repos()',
'',
])
self.ScratchFile('private_deps.bzl', [
'def other_repos():',
' native.new_local_repository(',
' name = "io_bazel_rules_python",',
' path = ".",',
""" build_file_content = "exports_files(glob(['*.py']))",""",
' )',
])

self._AssertQueryOutputContains('buildfiles(//external:io_bazel_rules_python)',
'//external:WORKSPACE', '//:deps.bzl', '//:private_deps.bzl', '//:BUILD.bazel')


def _AssertQueryOutput(self, query_expr, *expected_results):
exit_code, stdout, stderr = self.RunBazel(['query', query_expr])
self.AssertExitCode(exit_code, 0, stderr)
Expand All @@ -46,6 +93,14 @@ def _AssertQueryOutput(self, query_expr, *expected_results):
self.assertEqual(len(stdout), len(expected_results))
self.assertListEqual(stdout, sorted(expected_results))

def _AssertQueryOutputContains(self, query_expr, *expected_content):
exit_code, stdout, stderr = self.RunBazel(['query', query_expr])
self.AssertExitCode(exit_code, 0, stderr)

stdout = {x for x in stdout if x}
for item in expected_content:
self.assertIn(item, stdout)


if __name__ == '__main__':
unittest.main()