Skip to content

Commit

Permalink
Sluongng/revert patch (#1277)
Browse files Browse the repository at this point in the history
* Revert "Use `patch` from `@bazel_tools//tools/build_defs/repo:utils.bzl` (#1269)"

This reverts commit 1dc3aaa.

It seems like by using the native repository_ctx.path(file, strip)
in bazel_tools, we would be applying the patches to the repository
archive _before_ BUILD files generation.  This is not desirable as
patches on top of go_repository is often used to modify the BUILD files
themselves.

Another differences is that `patch` allow patch files without trailing
blank/empty lines while repository_ctx.patch() would fail to recognize
EOF in patch files without a trailing empty line:

  Expecting more chunk line at line xx

Let's revert this change until we find a more appropriate solution to
simplify go_repository structure.

* go_repository: add doc regarding patch usages

Highlight the reason why we had to revert a recent PR.
Also applied minor formatting changes.
  • Loading branch information
sluongng authored Jun 13, 2022
1 parent 55d692e commit 32dab97
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
39 changes: 32 additions & 7 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

load("//internal:common.bzl", "env_execute", "executable_extension")
load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch", "read_netrc", "use_netrc")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc")

_DOC = """
`go_repository` downloads a Go project and generates build files with Gazelle
Expand Down Expand Up @@ -103,7 +103,6 @@ Authorization: Bearer RANDOM-TOKEN
</pre>
"""


# We can't disable timeouts on Bazel, but we can set them to large values.
_GO_REPOSITORY_TIMEOUT = 86400

Expand Down Expand Up @@ -414,7 +413,7 @@ go_repository = repository_rule(
`replace` will be downloaded at `version` and verified with `sum`.
NOTE: There is no `go_repository` equivalent to file path `replace`
directives. Use `local_repository` instead."""
directives. Use `local_repository` instead.""",
),

# Attributes for a repository that needs automatic build file generation
Expand Down Expand Up @@ -518,9 +517,8 @@ go_repository = repository_rule(
doc = "A list of patches to apply to the repository after gazelle runs.",
),
"patch_tool": attr.string(
default = "",
doc = """The patch tool used to apply `patches`. If this is specified, Bazel will
use the specifed patch tool instead of the Bazel-native patch implementation.""",
default = "patch",
doc = "The patch tool used to apply `patches`.",
),
"patch_args": attr.string_list(
default = ["-p0"],
Expand All @@ -538,7 +536,34 @@ go_repository = repository_rule(
so this defaults to `False`. However, setting to `True` can be useful for debugging build failures and
unexpected behavior for the given rule.
""",
)
),
},
)
"""See repository.md#go-repository for full documentation."""

# Copied from @bazel_tools//tools/build_defs/repo:utils.bzl
#
# Here we shell out to `patch` executable instead of calling repository_ctx.patch()
# so that we can apply the patch files _AFTER_ gazelle ran and generated the
# BUILD files. This enables patch files to modify the generated BUILD files.
def patch(ctx):
"""Implementation of patching an already extracted repository"""
bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash"
for patchfile in ctx.attr.patches:
command = "{patchtool} {patch_args} < {patchfile}".format(
patchtool = ctx.attr.patch_tool,
patchfile = ctx.path(patchfile),
patch_args = " ".join([
"'%s'" % arg
for arg in ctx.attr.patch_args
]),
)
st = ctx.execute([bash_exe, "-c", command])
if st.return_code:
fail("Error applying patch %s:\n%s%s" %
(str(patchfile), st.stderr, st.stdout))
for cmd in ctx.attr.patch_cmds:
st = ctx.execute([bash_exe, "-c", cmd])
if st.return_code:
fail("Error applying patch command %s:\n%s%s" %
(cmd, st.stdout, st.stderr))
2 changes: 1 addition & 1 deletion repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ go_repository(
| <a id="go_repository-importpath"></a>importpath | The Go import path that matches the root directory of this repository.<br><br> In module mode (when <code>version</code> is set), this must be the module path. If neither <code>urls</code> nor <code>remote</code> is specified, <code>go_repository</code> will automatically find the true path of the module, applying import path redirection.<br><br> If build files are generated for this repository, libraries will have their <code>importpath</code> attributes prefixed with this <code>importpath</code> string. | String | required | |
| <a id="go_repository-patch_args"></a>patch_args | Arguments passed to the patch tool when applying patches. | List of strings | optional | ["-p0"] |
| <a id="go_repository-patch_cmds"></a>patch_cmds | Commands to run in the repository after patches are applied. | List of strings | optional | [] |
| <a id="go_repository-patch_tool"></a>patch_tool | The patch tool used to apply <code>patches</code>. If this is specified, Bazel will use the specifed patch tool instead of the Bazel-native patch implementation. | String | optional | "" |
| <a id="go_repository-patch_tool"></a>patch_tool | The patch tool used to apply <code>patches</code>. | String | optional | "patch" |
| <a id="go_repository-patches"></a>patches | A list of patches to apply to the repository after gazelle runs. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="go_repository-remote"></a>remote | The VCS location where the repository should be downloaded from. This is usually inferred from <code>importpath</code>, but you can set <code>remote</code> to download from a private repository or a fork. | String | optional | "" |
| <a id="go_repository-replace"></a>replace | A replacement for the module named by <code>importpath</code>. The module named by <code>replace</code> will be downloaded at <code>version</code> and verified with <code>sum</code>.<br><br> NOTE: There is no <code>go_repository</code> equivalent to file path <code>replace</code> directives. Use <code>local_repository</code> instead. | String | optional | "" |
Expand Down

0 comments on commit 32dab97

Please sign in to comment.