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

git_repository() is sensitive to GIT_DIR #24199

Closed
rpwoodbu opened this issue Nov 5, 2024 · 1 comment
Closed

git_repository() is sensitive to GIT_DIR #24199

rpwoodbu opened this issue Nov 5, 2024 · 1 comment
Labels
P2 We'll consider working on this in future. (Assignee optional) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug

Comments

@rpwoodbu
Copy link
Contributor

rpwoodbu commented Nov 5, 2024

Description of the bug:

If GIT_DIR happens to be set, git_repository() may operate on the wrong clone. It is never useful for git_repository() to be influenced by these GIT_ variables.

It is notable that Git sets GIT_DIR and others when calling hooks. This complicates calling Bazel from within a hook (e.g., bazel test). In particular, when using Git worktrees, this can lead to core.bare = true being set, breaking the user's clone.

Which category does this issue belong to?

No response

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

# MODULE.bazel
git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
    name = "foo",
    commit = "1bbb388780f6016b6981a2f075fb457e999bc3bd",
    remote = "https://github.com/bazelbuild/bazel-skylib.git",
)
# In your terminal:
export GIT_DIR=""
bazel clean --expunge
bazel query @foo//:all

Produces the following error:

Repository rule git_repository defined at:
  /var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git.bzl:189:33: in <toplevel>
ERROR: /var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git_worker.bzl:210:9: An error occurred during the fetch of repository '_main~_repo_rules~foo':
   Traceback (most recent call last):
        File "/var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git.bzl", line 180, column 35, in _git_repository_implementation
                update = _clone_or_update_repo(ctx)
        File "/var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git.bzl", line 43, column 20, in _clone_or_update_repo
                git_ = git_repo(ctx, directory)
        File "/var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 98, column 12, in git_repo
                _update(ctx, git_repo)
        File "/var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 114, column 9, in _update
                init(ctx, git_repo)
        File "/var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 131, column 15, in init
                _error(ctx.name, cl, st.stderr)
        File "/var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/bazel_tools/tools/build_defs/repo/git_worker.bzl", line 210, column 9, in _error
                fail("error running '%s' while working with @%s:\n%s" % (command_text, name, stderr))
Error in fail: error running 'git init /var/tmp/rpwoodbu/.cache/bazel/bc5487f033a0af65dff1ff0bd4000d75/external/_main~_repo_rules~foo' while working with @_main~_repo_rules~foo:
fatal: The empty string is not a valid path

Which operating system are you running Bazel on?

Debian Linux

What is the output of bazel info release?

release 7.4.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@satyanandak satyanandak added the team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. label Nov 5, 2024
@meteorcloudy meteorcloudy added P2 We'll consider working on this in future. (Assignee optional) and removed untriaged labels Nov 5, 2024
bazel-io pushed a commit to bazel-io/bazel that referenced this issue Nov 12, 2024
This ensures that this repository rule cannot be influenced by these environment variables, particularly `GIT_DIR`, keeping the operation limited to the directory specified by the `repository_ctx`.

In particular, this ensures that calling Bazel (e.g. `bazel test`) from within a Git hook will work reliably. Git hooks set `GIT_DIR` (among others), which can cause the repository rule's invocations of `git` to misbehave. https://git-scm.com/docs/githooks

When using Git worktrees and calling Bazel from a Git hook, it has been observed that `git_repository()` will not only fail, but will also set `core.bare = true`, breaking the user's clone.

Fixes bazelbuild#24199.

Closes bazelbuild#24204.

PiperOrigin-RevId: 695642299
Change-Id: Id81e32194117cd8996408b236a6a88a20b14910b
github-merge-queue bot pushed a commit that referenced this issue Nov 13, 2024
This ensures that this repository rule cannot be influenced by these
environment variables, particularly `GIT_DIR`, keeping the operation
limited to the directory specified by the `repository_ctx`.

In particular, this ensures that calling Bazel (e.g. `bazel test`) from
within a Git hook will work reliably. Git hooks set `GIT_DIR` (among
others), which can cause the repository rule's invocations of `git` to
misbehave. https://git-scm.com/docs/githooks

When using Git worktrees and calling Bazel from a Git hook, it has been
observed that `git_repository()` will not only fail, but will also set
`core.bare = true`, breaking the user's clone.

Fixes #24199.

Closes #24204.

PiperOrigin-RevId: 695642299
Change-Id: Id81e32194117cd8996408b236a6a88a20b14910b

Commit
89115d9

Co-authored-by: Richard Woodbury <[email protected]>
@iancha1992
Copy link
Member

A fix for this issue has been included in Bazel 8.0.0 RC3. Please test out the release candidate and report any issues as soon as possible.
If you're using Bazelisk, you can point to the latest RC by setting USE_BAZEL_VERSION=8.0.0rc3. Thanks!

ramil-bitrise pushed a commit to bitrise-io/bazel that referenced this issue Dec 18, 2024
This ensures that this repository rule cannot be influenced by these environment variables, particularly `GIT_DIR`, keeping the operation limited to the directory specified by the `repository_ctx`.

In particular, this ensures that calling Bazel (e.g. `bazel test`) from within a Git hook will work reliably. Git hooks set `GIT_DIR` (among others), which can cause the repository rule's invocations of `git` to misbehave. https://git-scm.com/docs/githooks

When using Git worktrees and calling Bazel from a Git hook, it has been observed that `git_repository()` will not only fail, but will also set `core.bare = true`, breaking the user's clone.

Fixes bazelbuild#24199.

Closes bazelbuild#24204.

PiperOrigin-RevId: 695642299
Change-Id: Id81e32194117cd8996408b236a6a88a20b14910b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 We'll consider working on this in future. (Assignee optional) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants