diff --git a/coursier.bzl b/coursier.bzl index 6e54dc333..62c5e02d6 100644 --- a/coursier.bzl +++ b/coursier.bzl @@ -512,7 +512,7 @@ def _pinned_coursier_fetch_impl(repository_ctx): for artifact in dep_tree["dependencies"]: if artifact.get("url") != None: - http_file_repository_name = escape(artifact["coord"]) + http_file_repository_name = repository_ctx.attr.workspace_prefix_for_pinned_deps + escape(artifact["coord"]) maven_artifacts.extend([artifact["coord"]]) http_files.extend([ " http_file(", @@ -1259,6 +1259,10 @@ pinned_coursier_fetch = repository_rule( "none", ], ), + "workspace_prefix_for_pinned_deps": attr.string( + doc = """Prefix to use for names of http_file if using pinned dependencies. For example, with the default empty string, `maven_install` creates intermediate dependencies like `@junit_junit`. With a prefix "my_deps_", the intermediate workspace names becomes `@my_deps_junit_junit`. This is useful to avoid workspace name pollution during migration.""", + default = "", + ), }, implementation = _pinned_coursier_fetch_impl, ) @@ -1320,6 +1324,10 @@ coursier_fetch = repository_rule( "none", ], ), + "workspace_prefix_for_pinned_deps": attr.string( + doc = """Prefix to use for names of http_file if using pinned dependencies. For example, with the default empty string, `maven_install` creates intermediate dependencies like `@junit_junit`. With a prefix "my_deps_", the intermediate workspace names becomes `@my_deps_junit_junit`. This is useful to avoid workspace name pollution during migration.""", + default = "", + ), }, environ = [ "JAVA_HOME", diff --git a/private/dependency_tree_parser.bzl b/private/dependency_tree_parser.bzl index 3b1b4b1af..be9b9b2c5 100644 --- a/private/dependency_tree_parser.bzl +++ b/private/dependency_tree_parser.bzl @@ -31,11 +31,11 @@ load( JETIFY_INCLUDE_LIST_JETIFY_ALL = ["*"] -def _genrule_copy_artifact_from_http_file(artifact, visibilities): +def _genrule_copy_artifact_from_http_file(artifact, visibilities, workspace_prefix_for_pinned_deps): # skip artifacts without any urls (ie: maven local artifacts) if not artifact.get("url"): return "" - http_file_repository = escape(artifact["coord"]) + http_file_repository = workspace_prefix_for_pinned_deps + escape(artifact["coord"]) return "\n".join([ "genrule(", " name = \"%s_extension\"," % http_file_repository, @@ -97,7 +97,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, default_visibilities)) + all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities, repository_ctx.attr.workspace_prefix_for_pinned_deps)) jetify_all = repository_ctx.attr.jetify and repository_ctx.attr.jetify_include_list == JETIFY_INCLUDE_LIST_JETIFY_ALL @@ -141,7 +141,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, default_visibilities)) + all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities, repository_ctx.attr.workspace_prefix_for_pinned_deps)) elif artifact_path != None: seen_imports[target_label] = True @@ -356,7 +356,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, default_visibilities)) + all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities, repository_ctx.attr.workspace_prefix_for_pinned_deps)) 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 c2113faea..22a3c4b84 100644 --- a/private/rules/maven_install.bzl +++ b/private/rules/maven_install.bzl @@ -25,7 +25,9 @@ def maven_install( fail_if_repin_required = False, use_starlark_android_rules = False, aar_import_bzl_label = DEFAULT_AAR_IMPORT_LABEL, - duplicate_version_warning = "warn"): + duplicate_version_warning = "warn", + workspace_prefix_for_pinned_deps = "", + ): """Resolves and fetches artifacts transitively from Maven repositories. This macro runs a repository rule that invokes the Coursier CLI to resolve @@ -74,6 +76,10 @@ def maven_install( duplicate_version_warning: What to do if an artifact is specified multiple times. If "error" then fail the build, if "warn" then print a message and continue, if "none" then do nothing. The default is "warn". + workspace_prefix_for_pinned_deps: Prefix to use for names of http_file if using pinned dependencies. + For example, with the default empty string, `maven_install` creates intermediate dependencies like + `@junit_junit`. With a prefix "my_deps_", the intermediate workspace names becomes + `@my_deps_junit_junit`. This is useful to avoid workspace name pollution during migration. """ repositories_json_strings = [] for repository in parse.parse_repository_spec_list(repositories): @@ -148,4 +154,5 @@ def maven_install( duplicate_version_warning = duplicate_version_warning, use_starlark_android_rules = use_starlark_android_rules, aar_import_bzl_label = aar_import_bzl_label, + workspace_prefix_for_pinned_deps = workspace_prefix_for_pinned_deps, ) diff --git a/repositories.bzl b/repositories.bzl index a5cf9383f..94a4d7f8a 100644 --- a/repositories.bzl +++ b/repositories.bzl @@ -18,4 +18,5 @@ def rules_jvm_external_deps(repositories = _DEFAULT_REPOSITORIES): fail_if_repin_required = True, strict_visibility = True, repositories = repositories, + workspace_prefix_for_pinned_deps = "_rules_jvm_external_deps", )