Skip to content

Commit d273cb6

Browse files
cpsauercopybara-github
authored andcommitted
Unify URL/URLs parameter code across http_archive, http_file, http_jar
Hi wonderful Bazel folks, I--like probably everyone else--make heavy use of the (super handy) http repository rules. While using them, I'd noticed that the docs for the URL attributes they have in common had started drifting. For example, http_file's URLs array doc was mostly same as http_archive's, except that it forgot to specify that the URLs were tried in the order they were specified. jar_file's were much less complete. Similarly, only http_file was missing the URL parameter, which seems a shame, since probably most people only want to supply one authoritative source most of the time. When I looked at the code, it became clear that this was because the implementations were copy-pasted, and being updated separately. So I spent a few minutes cleaning things up and making these commonly used features a little more consistent, fixing the issues above. Hopefully that's welcome. I'd love your thoughtful consideration and review! Thanks, wonderful Bazel folks, for all you do! Cheers, Chris (ex-Googler) P.S. I've given edit access with the "allow edits by maintainers" box. If there are things you'd like changed, feel free also to just make them directly! Closes bazelbuild#15408. PiperOrigin-RevId: 449225740
1 parent e575800 commit d273cb6

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

tools/build_defs/repo/http.bzl

+45-55
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,38 @@ load(
4141
)
4242

4343
# Shared between http_jar, http_file and http_archive.
44+
45+
_URL_DOC = """A URL to a file that will be made available to Bazel.
46+
47+
This must be a file, http or https URL. Redirections are followed.
48+
Authentication is not supported.
49+
50+
More flexibility can be achieved by the urls parameter that allows
51+
to specify alternative URLs to fetch from."""
52+
53+
_URLS_DOC = """A list of URLs to a file that will be made available to Bazel.
54+
55+
Each entry must be a file, http or https URL. Redirections are followed.
56+
Authentication is not supported.
57+
58+
URLs are tried in order until one succeeds, so you should list local mirrors first.
59+
If all downloads fail, the rule will fail."""
60+
61+
def _get_all_urls(ctx):
62+
"""Returns all urls provided via the url or urls attributes.
63+
64+
Also checks that at least one url is provided."""
65+
if not ctx.attr.url and not ctx.attr.urls:
66+
fail("At least one of url and urls must be provided")
67+
68+
all_urls = []
69+
if ctx.attr.urls:
70+
all_urls = ctx.attr.urls
71+
if ctx.attr.url:
72+
all_urls = [ctx.attr.url] + all_urls
73+
74+
return all_urls
75+
4476
_AUTH_PATTERN_DOC = """An optional dict mapping host names to custom authorization patterns.
4577
4678
If a URL's host name is present in this dict the value will be used as a pattern when
@@ -84,17 +116,10 @@ def _get_auth(ctx, urls):
84116

85117
def _http_archive_impl(ctx):
86118
"""Implementation of the http_archive rule."""
87-
if not ctx.attr.url and not ctx.attr.urls:
88-
fail("At least one of url and urls must be provided")
89119
if ctx.attr.build_file and ctx.attr.build_file_content:
90120
fail("Only one of build_file and build_file_content can be provided.")
91121

92-
all_urls = []
93-
if ctx.attr.urls:
94-
all_urls = ctx.attr.urls
95-
if ctx.attr.url:
96-
all_urls = [ctx.attr.url] + all_urls
97-
122+
all_urls = _get_all_urls(ctx)
98123
auth = _get_auth(ctx, all_urls)
99124

100125
download_info = ctx.download_and_extract(
@@ -138,9 +163,10 @@ def _http_file_impl(ctx):
138163
download_path = ctx.path("file/" + downloaded_file_path)
139164
if download_path in forbidden_files or not str(download_path).startswith(str(repo_root)):
140165
fail("'%s' cannot be used as downloaded_file_path in http_file" % ctx.attr.downloaded_file_path)
141-
auth = _get_auth(ctx, ctx.attr.urls)
166+
all_urls = _get_all_urls(ctx)
167+
auth = _get_auth(ctx, all_urls)
142168
download_info = ctx.download(
143-
ctx.attr.urls,
169+
all_urls,
144170
"file/" + downloaded_file_path,
145171
ctx.attr.sha256,
146172
ctx.attr.executable,
@@ -173,11 +199,7 @@ filegroup(
173199

174200
def _http_jar_impl(ctx):
175201
"""Implementation of the http_jar rule."""
176-
all_urls = []
177-
if ctx.attr.urls:
178-
all_urls = ctx.attr.urls
179-
if ctx.attr.url:
180-
all_urls = [ctx.attr.url] + all_urls
202+
all_urls = _get_all_urls(ctx)
181203
auth = _get_auth(ctx, all_urls)
182204
downloaded_file_name = ctx.attr.downloaded_file_name
183205
download_info = ctx.download(
@@ -192,28 +214,8 @@ def _http_jar_impl(ctx):
192214
return update_attrs(ctx.attr, _http_jar_attrs.keys(), {"sha256": download_info.sha256})
193215

194216
_http_archive_attrs = {
195-
"url": attr.string(
196-
doc =
197-
"""A URL to a file that will be made available to Bazel.
198-
199-
This must be a file, http or https URL. Redirections are followed.
200-
Authentication is not supported.
201-
202-
This parameter is to simplify the transition from the native http_archive
203-
rule. More flexibility can be achieved by the urls parameter that allows
204-
to specify alternative URLs to fetch from.
205-
""",
206-
),
207-
"urls": attr.string_list(
208-
doc =
209-
"""A list of URLs to a file that will be made available to Bazel.
210-
211-
Each entry must be a file, http or https URL. Redirections are followed.
212-
Authentication is not supported.
213-
214-
URLs are tried in order until one succeeds, so you should list local mirrors first.
215-
If all downloads fail, the rule will fail.""",
216-
),
217+
"url": attr.string(doc = _URL_DOC),
218+
"urls": attr.string_list(doc = _URLS_DOC),
217219
"sha256": attr.string(
218220
doc = """The expected SHA-256 of the file downloaded.
219221
@@ -393,7 +395,7 @@ Examples:
393395
394396
http_archive(
395397
name = "my_ssl",
396-
urls = ["http://example.com/openssl.zip"],
398+
url = "http://example.com/openssl.zip",
397399
sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
398400
build_file = "@//:openssl.BUILD",
399401
)
@@ -426,13 +428,8 @@ If specified and non-empty, bazel will not take the archive from cache,
426428
unless it was added to the cache by a request with the same canonical id.
427429
""",
428430
),
429-
"urls": attr.string_list(
430-
mandatory = True,
431-
doc = """A list of URLs to a file that will be made available to Bazel.
432-
433-
Each entry must be a file, http or https URL. Redirections are followed.
434-
Authentication is not supported.""",
435-
),
431+
"url": attr.string(doc = _URL_DOC),
432+
"urls": attr.string_list(doc = _URLS_DOC),
436433
"netrc": attr.string(
437434
doc = "Location of the .netrc file to use for authentication",
438435
),
@@ -458,7 +455,7 @@ Examples:
458455
459456
http_file(
460457
name = "my_deb",
461-
urls = ["http://example.com/package.deb"],
458+
url = "http://example.com/package.deb",
462459
sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
463460
)
464461
```
@@ -478,15 +475,8 @@ If specified and non-empty, bazel will not take the archive from cache,
478475
unless it was added to the cache by a request with the same canonical id.
479476
""",
480477
),
481-
"url": attr.string(
482-
doc =
483-
"The URL to fetch the jar from. It must end in `.jar`.",
484-
),
485-
"urls": attr.string_list(
486-
doc =
487-
"A list of URLS the jar can be fetched from. They have to end " +
488-
"in `.jar`.",
489-
),
478+
"url": attr.string(doc = _URL_DOC + "\n\nThe URL must end in `.jar`."),
479+
"urls": attr.string_list(doc = _URLS_DOC + "\n\nAll URLs must end in `.jar`."),
490480
"netrc": attr.string(
491481
doc = "Location of the .netrc file to use for authentication",
492482
),

0 commit comments

Comments
 (0)