Skip to content

Skylark API for accessing repository cache in repository rules #5086

Closed

Description

@jayconrod

Description of the problem / feature request:

Repository rules implemented in Skylark should have the ability to read and write files or directories in the repository cache. This would be useful for downloading files using something other than HTTP. It would also be useful for verifying downloads using mechanisms other than SHA-256 on the archive.

For example, here's how this might be used to checkout a git repository.

def _git_repository_impl(ctx):
    key = struct(
        remote = ctx.attr.remote,
        commit = ctx.attr.commit,
    )
    cache_path = ctx.cache.get(key)
    if cache_path:
        ctx.execute(["cp", "-r", cache_path, "."])
        return
    ctx.execute(["git", "clone", "-n", ctx.attr.remote, "."])
    ctx.execute(["git", "checkout", revision])
    ctx.execute(["rm", "-rf", ".git"])
    ctx.cache.put(key, ".")

Feature requests: what underlying problem are you trying to solve with this feature?

In rules_go, users usually checkout dependencies using go_repository with a GitHub URL. It's not common practice to attach source archives to releases, so most of the time, this rule downloads repositories with git clone (other VCSs are supported, too). There should be a way for these downloads to be cached.

go_repository may also download repository archives over HTTP. GitHub has an endpoint for this, codeload.github.com. However, GitHub generates archives on request, and their SHA-256 is not stable. For example, things like alignment or file order may change when GitHub updates their server software. This broke us a few times, and we've largely abandoned the practice of using sha256 on codeload.github.com URLs. Ideally, we could verify the SHA-256 of the archive contents instead of the archive itself, since that should never change.

Activity

  1. meteorcloudy commented on Apr 25, 2018

    @meteorcloudy
    Member

    @laurentlb Can you take a look? :)

  2. ittaiz commented on Jul 16, 2018

    @ittaiz
    Member

    @aehlig any thoughts given your recent work?

  3. aehlig commented on Jul 16, 2018

    @aehlig
    Contributor

    @aehlig any thoughts given your recent work?

    I don't think my recent work influences this feature request.

    My general thoughts are that, on the one hand, it is a good idea to allow other cache keys. But the main concern is correctness. For the simple hash of a file, bazel can easily verify itself that the cache hit is correct, so all is fine. For the API proposed in that feature request, however, arbitrary repository rules can add arbitrary values into the cache, making (accidental) cache poising a real issue. Thus, a proper design avoiding this danger still has to be found.

    For git in particular, which is basically a content-addressable store, giving optional access to a "cache repository" (where arbitrary commits can be pushed to) might be a good plan forward. That can already be implement in Skylark as we currently have it (providing the cache repository as an optional argument containing an absolute path outside bazel), if someone finds the time.

  4. added
    team-ExternalDeps
    P3
    and removed on Mar 22, 2019
  5. removed their assignment
    on Oct 8, 2019
  6. fenollp commented on Apr 21, 2020

    @fenollp

    Another advantage of caching contents instead of the "envelopes": toolchains could be cached.
    On Bazel 3.0.0 (at least) the same toolchains can be duplicated in multiple output_bases and take a few GBs each.
    This is common with bazel sync-ing a resolved file.


    arbitrary repository rules can add arbitrary values into the cache, making (accidental) cache poisoning a real issue

    Can't this be solved by passing the "cache miss" function along with the (key,value)?
    For instance: ({"urls": [..]}, native.download_and_extract, {"sha256": ...}).

    i.e. color cache key with a callback macro's class (@dep//tar:get%macro, or all the way to a hash of its AST). Calling this macro should be able to reproduce the cached value.

  7. added
    team-OSS
    on Jun 15, 2020
  8. removed
    team-OSS
    on Nov 29, 2021
  9. ckolli5 commented on Jun 10, 2022

    @ckolli5

    Hey @jayconrod, Hope you are doing well!
    I wanted to check with you on the above issue whether if you are still able to reproduce or resolved, Can you please share the complete details. It will be closed very soon, if no further activity occurs on this issue. Thank you.

  10. sgowroji commented on Jun 17, 2022

    @sgowroji
    Member

    @jayconrod, I'm going to go ahead and close this Issue, because it seems to have stalled from long duration. If you're still interested in pursing this (and responding to above comments), please feel free to open a new issue adding this as reference!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3team-ExternalDepstype: feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions