diff --git a/coursier.bzl b/coursier.bzl index 72f13a05e..c9e612eda 100644 --- a/coursier.bzl +++ b/coursier.bzl @@ -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") @@ -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), @@ -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), @@ -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 = []), @@ -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), diff --git a/private/dependency_tree_parser.bzl b/private/dependency_tree_parser.bzl index 3b0a3ff64..be5f4ee98 100644 --- a/private/dependency_tree_parser.bzl +++ b/private/dependency_tree_parser.bzl @@ -21,7 +21,7 @@ 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(", @@ -29,6 +29,7 @@ def _genrule_copy_artifact_from_http_file(artifact): " srcs = [\"@%s//file\"]," % http_file_repository, " outs = [\"%s\"]," % artifact["file"], " cmd = \"cp $< $@\",", + " visibility = [%s]" % (",".join(["\"%s\"" % v for v in visibilities])), ")", ]) @@ -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 @@ -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 @@ -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. @@ -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 @@ -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. # @@ -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. diff --git a/private/rules/maven_install.bzl b/private/rules/maven_install.bzl index c2199d20d..738e36309 100644 --- a/private/rules/maven_install.bzl +++ b/private/rules/maven_install.bzl @@ -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, @@ -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. @@ -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, @@ -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,