Skip to content

Commit

Permalink
Add new strict_visibility_value= attribute to allow exposing strict d…
Browse files Browse the repository at this point in the history
…ependencies to some scopes

Note that using package default_visibility doesn't always work so explicit visiblity attributes are needed
bazelbuild/bazel#13681

Somewhat related to bazel-contrib#648

In combination this for example allows to create a class_name -> maven artifact resolver for
transitive dependency classes while still keeping strict visibility (making transitive dependencies
visible only to the resolve cli tool)
  • Loading branch information
dmivankov committed Jan 18, 2022
1 parent cbc10ce commit 820bab7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 5 additions & 3 deletions coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ load(
)

_BUILD = """
package(default_visibility = ["//visibility:{visibility}"])
# package(default_visibility = [{visibilities}]) # https://github.com/bazelbuild/bazel/issues/13681
load("@rules_jvm_external//private/rules:jvm_import.bzl", "jvm_import")
load("@rules_jvm_external//private/rules:jetifier.bzl", "jetify_aar_import", "jetify_jvm_import")
Expand Down Expand Up @@ -539,7 +539,7 @@ def _pinned_coursier_fetch_impl(repository_ctx):
repository_ctx.file(
"BUILD",
(_BUILD + _BUILD_OUTDATED).format(
visibility = "private" if repository_ctx.attr.strict_visibility else "public",
visibilities = ",".join(["\"%s\"" % s for s in (["//visibility:public"] if not repository_ctx.attr.strict_visibility else repository_ctx.attr.strict_visibility_value)]),
repository_name = repository_ctx.name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
Expand Down Expand Up @@ -1041,7 +1041,7 @@ def _coursier_fetch_impl(repository_ctx):
repository_ctx.file(
"BUILD",
(_BUILD + _BUILD_PIN + outdated_build_file_content).format(
visibility = "private" if repository_ctx.attr.strict_visibility else "public",
visibilities = ",".join(["\"%s\"" % s for s in (["//visibility:public"] if not repository_ctx.attr.strict_visibility else repository_ctx.attr.strict_visibility_value)]),
repository_name = repository_name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
Expand Down Expand Up @@ -1146,6 +1146,7 @@ pinned_coursier_fetch = repository_rule(
""",
default = False,
),
"strict_visibility_value": attr.label_list(default = ["//visibility:private"]),
"jetify": attr.bool(doc = "Runs the AndroidX [Jetifier](https://developer.android.com/studio/command-line/jetifier) tool on artifacts specified in jetify_include_list. If jetify_include_list is not specified, run Jetifier on all artifacts.", default = False),
"jetify_include_list": attr.string_list(doc = "List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.", default = JETIFY_INCLUDE_LIST_JETIFY_ALL),
"additional_netrc_lines": attr.string_list(doc = "Additional lines prepended to the netrc file used by `http_file` (with `maven_install_json` only).", default = []),
Expand Down Expand Up @@ -1206,6 +1207,7 @@ coursier_fetch = repository_rule(
""",
default = False,
),
"strict_visibility_value": attr.label_list(default = ["//visibility:private"]),
"resolve_timeout": attr.int(default = 600),
"jetify": attr.bool(doc = "Runs the AndroidX [Jetifier](https://developer.android.com/studio/command-line/jetifier) tool on artifacts specified in jetify_include_list. If jetify_include_list is not specified, run Jetifier on all artifacts.", default = False),
"jetify_include_list": attr.string_list(doc = "List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.", default = JETIFY_INCLUDE_LIST_JETIFY_ALL),
Expand Down
16 changes: 11 additions & 5 deletions private/dependency_tree_parser.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ load("//private:coursier_utilities.bzl", "escape", "get_classifier", "get_packag

JETIFY_INCLUDE_LIST_JETIFY_ALL = ["*"]

def _genrule_copy_artifact_from_http_file(artifact):
def _genrule_copy_artifact_from_http_file(artifact, visibilities):
http_file_repository = escape(artifact["coord"])
return "\n".join([
"genrule(",
" name = \"%s_extension\"," % http_file_repository,
" srcs = [\"@%s//file\"]," % http_file_repository,
" outs = [\"%s\"]," % artifact["file"],
" cmd = \"cp $< $@\",",
" visibility = [%s]" % (",".join(["\"%s\"" % v for v in visibilities])),
")",
])

Expand Down Expand Up @@ -65,6 +66,8 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
for coord in override_targets:
labels_to_override.update({escape(coord): override_targets.get(coord)})

default_visibilities = repository_ctx.attr.strict_visibility_value if repository_ctx.attr.strict_visibility else ["//visibility:public"]

# First collect a map of target_label to their srcjar relative paths, and symlink the srcjars if needed.
# We will use this map later while generating target declaration strings with the "srcjar" attr.
srcjar_paths = None
Expand All @@ -78,7 +81,7 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
target_label = escape(strip_packaging_and_classifier_and_version(artifact["coord"]))
srcjar_paths[target_label] = artifact_path
if repository_ctx.attr.maven_install_json:
all_imports.append(_genrule_copy_artifact_from_http_file(artifact))
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))

jetify_all = repository_ctx.attr.jetify and repository_ctx.attr.jetify_include_list == JETIFY_INCLUDE_LIST_JETIFY_ALL

Expand Down Expand Up @@ -112,7 +115,7 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
"alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n\tvisibility = [\"//visibility:public\"],\n)" % (target_label, versioned_target_alias_label),
)
if repository_ctx.attr.maven_install_json:
all_imports.append(_genrule_copy_artifact_from_http_file(artifact))
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))
elif target_label in labels_to_override:
# Override target labels with the user provided mapping, instead of generating
# a jvm_import/aar_import based on information in dep_tree.
Expand All @@ -122,7 +125,7 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
)
if repository_ctx.attr.maven_install_json:
# Provide the downloaded artifact as a file target.
all_imports.append(_genrule_copy_artifact_from_http_file(artifact))
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))
elif artifact_path != None:
seen_imports[target_label] = True

Expand Down Expand Up @@ -287,6 +290,9 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
if repository_ctx.attr.strict_visibility and explicit_artifacts.get(simple_coord):
target_import_string.append("\tvisibility = [\"//visibility:public\"],")
alias_visibility = "\tvisibility = [\"//visibility:public\"],\n"
else:
target_import_string.append("\tvisibility = [%s]," % (",".join(["\"%s\"" % v for v in default_visibilities])))
alias_visibility = "\tvisibility = [%s],\n" % (",".join(["\"%s\"" % v for v in default_visibilities]))

# 9. Finish the java_import rule.
#
Expand Down Expand Up @@ -328,7 +334,7 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
# cmd = "cp $< $@",
# )
if repository_ctx.attr.maven_install_json:
all_imports.append(_genrule_copy_artifact_from_http_file(artifact))
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))

else: # artifact_path == None:
# Special case for certain artifacts that only come with a POM file.
Expand Down
5 changes: 5 additions & 0 deletions private/rules/maven_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def maven_install(
maven_install_json = None,
override_targets = {},
strict_visibility = False,
strict_visibility_value = ["//visibility:private"],
resolve_timeout = 600,
jetify = False,
jetify_include_list = JETIFY_INCLUDE_LIST_JETIFY_ALL,
Expand Down Expand Up @@ -56,6 +57,8 @@ def maven_install(
strict_visibility: Controls visibility of transitive dependencies. If `True`, transitive dependencies
are private and invisible to user's rules. If `False`, transitive dependencies are public and
visible to user's rules.
strict_visibility_value: Allows changing transitive dependencies strict visibility scope from private
to specified scopes list.
resolve_timeout: The execution timeout of resolving and fetching artifacts.
jetify: Runs the AndroidX [Jetifier](https://developer.android.com/studio/command-line/jetifier) tool on artifacts specified in jetify_include_list. If jetify_include_list is not specified, run Jetifier on all artifacts.
jetify_include_list: List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.
Expand Down Expand Up @@ -115,6 +118,7 @@ def maven_install(
version_conflict_policy = version_conflict_policy,
override_targets = override_targets,
strict_visibility = strict_visibility,
strict_visibility_value = strict_visibility_value,
maven_install_json = maven_install_json,
resolve_timeout = resolve_timeout,
jetify = jetify,
Expand All @@ -136,6 +140,7 @@ def maven_install(
generate_compat_repositories = generate_compat_repositories,
override_targets = override_targets,
strict_visibility = strict_visibility,
strict_visibility_value = strict_visibility_value,
jetify = jetify,
jetify_include_list = jetify_include_list,
additional_netrc_lines = additional_netrc_lines,
Expand Down

0 comments on commit 820bab7

Please sign in to comment.