Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: bazel-contrib/bazel-gazelle
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.36.0
Choose a base ref
...
head repository: bazel-contrib/bazel-gazelle
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.37.0
Choose a head ref

Commits on Apr 9, 2024

  1. Apply map_kind to args as well as rule kinds (#1722)

    * Apply map_kind to args as well as rule kinds
    
    #1310 added support for
    adding args from Gazelle, so you can express things like:
    
    ```
    maybe(
        java_test,
        ...
    )
    ```
    
    But `map_kind` doesn't currently get applied to these args.
    
    This PR adds that application.
    
    * Panic -> Error
    
    * Sort imports in testdata
    illicitonion authored Apr 9, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1e0d875 View commit details

Commits on Apr 11, 2024

  1. Add a pointer to bzlmod guide (#1781)

    sluongng authored Apr 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    65b433f View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    26bb2ac View commit details

Commits on Apr 12, 2024

  1. [cmd/fetch_repo] make cache corruption failures more clear (#1782)

    This failure was confusing to our users. We should make more clear error messages in cases when a `GOMODCACHE` might be corrupt.
    
    Resolves #1774
    
    Example failure:
    ```
    ERROR: /home/user/go-code/WORKSPACE:69:26: fetching go_repository rule //external:com_github_bazelbuild_remote_apis: Traceback (most recent call last):
            File "/home/user/.cache/bazel/_bazel_tfrench/b97476d719d716accead0f2d5b93104f/external/bazel_gazelle/internal/go_repository.bzl", line 263, column 17, in _go_repository_impl
                    fail("failed to fetch %s: %s" % (ctx.name, result.stderr))
    Error in fail: failed to fetch com_github_bazelbuild_remote_apis: fetch_repo: resulting module with sum h1:xhO5v3Ac039HesaPqoVUwOy4WhRRJbmo9om245kp+AY=; expected sum h1:xhO5v3Ac039HesaPqoVUwOy4WhRRJbmo9om245kp+AY=, Please try clearing your module cache directory "/home/user/go-code/pkg/mod"
    ERROR: Error computing the main repository mapping: no such package '@@com_github_bazelbuild_remote_apis//': failed to fetch com_github_bazelbuild_remote_apis: fetch_repo: resulting module with sum h1:xhO5v3Ac039HesaPqoVUwOy4WhRRJbmo9om245kp+AY=; expected sum h1:xhO5v3Ac039HesaPqoVUwOy4WhRRJbmo9om245kp+AY=, Please try clearing your module cache directory "/home/user/go-code/pkg/mod"
    INFO: Build Event Protocol files produced successfully.
    Computing main repo mapping: 
    WARNING:Bazel failed with exit code 1
    ```
    tyler-french authored Apr 12, 2024
    Copy the full SHA
    cc40a33 View commit details

Commits on Apr 13, 2024

  1. Nit: pass -modcacherw in exec.Command. (#1784)

    In previous change it was passed conditionally so it was separate, now it's always added.
    hauserx authored Apr 13, 2024
    Copy the full SHA
    942129d View commit details

Commits on Apr 16, 2024

  1. Mention JS extension in Aspect CLI (#1646)

    * Update README.rst
    
    * Update README.rst
    alexeagle authored Apr 16, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    42b7781 View commit details

Commits on Apr 18, 2024

  1. [Feature] bzlmod & go.work (#1731)

    * adds go_deps.from_file(go_work = "//:go.work")
    * adds ability to op-out of version conflict failures go_deps.from_file(go_work = "//:go.work, fail_on_version_conflict = False)
    * parses go.work files to discover used go modules and replace statements
    * generates repos for all deps specified in go.mod's referenced by the //:go.work
    * handle replace statements (both at the go.work and the go.mod level)
    * Errors with actionable error message if duplicate dependencies with differing versions arise (Question: can we do better?)
    * expand BCR test coverage to include both go.work and go.mod scenarios
    
    Caveats:
    * allows for dependency bleed between different go modules in the workspace - this is sorta by design for this stepping stone, but is ideal
    * doesn't support replace statements in go.work yet (but that shouldn't be to hard to add in a follow up)
    stefanpenner authored Apr 18, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    908ba74 View commit details

Commits on Apr 25, 2024

  1. Add GIT_CONFIG_* env vars to go_repository allow-list (#1791)

    * Add GIT_CONFIG_* env vars to go_repository allow-list
    
    * Add '# keep sorted' comment
    mortenmj authored Apr 25, 2024
    Copy the full SHA
    a0f2bb6 View commit details
  2. Reformat with latest buildifier (#1792)

    fmeum authored Apr 25, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c26571e View commit details

Commits on May 2, 2024

  1. [Feature] go.mod & go.work FilePath ReplaceDirective support (#1776)

    stefanpenner authored May 2, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    918a4c4 View commit details

Commits on May 4, 2024

  1. Update README.rst (#1795)

    AugustKarlstedt authored May 4, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    9e8ba4f View commit details

Commits on May 5, 2024

  1. Update README.rst (#1796)

    AugustKarlstedt authored May 5, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7c84c33 View commit details

Commits on May 8, 2024

  1. Normalise newlines on Windows (#1798)

    Otherwise assorted things go wrong when running tests using this
    infrastructure on Windows where \r\n line endings confuse things.
    illicitonion authored May 8, 2024
    Copy the full SHA
    ba2ce36 View commit details

Commits on May 11, 2024

  1. Fix go.work use ROOT moddir (#1800)

    hunshcn authored May 11, 2024
    Copy the full SHA
    8643a9d View commit details

Commits on May 12, 2024

  1. allow go_visibility directive to change command package's visibility (#…

    …1794)
    
    Co-authored-by: Zhongpeng Lin <zplin@uber.com>
    hunshcn and linzhp authored May 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1541133 View commit details

Commits on May 16, 2024

  1. Ensure the Gazelle binary is built for the right platform (#1803)

    Right now we pick Gazelle for the "exec" platform. This isn't correct,
    because we are not running Gazelle as part of a build action. Instead,
    we are emitting a shell script that *invokes* Gazelle. That's why we
    need to make sure it's built for the target platform.
    
    This addresses an issue where Bazel ends up invoking a copy of Gazelle
    for Linux on my Mac, if I invoke bazel with
    --extra_execution_platforms=//my_linux_platform,//my_mac_platform.
    EdSchouten authored May 16, 2024
    Copy the full SHA
    f93e45f View commit details

Commits on May 19, 2024

  1. Add support for debug_mode option to go_deps (#1806)

    davidbyttow authored May 19, 2024
    Copy the full SHA
    47e9c2a View commit details

Commits on May 20, 2024

  1. Copy the full SHA
    61b277b View commit details
  2. Copy the full SHA
    f43d5d0 View commit details
  3. Make # gazelle:proto file work without needing to set different `op…

    …tion go_package` in .proto files (#1765)
    
    Co-authored-by: Shahms King <shahms@google.com>
    Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
    Co-authored-by: Zhongpeng Lin <zplin@uber.com>
    4 people authored May 20, 2024
    Copy the full SHA
    6a27127 View commit details

Commits on May 21, 2024

  1. Copy the full SHA
    75eade4 View commit details

Commits on May 24, 2024

  1. Copy the full SHA
    4d0129d View commit details
Showing with 2,734 additions and 458 deletions.
  1. +6 −2 .bazelci/presubmit.yml
  2. +1 −1 .bcr/presubmit.yml
  3. +5 −5 .gitignore
  4. +1 −1 MODULE.bazel
  5. +12 −6 README.rst
  6. +6 −0 cmd/fetch_repo/BUILD.bazel
  7. +59 −0 cmd/fetch_repo/copy_tree.go
  8. +29 −4 cmd/fetch_repo/fetch_repo.go
  9. +70 −0 cmd/fetch_repo/go_mod_download.go
  10. +9 −106 cmd/fetch_repo/module.go
  11. +37 −0 cmd/fetch_repo/path.go
  12. +1 −0 cmd/gazelle/BUILD.bazel
  13. +45 −12 cmd/gazelle/fix-update.go
  14. +247 −0 cmd/gazelle/fix_test.go
  15. +238 −0 cmd/gazelle/integration_test.go
  16. +7 −7 def.bzl
  17. +1 −1 docs/BUILD.bazel
  18. +3 −0 internal/bzlmod/BUILD.bazel
  19. +124 −11 internal/bzlmod/go_deps.bzl
  20. +235 −43 internal/bzlmod/go_mod.bzl
  21. +8 −8 internal/bzlmod/non_module_deps.bzl
  22. +7 −5 internal/bzlmod/semver.bzl
  23. +1 −1 internal/extend_docs.bzl
  24. +54 −17 internal/go_repository.bzl
  25. +3 −0 internal/go_repository_tools_srcs.bzl
  26. +162 −69 language/go/generate.go
  27. +7 −5 language/go/kinds.go
  28. +2 −2 language/go/package.go
  29. +1 −0 language/go/resolve.go
  30. +12 −40 language/go/resolve_test.go
  31. +1 −0 language/go/testdata/proto_file_mode/abc/BUILD.old
  32. +31 −0 language/go/testdata/proto_file_mode/abc/BUILD.want
  33. +4 −0 language/go/testdata/proto_file_mode/abc/a.proto
  34. +17 −0 language/go/testdata/proto_file_mode/abc/b.proto
  35. +1 −0 language/go/testdata/proto_file_mode/multiple_go_packages/BUILD.old
  36. +67 −0 language/go/testdata/proto_file_mode/multiple_go_packages/BUILD.want
  37. +4 −0 language/go/testdata/proto_file_mode/multiple_go_packages/bar.proto
  38. +4 −0 language/go/testdata/proto_file_mode/multiple_go_packages/bar2.proto
  39. +3 −0 language/go/testdata/proto_file_mode/multiple_go_packages/baz.proto
  40. +4 −0 language/go/testdata/proto_file_mode/multiple_go_packages/foo.proto
  41. +1 −0 language/go/testdata/proto_file_mode/multiple_go_packages/gofile.go
  42. +1 −0 language/go/testdata/proto_file_mode/xyz/BUILD.old
  43. +17 −0 language/go/testdata/proto_file_mode/xyz/BUILD.want
  44. +3 −0 language/go/testdata/proto_file_mode/xyz/x.proto
  45. +0 −25 language/proto/resolve.go
  46. +0 −7 language/proto/resolve_test.go
  47. +4 −3 repository.md
  48. +10 −0 rule/rule.go
  49. +8 −0 rule/rule_test.go
  50. +0 −1 tests/BUILD.bazel
  51. +0 −54 tests/bcr/go.sum
  52. 0 tests/bcr/{ → go_mod}/.bazelignore
  53. 0 tests/bcr/{ → go_mod}/.bazelrc
  54. 0 tests/bcr/{ → go_mod}/.bazelversion
  55. 0 tests/bcr/{ → go_mod}/BUILD.bazel
  56. +4 −2 tests/bcr/{ → go_mod}/MODULE.bazel
  57. 0 tests/bcr/{ → go_mod}/WORKSPACE
  58. 0 tests/bcr/{ → go_mod}/WORKSPACE.bzlmod
  59. +13 −3 tests/bcr/{ → go_mod}/go.mod
  60. +128 −0 tests/bcr/go_mod/go.sum
  61. 0 tests/bcr/{ → go_mod}/patches/BUILD.bazel
  62. 0 tests/bcr/{ → go_mod}/patches/buildtools.patch
  63. 0 tests/bcr/{ → go_mod}/patches/testify.patch
  64. +2 −1 tests/bcr/{ → go_mod}/pkg/BUILD.bazel
  65. +1 −1 tests/bcr/{ → go_mod}/pkg/data/BUILD.bazel
  66. 0 tests/bcr/{ → go_mod}/pkg/data/data.go
  67. +8 −1 tests/bcr/{ → go_mod}/pkg/pkg_test.go
  68. 0 tests/bcr/{ → go_mod}/pkg/platform_lib_unix.go
  69. 0 tests/bcr/{ → go_mod}/pkg/platform_lib_windows.go
  70. tests/bcr/{ → go_mod}/proto/BUILD.bazel
  71. 0 tests/bcr/{ → go_mod}/proto/foo.proto
  72. 0 tests/bcr/{ → go_mod}/proto/foo_test.go
  73. 0 tests/bcr/{ → go_mod}/test_dep/BUILD.bazel
  74. +1 −2 tests/bcr/{ → go_mod}/test_dep/MODULE.bazel
  75. 0 tests/bcr/{ → go_mod}/test_dep/WORKSPACE
  76. 0 tests/bcr/{ → go_mod}/test_dep/patches/BUILD.bazel
  77. 0 tests/bcr/{ → go_mod}/test_dep/patches/testify.patch
  78. 0 tests/bcr/{ → go_mod}/test_dep/test.go
  79. 0 tests/bcr/{ → go_mod}/tools/BUILD.bazel
  80. +1 −1 tests/bcr/{ → go_mod}/tools/go.mod
  81. 0 tests/bcr/{ → go_mod}/tools/go.sum
  82. 0 tests/bcr/{ → go_mod}/tools/tools.go
  83. +1 −0 tests/bcr/go_work/.bazelignore
  84. +6 −0 tests/bcr/go_work/.bazelrc
  85. +1 −0 tests/bcr/go_work/.bazelversion
  86. +5 −0 tests/bcr/go_work/BUILD.bazel
  87. +151 −0 tests/bcr/go_work/MODULE.bazel
  88. 0 tests/bcr/go_work/WORKSPACE
  89. 0 tests/bcr/go_work/WORKSPACE.bzlmod
  90. +3 −0 tests/bcr/go_work/go.mod
  91. +10 −0 tests/bcr/go_work/go.work
  92. 0 tests/bcr/go_work/patches/BUILD.bazel
  93. +13 −0 tests/bcr/go_work/patches/buildtools.patch
  94. +13 −0 tests/bcr/go_work/patches/testify.patch
  95. +66 −0 tests/bcr/go_work/pkg/BUILD.bazel
  96. +9 −0 tests/bcr/go_work/pkg/data/BUILD.bazel
  97. +3 −0 tests/bcr/go_work/pkg/data/data.go
  98. +33 −0 tests/bcr/go_work/pkg/go.mod
  99. +126 −0 tests/bcr/go_work/pkg/go.sum
  100. +86 −0 tests/bcr/go_work/pkg/pkg_test.go
  101. +10 −0 tests/bcr/go_work/pkg/platform_lib_unix.go
  102. +7 −0 tests/bcr/go_work/pkg/platform_lib_windows.go
  103. +31 −0 tests/bcr/go_work/proto/BUILD.bazel
  104. +12 −0 tests/bcr/go_work/proto/foo.proto
  105. +23 −0 tests/bcr/go_work/proto/foo_test.go
  106. +2 −0 tests/bcr/go_work/proto/go.mod
  107. +8 −0 tests/bcr/go_work/test_dep/BUILD.bazel
  108. +49 −0 tests/bcr/go_work/test_dep/MODULE.bazel
  109. 0 tests/bcr/go_work/test_dep/WORKSPACE
  110. +11 −0 tests/bcr/go_work/test_dep/go.mod
  111. +10 −0 tests/bcr/go_work/test_dep/go.sum
  112. 0 tests/bcr/go_work/test_dep/patches/BUILD.bazel
  113. +13 −0 tests/bcr/go_work/test_dep/patches/testify.patch
  114. +13 −0 tests/bcr/go_work/test_dep/test.go
  115. +10 −0 tests/bcr/go_work/tools/BUILD.bazel
  116. +26 −0 tests/bcr/go_work/tools/go.mod
  117. +134 −0 tests/bcr/go_work/tools/go.sum
  118. +12 −0 tests/bcr/go_work/tools/tools.go
  119. +61 −3 tests/bzlmod/go_mod_test.bzl
  120. +1 −1 tests/bzlmod/utils_test.bzl
  121. +1 −1 tests/cli/env_variables_test.bzl
  122. +10 −0 tests/fixtures/hello/go.mod
  123. +6 −0 tests/fixtures/hello/go.sum
  124. +7 −0 tests/fixtures/hello/hello.go
  125. +10 −0 tests/fixtures/hello/hello_test.go
  126. +9 −5 testtools/files.go
8 changes: 6 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
@@ -4,6 +4,9 @@ matrix:
- ubuntu2204
- macos
bazel: [6.x, 7.x]
bcr_variant:
- tests/bcr/go_work
- tests/bcr/go_mod
tasks:
ubuntu2204:
name: Ubuntu 22.04 with WORKSPACE
@@ -39,11 +42,12 @@ tasks:
name: BCR test module
platform: ${{ platform }}
bazel: ${{ bazel }}
working_directory: tests/bcr
working_directory: ${{ bcr_variant }}
shell_commands:
# Regenerate the BUILD files for the test module using Gazelle.
# Also verify -repo_config are generated correctly in gazelle.bash
- rm pkg/BUILD.bazel proto/BUILD.bazel
- touch pkg/BUILD.bazel proto/BUILD.bazel
- bazel run //:gazelle -- update pkg proto
- bazel run //:gazelle -- pkg proto
build_targets:
@@ -59,7 +63,7 @@ tasks:
name: BCR test on Windows
platform: windows
bazel: ${{ bazel }}
working_directory: tests/bcr
working_directory: ${{ bcr_variant }}
shell_commands:
# Regenerate the BUILD file for the test module using Gazelle.
# Also verify -repo_config are generated correctly in gazelle.bash
2 changes: 1 addition & 1 deletion .bcr/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bcr_test_module:
module_path: tests/bcr
module_path: tests/bcr/go_mod
matrix:
platform:
- debian10
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@
/bazel-bin
/bazel-out
/bazel-testlogs
/tests/bcr/bazel-bcr
/tests/bcr/bazel-bin
/tests/bcr/bazel-out
/tests/bcr/bazel-testlogs
/tests/bcr/MODULE.bazel.lock
/tests/bcr/*/bazel-go_*
/tests/bcr/*/bazel-bin
/tests/bcr/*/bazel-out
/tests/bcr/*/bazel-testlogs
/tests/bcr/*/MODULE.bazel.lock
MODULE.bazel.lock
.DS_STORE
.ijwb/
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module(
name = "gazelle",
version = "0.36.0",
version = "0.37.0",
repo_name = "bazel_gazelle",
)

18 changes: 12 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ Gazelle build file generator
.. All external links are here
.. _a gazelle extension: https://github.com/bazel-contrib/rules_jvm/tree/main/java/gazelle
.. _Kotlin Support: https://github.com/aspect-build/aspect-cli/blob/main/gazelle/kotlin/
.. _JavaScript and TypeScript Support: https://github.com/aspect-build/aspect-cli/blob/main/gazelle/js/
.. _Architecture of Gazelle: Design.rst
.. _Repository rules: repository.md
.. _go_repository: repository.md#go_repository
@@ -43,6 +44,7 @@ Gazelle build file generator
.. role:: cmd(code)
.. role:: flag(code)
.. role:: direc(code)
.. role:: kbd
.. role:: param(kbd)
.. role:: type(emphasis)
.. role:: value(code)
@@ -104,6 +106,8 @@ Gazelle can generate Bazel BUILD files for many languages:
generating ``java_library``, ``java_binary``, ``java_test``, and ``java_test_suite`` rules.

* JavaScript / TypeScript

Aspect provides `JavaScript and TypeScript Support`_ in aspect-cli (also usable separately).

BenchSci's `rules_nodejs_gazelle`_ supports generating `ts_project`, `js_library`, `jest_test`,
and `web_asset` rules, and is able to support module bundlers like Webpack and Next.js
@@ -112,8 +116,7 @@ Gazelle can generate Bazel BUILD files for many languages:
* Kotlin

Aspect Build provides some `Kotlin Support`_ in the repo of their aspect-cli (also usable separately).
Still under development, please check the README for
currently available features.
Still under development, please check the README for currently available features.

* Protocol Buffers

@@ -135,7 +138,7 @@ Gazelle can generate Bazel BUILD files for many languages:

* Starlark

`bazel-skylib`_ has an extension for generating ``bzl_library`` rules. See `bazel_skylib//gazelle/bzl`_.
`bazel-skylib`_ has an extension for generating ``bzl_library`` rules. See `bazel_skylib/gazelle/bzl`_.

* Swift

@@ -584,7 +587,7 @@ The following flags are accepted:
| :flag:`-cpuprofile filename` | :value:`""` |
+-------------------------------------------------------------------+----------------------------------------+
| If specified, gazelle uses [runtime/pprof](https://pkg.go.dev/runtime/pprof#StartCPUProfile) to collect |
| CPU profiling information from the command and save it to the given file. |
| CPU profiling information from the command and save it to the given file. |
| |
| By default, this is disabled |
+-------------------------------------------------------------------+----------------------------------------+
@@ -607,6 +610,9 @@ used to add new repository rules or update existing rules to the specified
version. It can also import repository rules from a ``go.mod`` or a ``go.work``
file.

WARNING: This command is mainly used for managing external Go dependencies in Bazel's WORKSPACE mode.
For managing external Go dependencies in Bazel's BzlMod mode, please check: https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/bzlmod.md#external-dependencies

.. code:: bash
# Add or update a repository to latest version by import path
@@ -636,7 +642,7 @@ The following flags are accepted:
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| Import repositories from a file as `go_repository`_ rules. These rules will be added to the bottom of the WORKSPACE file or merged with existing rules. |
| |
| The lock file format is inferred from the file name. ``go.mod`` and ``go.work` are all supported. |
| The lock file format is inferred from the file name. ``go.mod`` and ``go.work`` are all supported. |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
| :flag:`-repo_root dir` | |
+----------------------------------------------------------------------------------------------------------+----------------------------------------------+
@@ -1060,7 +1066,7 @@ parts of build files from being modified. ``# keep`` may be written before
a rule, before an attribute, or after a string within a list.

``# keep`` comments might take one of 2 forms; the ``# keep`` literal or a
description prefixed by ``# keep: ``.
description prefixed by ``# keep:``.

Example
^^^^^^^
6 changes: 6 additions & 0 deletions cmd/fetch_repo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -3,8 +3,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
go_library(
name = "fetch_repo_lib",
srcs = [
"copy_tree.go",
"fetch_repo.go",
"go_mod_download.go",
"module.go",
"path.go",
"vcs.go",
],
importpath = "github.com/bazelbuild/bazel-gazelle/cmd/fetch_repo",
@@ -33,9 +36,12 @@ filegroup(
testonly = True,
srcs = [
"BUILD.bazel",
"copy_tree.go",
"fetch_repo.go",
"fetch_repo_test.go",
"go_mod_download.go",
"module.go",
"path.go",
"vcs.go",
],
visibility = ["//visibility:public"],
59 changes: 59 additions & 0 deletions cmd/fetch_repo/copy_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright 2019 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"io"
"os"
"path/filepath"
)

func copyTree(destRoot, srcRoot string) error {
return filepath.Walk(srcRoot, func(src string, info os.FileInfo, e error) (err error) {
if e != nil {
return e
}
rel, err := filepath.Rel(srcRoot, src)
if err != nil {
return err
}
if rel == "." {
return nil
}
dest := filepath.Join(destRoot, rel)

if info.IsDir() {
return os.Mkdir(dest, 0o777)
}

r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(dest)
if err != nil {
return err
}
defer func() {
if cerr := w.Close(); err == nil && cerr != nil {
err = cerr
}
}()
_, err = io.Copy(w, r)
return err
})
}
33 changes: 29 additions & 4 deletions cmd/fetch_repo/fetch_repo.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@@ -34,6 +34,7 @@ import (
var (
// Common flags
importpath = flag.String("importpath", "", "Go importpath to the repository fetch")
path = flag.String("path", "", "absolute or relative path to a local go module")
dest = flag.String("dest", "", "destination directory")

// Repository flags
@@ -54,17 +55,41 @@ func main() {
log.SetPrefix("fetch_repo: ")

flag.Parse()
if *importpath == "" {
log.Fatal("-importpath must be set")

if *importpath == "" && *path == "" {
log.Fatal("-importpath or -path must be set")
}

if *dest == "" {
log.Fatal("-dest must be set")
}
if flag.NArg() != 0 {
log.Fatal("fetch_repo does not accept positional arguments")
}

if *version != "" {
if *path != "" {
if *importpath != "" {
log.Fatal("-importpath must not be set")
}
if *remote != "" {
log.Fatal("-remote must not be set in module path mode")
}
if *cmd != "" {
log.Fatal("-vcs must not be set in module path mode")
}
if *rev != "" {
log.Fatal("-rev must not be set in module path mode")
}
if *version != "" {
log.Fatal("-version must not be set in module path mode")
}
if *sum != "" {
log.Fatal("-sum must not be set in module path mode")
}
if err := moduleFromPath(*path, *dest); err != nil {
log.Fatal(err)
}
} else if *version != "" {
if *remote != "" {
log.Fatal("-remote must not be set in module mode")
}
70 changes: 70 additions & 0 deletions cmd/fetch_repo/go_mod_download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

type GoModDownloadResult struct {
Dir string
Sum string
Error string
}

func findGoPath() string {
// Locate the go binary. If GOROOT is set, we'll use that one; otherwise,
// we'll use PATH.
goPath := "go"
if runtime.GOOS == "windows" {
goPath += ".exe"
}
if goroot, ok := os.LookupEnv("GOROOT"); ok {
goPath = filepath.Join(goroot, "bin", goPath)
}
return goPath
}

func runGoModDownload(dl *GoModDownloadResult, dest string, importpath string, version string) error {
buf := bytes.NewBuffer(nil)
bufErr := bytes.NewBuffer(nil)
cmd := exec.Command(findGoPath(), "mod", "download", "-json", "-modcacherw")
cmd.Dir = dest

if version != "" && importpath != "" {
cmd.Args = append(cmd.Args, importpath+"@"+version)
}

cmd.Stdout = buf
cmd.Stderr = bufErr
dlErr := cmd.Run()
if dlErr != nil {
if _, ok := dlErr.(*exec.ExitError); !ok {
if bufErr.Len() > 0 {
return fmt.Errorf("go mod download exec error: %s %q: %s, %w", cmd.Path, strings.Join(cmd.Args, " "), bufErr.String(), dlErr)
}
return fmt.Errorf("go mod download exec error: %s %s: %v", cmd.Path, strings.Join(cmd.Args, " "), dlErr)
}
}

// Parse the JSON output.
if err := json.Unmarshal(buf.Bytes(), &dl); err != nil {
if bufErr.Len() > 0 {
return fmt.Errorf("go mod download output format: `%s %s`: parsing JSON: %q stderr: %q error: %w", cmd.Path, strings.Join(cmd.Args, " "), buf.String(), bufErr.String(), err)
}
return fmt.Errorf("go mod download output format: `%s %s`: parsing JSON: %q error: %w", cmd.Path, strings.Join(cmd.Args, " "), buf.String(), err)
}
if dl.Error != "" {
return errors.Join(errors.New(dl.Error), dlErr)
}
if dlErr != nil {
return dlErr
}
return nil
}
Loading