Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read .netrc in go_repository #1090

Merged
merged 2 commits into from
Aug 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,29 @@

load("@io_bazel_rules_go//go/private:common.bzl", "env_execute", "executable_extension")
load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc")

# We can't disable timeouts on Bazel, but we can set them to large values.
_GO_REPOSITORY_TIMEOUT = 86400

# Inspired by @bazel_tools//tools/build_defs/repo:http.bzl
def _get_auth(ctx, urls):
linzhp marked this conversation as resolved.
Show resolved Hide resolved
"""Given the list of URLs obtain the correct auth dict."""
netrcfile = ""
if "NETRC" in ctx.os.environ:
netrcfile = ctx.os.environ["NETRC"]
elif ctx.os.name.startswith("windows"):
if "USERPROFILE" in ctx.os.environ:
netrcfile = "%s/_netrc" % (ctx.os.environ["USERPROFILE"])
elif "HOME" in ctx.os.environ:
netrcfile = "%s/.netrc" % (ctx.os.environ["HOME"])

if netrcfile and ctx.path(netrcfile).exists:
netrc = read_netrc(ctx, netrcfile)
return use_netrc(netrc, urls, {})

return {}

def _go_repository_impl(ctx):
# TODO(#549): vcs repositories are not cached and still need to be fetched.
# Download the repository or module.
Expand All @@ -33,6 +52,7 @@ def _go_repository_impl(ctx):
sha256 = ctx.attr.sha256,
stripPrefix = ctx.attr.strip_prefix,
type = ctx.attr.type,
auth = _get_auth(ctx, ctx.attr.urls),
linzhp marked this conversation as resolved.
Show resolved Hide resolved
)
elif ctx.attr.commit or ctx.attr.tag:
# repository mode
Expand Down