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

Make git_repository() ignore GIT_ env vars #24204

Closed

Conversation

rpwoodbu
Copy link
Contributor

@rpwoodbu rpwoodbu commented Nov 5, 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.

@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Nov 5, 2024
@@ -126,7 +126,7 @@ def _update(ctx, git_repo):

def init(ctx, git_repo):
cl = ["git", "init", str(git_repo.directory)]
st = ctx.execute(cl, environment = ctx.os.environ)
st = ctx.execute(_strip_git_vars(cl), environment = ctx.os.environ)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove those env vars from ctx.os.environ instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you can only force variables to new values (e.g. the empty value), but can't remove them. Or maybe that works if they are set to None? If not, that would probably be a reasonable feature request :⁠-⁠)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fmeum is correct. This was my first thought, but setting to the empty string upsets Git, and setting to None upsets Bazel. And yes, being able to unset a variable with None would be a great FR!

Also, I would have liked to have been able to unset a variable with --repo_env.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem (as indicated by the test results) is that this doesn't work on Windows... We might need to implement that FR first :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw the Windows failures. I'll dig into that and see if this can reasonably be solved within this repo rule.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I sent #24221, so it may be more efficient to wait for that.

Copy link
Contributor Author

@rpwoodbu rpwoodbu Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that! This is definitely much cleaner. However, I still need a solution for Bazel 7. So I will continue to get this working in Windows.

Perhaps this PR (once I fix Windows) could go in and be cherry-picked into the 7 branch, then a follow-up could replace the solution based on your PR? I'm not sure what the process is for point releases.

Or maybe it makes more sense to bring your PR into Bazel 7?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if there is ever a 7.5 release, we could just cherry-pick both commits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have rebased and changed my PR to use your new feature. I haven't been able to get my Windows build of Bazel to pass that failing test even at HEAD, so I'm going to lean on the merge checks here. 🤞

return [
"bash",
"-c",
"unset $(git rev-parse --local-env-vars); " + " ".join(args),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it clear that unsetting all GIT_ vars is correct? What about something like GIT_SSH or GIT_SSL_NO_VERIFY?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ git rev-parse --local-env-vars
GIT_ALTERNATE_OBJECT_DIRECTORIES
GIT_CONFIG
GIT_CONFIG_PARAMETERS
GIT_CONFIG_COUNT
GIT_OBJECT_DIRECTORY
GIT_DIR
GIT_WORK_TREE
GIT_IMPLICIT_WORK_TREE
GIT_GRAFT_FILE
GIT_INDEX_FILE
GIT_NO_REPLACE_OBJECTS
GIT_REPLACE_REF_BASE
GIT_PREFIX
GIT_INTERNAL_SUPER_PREFIX
GIT_SHALLOW_FILE
GIT_COMMON_DIR

So this doesn't snarf GIT_SSH or GIT_SSL_NO_VERIFY. I'll dig info the Windows issue.

@iancha1992 iancha1992 added the team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. label Nov 6, 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.
@rpwoodbu rpwoodbu force-pushed the rwoodbury-git-repository branch from 5d10ac1 to c541924 Compare November 11, 2024 07:43
Copy link
Member

@meteorcloudy meteorcloudy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@meteorcloudy meteorcloudy added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Nov 11, 2024
@fmeum
Copy link
Collaborator

fmeum commented Nov 11, 2024

@bazel-io fork 8.0.0

@github-actions github-actions bot removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Nov 12, 2024
bazel-io pushed a commit to bazel-io/bazel that referenced this pull request 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 pull request 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]>
ramil-bitrise pushed a commit to bitrise-io/bazel that referenced this pull request 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
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

git_repository() is sensitive to GIT_DIR
5 participants