Skip to content

Commit 0391ef4

Browse files
authored
Add support for netrc in jvm_maven_import_external (#1509)
`auth=` parameter for `repository_ctx.download` is both required to fetch from private registries and is not well-documented bazelbuild/bazel#13709 (comment) With this change ```python rules_scala_toolchain_deps_repositories( maven_servers = ["some_private_artifactory_url"] ) ``` would be able to authenticate using default .netrc (like `~/.netrc`)
1 parent 73719cb commit 0391ef4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

scala/scala_maven_import_external.bzl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ the following macros are defined below that utilize jvm_import_external:
3535
- java_import_external - to demonstrate that the original functionality of `java_import_external` stayed intact.
3636
"""
3737

38+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "read_user_netrc", "use_netrc")
39+
40+
# https://github.com/bazelbuild/bazel/issues/13709#issuecomment-1336699672
41+
def _get_auth(ctx, urls):
42+
"""Given the list of URLs obtain the correct auth dict."""
43+
if ctx.attr.netrc:
44+
netrc = read_netrc(ctx, ctx.attr.netrc)
45+
else:
46+
netrc = read_user_netrc(ctx)
47+
return use_netrc(netrc, urls, ctx.attr.auth_patterns)
48+
3849
_HEADER = "# DO NOT EDIT: generated by jvm_import_external()"
3950
_PASS_PROPS = (
4051
"neverlink",
@@ -109,7 +120,7 @@ def _jvm_import_external(repository_ctx):
109120
lines.append(extra)
110121
if not extra.endswith("\n"):
111122
lines.append("")
112-
repository_ctx.download(urls, path, sha)
123+
repository_ctx.download(urls, path, sha, auth = _get_auth(repository_ctx, urls))
113124
if srcurls and _should_fetch_sources_in_current_env(repository_ctx):
114125
repository_ctx.download(srcurls, srcpath, srcsha)
115126
repository_ctx.file("BUILD", "\n".join(lines))
@@ -227,6 +238,8 @@ jvm_import_external = repository_rule(
227238
default = ["//visibility:public"],
228239
),
229240
"extra_build_file_content": attr.string(),
241+
"auth_patterns": attr.string_dict(),
242+
"netrc": attr.string(),
230243
},
231244
environ = [_FETCH_SOURCES_ENV_VAR_NAME],
232245
)

0 commit comments

Comments
 (0)