Skip to content

Commit

Permalink
Add support for netrc in jvm_maven_import_external (#1509)
Browse files Browse the repository at this point in the history
`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`)
  • Loading branch information
dmivankov authored Oct 2, 2023
1 parent 73719cb commit 0391ef4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion scala/scala_maven_import_external.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ the following macros are defined below that utilize jvm_import_external:
- java_import_external - to demonstrate that the original functionality of `java_import_external` stayed intact.
"""

load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "read_user_netrc", "use_netrc")

# https://github.com/bazelbuild/bazel/issues/13709#issuecomment-1336699672
def _get_auth(ctx, urls):
"""Given the list of URLs obtain the correct auth dict."""
if ctx.attr.netrc:
netrc = read_netrc(ctx, ctx.attr.netrc)
else:
netrc = read_user_netrc(ctx)
return use_netrc(netrc, urls, ctx.attr.auth_patterns)

_HEADER = "# DO NOT EDIT: generated by jvm_import_external()"
_PASS_PROPS = (
"neverlink",
Expand Down Expand Up @@ -109,7 +120,7 @@ def _jvm_import_external(repository_ctx):
lines.append(extra)
if not extra.endswith("\n"):
lines.append("")
repository_ctx.download(urls, path, sha)
repository_ctx.download(urls, path, sha, auth = _get_auth(repository_ctx, urls))
if srcurls and _should_fetch_sources_in_current_env(repository_ctx):
repository_ctx.download(srcurls, srcpath, srcsha)
repository_ctx.file("BUILD", "\n".join(lines))
Expand Down Expand Up @@ -227,6 +238,8 @@ jvm_import_external = repository_rule(
default = ["//visibility:public"],
),
"extra_build_file_content": attr.string(),
"auth_patterns": attr.string_dict(),
"netrc": attr.string(),
},
environ = [_FETCH_SOURCES_ENV_VAR_NAME],
)
Expand Down

0 comments on commit 0391ef4

Please sign in to comment.