Skip to content

Should git_repository use repository cache? #5116

Closed

Description

@ittaiz

Description of the problem / feature request:

have new_git_repository rules use the repository_cache

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

I'd like to be able to reuse external git dependencies like one can reuse new_http_archive

Activity

  1. aehlig commented on May 15, 2018

    @aehlig
    Contributor

    I think, in general it is a good idea, especially as git basically is a content-addressable storage, so even individual shared blobs could be avoided being downloaded again.

    Note that we're moving from the native git_repository rules to the Skylark version. So, assuming you're given a directory that you can use as cache, what would be the appropriate git commands to make best use of it?

  2. ittaiz commented on Jul 16, 2018

    @ittaiz
    MemberAuthor

    @aehlig given your work on WORKSPACE.resolved and git_repository this sounds like it might be super relevant, right?

  3. aehlig commented on Jul 16, 2018

    @aehlig
    Contributor

    As mentioned in #5086, one possible way forward might be telling the Skylark git_repository rule (e.g., via an environment variable) about the location of a git repository on the local machine that might be used as a cache. But still, I'm not sure what the best option is.

  4. aoded commented on Aug 13, 2018

    @aoded

    Hi @aehlig, we would appreciate some help with this :)

    We tried to work on this feature, and while modifying this method -

    def _clone_or_update(ctx):
    we wanted to get the path to the cache folder either from environment or command line arguments.
    We tried using ctx.os.environ, but adding a new environment variable didn't affect its value
    We tired looking for a way to get a command line parameter value via ctx and found no such way

    Thanks!
    Oded

  5. aehlig commented on Aug 14, 2018

    @aehlig
    Contributor

    we wanted to get the path to the cache folder either from environment or command line arguments. We tried using ctx.os.environ, but adding a new environment variable didn't affect its value

    ctx.os.environ does refelect the invocation environment; however, a repository is only invalidated based on the declared environment variables—but you can always use bazel sync. (Note that the cache location should not invalidate the repository, as its content do not depend on the location of the cache.)

  6. added
    team-ExternalDeps
    and removed
    team-ExternalDeps
    untriaged
    on Nov 26, 2018
  7. 3 remaining items

  8. removed
    team-OSS
    on Nov 29, 2021
  9. sluongng commented on Feb 15, 2022

    @sluongng
    Contributor

    It's worth to note that #7424 has almost finished an implementation of git_repository cache but was abandoned as the author has no capacity to support it. Whoever want to pick up this issue should consider using it as reference.

    I just want to note that in upstream Git core, Microsoft's Scalar is being integrated into contrib dir and is planned to eventually integrate to be part of Git itself. If you are not familiar with Scalar, it's a high level 'git repository manager' command where it helps:

    1. Set a sane default git configs
    2. Help cache all git repositories it manage
    3. Setup maintenance background jobs on top of the cache and the managed repositories

    While (3) has been merged into Git Core as git-maintenance recently, (1) and (2) functionality is still being worked on. I would suggest that the repository cache adopt the same caching convention as Scalar as there are some overlapping interests between (a) Bazel users and (b) Monorepo workflow users and (c) Scalar users.

    On a high level, this is how Scalar git repository cache works:

    # User command
    > scalar clone <url-to-repo> <dest-directory>
    
    # Scalar commands underneath
    > CACHE_KEY=$(sha256sum url-to-repo)
    > CACHE_DIR="$HOME/.cache/scalar/$CACHE_KEY"
    > mkdir -p $CACHE_DIR
    > git clone --mirror <url-to-repo> $CACHE_DIR
    > git clone --reference-if-able $CACHE_DIR <url-to-repo> <dest-directory>
    

    I am gloating over the details, but using git clone --reference would setup a .git/info/alternates file pointing back to $CACHE_DIR. This helps signal git to reuse the Object Database(ODB) inside cache dir.

    Using git clone --reference-if-able, in my opinion, is a better approach than git worktree as the cloned repo comes with a separate set of git configs and git index which let you apply another level of modification (i.e. applying a sparse-checkout) without affecting/leaving residual states in the cache dir.

    However to make offline cache support to work, implementer most likely wont be able to use git clone directly. They should consider using a set of plumping command instead to prioritize making offline cache works.

    mkdir -p <dest-directory>
    git init
    echo "$CACHE_DIR" > .git/info/alternates
    git checkout <sha / branch / tag> && exit 0 || echo 'does not find commit in cache' && exit 1
    
    # Update cache repo
    git -C "$CACHE_DIR" config remote.origin.url <url-to-repo>
    git -C "$CACHE_DIR" fetch origin <branch>
    
    # 2nd attempt
    git checkout <sha / branch / tag> && exit 0 || echo 'Could not find commit at all' && exit 1
    

    Question: what is the best way for user to configure $CACHE_DIR location? Environment variable or a bazel flag?

  10. github-actions commented on Jun 13, 2023

    @github-actions

    Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team (@bazelbuild/triage) if you think this issue is still relevant or you are interested in getting the issue resolved.

  11. added
    stale
    on Jun 13, 2023
  12. bcsgh commented on Jun 13, 2023

    @bcsgh

    I ran across this issue just yesterday: a repo I'm trying to build has some submodules on a very slow server (and because they are submodules, I can't just choose a faster replica).

  13. fmeum commented on Jun 13, 2023

    @fmeum
    Collaborator
  14. removed
    stale
    on Jun 14, 2023
  15. bcsgh commented on Dec 19, 2023

    @bcsgh

    Side note: one trick to reducing the issues that slow downloads cause (at least while developing the injected BUILD file) is to avoid all need to change anything actually in git_repository.build_file by making is be nothing more than a reference back to a .bzl file in the root project:

    # //repository:BUILD.slow_project
    load("@my_project//repository:slow_project.bzl", "BUILD")
    BUILD()
    
    # //repository:slow_project.bzl
    def BUILD():
      # Stuff that would normally be in :BUILD.slow_project
      # ...
    

    That also works with git_repository.build_file_content and for something that small that's likely the way to go.

  16. amfern commented on Jul 24, 2024

    @amfern

    i just stumped upon this issue, the build suppose to happen offline, so i would like to copy the cache to an offline server.
    However if the project is using git_repository i am not able to build offline.

  17. added a commit that references this issue on Jun 28, 2025
  18. fmeum commented on Aug 14, 2025

    @fmeum
    Collaborator

    As of Bazel 8.3.1, git_repository with commit specified is marked as reproducible and thus uses the --repo_contents_cache.

  19. tmewett commented on Sep 2, 2025

    @tmewett

    Nice, thank you!

    The git_repository docs still contain this:

    http_archive works with the repository cache, but not git_repository. See #5116 for more information.

    What should it say instead now? Should it be removed entirely? Or something like this?

    http_archive uses the full repository cache system, whereas git_repository only uses the --repo_contents_cache

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