bazel icon indicating copy to clipboard operation
bazel copied to clipboard

http.bzl http_archive downloads multiple times if build_file is specified

Open jemdiggity opened this issue 9 years ago • 6 comments

Description of the problem:

If you override the native rule for http_archive by loading http.bzl, and provide a build_file argument to http_archive, bazel downloads the file 3 times. If build_file_content is provided as empty or neither build_file_content or build_file are provided, it downloads 1 time as expected.

I was trying to write my own repository rule based on http.bzl and found that my rule was downloading external dependencies multiple times.

reproduce the problem:

There's a test repository at https://github.com/jemdiggity/test_bazel_external_dependencies.git

WORKSPACE:

load(
    "@bazel_tools//tools/build_defs/repo:http.bzl",
    "http_archive",
)

http_archive(
  name = "test_1M",
  build_file = "//:test.BUILD",
  urls = ["http://127.0.0.1:8000/random_1M.tar.gz"],
  sha256 = "45939342315aaefed2215e21fe2c2280e0a06346b33304e9dbd241c5eb4a31cb",
)

test.sh:

python -m SimpleHTTPServer &

bazel clean --expunge
bazel fetch @test_1M//...

output:

INFO: Starting clean (this may take a while). Consider using --expunge_async if the clean takes more than several minutes.
...........................................Serving HTTP on 0.0.0.0 port 8000 ...
....................
127.0.0.1 - - [18/Mar/2017 12:08:20] "GET /random_1M.tar.gz HTTP/1.1" 200 -
127.0.0.1 - - [18/Mar/2017 12:08:20] "GET /random_1M.tar.gz HTTP/1.1" 200 -
127.0.0.1 - - [18/Mar/2017 12:08:20] "GET /random_1M.tar.gz HTTP/1.1" 200 -
WARNING: /private/var/tmp/_bazel_jeremyhale/85a0eef1523b5b3d420a647e3e90da76/external/bazel_tools/tools/build_defs/repo/http.bzl:42:5: ctx.attr.build_file //:test.BUILD.
INFO: All external dependencies fetched successfully.

Environment info

  • Operating System: Darwin Jeremys-MacBook.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

  • Bazel version (output of bazel info release): release 0.4.5-homebrew

  • If bazel info release returns "development version" or "(@non-git)", please tell us what source tree you compiled Bazel from; git commit hash is appreciated (git rev-parse HEAD):

Have you found anything relevant by searching the web? (e.g. GitHub issues, email threads in the [email protected] archive)

Anything else, information or logs or outputs that would be helpful?

(If they are large, please upload as attachment or provide link).

jemdiggity avatar Mar 18 '17 03:03 jemdiggity

Putting ctx.download_and_extract at the end of function _http_archive_impl seems to fix the multiple downloads. Not sure why....

jemdiggity avatar Mar 20 '17 00:03 jemdiggity

@kchodorow can you help please?

hermione521 avatar Mar 20 '17 12:03 hermione521

Manged to reproduce the issue; thanks a lot for the detailed reproduction case!

The underlying issue seems to be the implicit restart in the evaluation of skylark code; the HttpDownloader gets called several times. As a workaround, while I'm working on a proper solution, I can offer the use of --experimental_repository_cache which will ensure that the second and third download is at least taken from local disk, so the network is accessed only once. I hope, we can get that feature out of experimental soon (as it will also help with several independent projects sharing a common dependency).

-- Klaus Aehlig Google Germany GmbH, Erika-Mann-Str. 33, 80636 Muenchen Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Geschaeftsfuehrer: Paul Terence Manicle, Halimah DeLaine Prado

aehlig avatar Jan 25 '18 12:01 aehlig

ada0c370c2a3ea484925bc4e7d927b015bc4ad92 makes http_archive work well with the current restart semantics: it makes sure, we reference everything we need later before calling out to the network the first time.

However, there is an ongoing design discussion about changing the current semantics to something that makes it easier to write correct repository rules. So I leave the issue open as a placeholder of how the repository rules should work to avoid unnecessary (and, in particular, duplicate) network access.

aehlig avatar Jan 26 '18 07:01 aehlig

As ada0c37 fixes the immediate issue, closing this. #4533 tracks the larger issue (technically classified as feature request, as current behavior is actually docoemented and WAI, although I do view this as a design bug)

dslomov avatar Jan 27 '18 11:01 dslomov

Dear @aehlig , @dslomov ,

Does bazel have released bazel-0.15.0-without-jdk-installer-linux-x86_64.sh ? Thank you for your help.

stevenyslins avatar Jun 29 '18 11:06 stevenyslins