Skip to content

Commit 712ddee

Browse files
jayconrodfmeum
andauthored
Rewrite examples and new user documentation (#4251)
**What type of PR is this?** > Documentation **What does this PR do? Why is it needed?** - Added examples/hello and examples/basic_gazelle. The first is a minimal example, the second uses Gazelle. Both use modules. - Removed examples/basic-gazelle. It served as an example for a tutorial that's been replaced upstream. It used WORKSPACE, so it needed an update. I replaced it with a smaller example, since it pulled in a lot of external dependencies. - Updated .bazelci/presubmit.yml to build examples on all platforms. I wonder if we want to use bazel_integration_test or go_bazel_test for this though. - Several updates to README.rst: - Removed Announcements section since it's a bit cluttered and doesn't mention recent releases. - Fixed formatting on gomock link. - Removed "The rules are in the beta stage of development" since they're actually pretty mature. - Added arm64 as a supported platform. It's being tested on macOS at least. - Rewrote the Setup section to link to the new user tutorial, the two examples, and the bzlmod/WORKSPACE docs. **Which issues(s) does this PR fix?** Fixes #3717 Fixes #3183 Fixes #1912 Fixes #1154 Updates #1643 **Other notes for review** --------- Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent 2279b49 commit 712ddee

33 files changed

+390
-553
lines changed

.bazelci/presubmit.yml

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,36 +176,65 @@ tasks:
176176
# TODO: Update stardoc for consistent line endings.
177177
- "-//docs:all"
178178
# The following configurations test a seperate WORKSPACE under the examples folder
179-
ubuntu2004_examples:
180-
name: Examples test on Ubuntu
179+
ubuntu_hello_example:
180+
name: Hello example on Ubuntu
181181
platform: ubuntu2404
182-
working_directory: examples/basic-gazelle
183-
build_flags:
184-
- "--config=incompatible"
185-
test_flags:
186-
- "--config=incompatible"
182+
working_directory: examples/hello
187183
build_targets:
188184
- "//..."
189185
test_targets:
190186
- "//..."
191-
macos_examples:
192-
name: Examples test on macOS
187+
macos_hello_example:
188+
name: Hello example on macOS
193189
platform: macos_arm64
194-
working_directory: examples/basic-gazelle
190+
working_directory: examples/hello
191+
build_targets:
192+
- "//..."
193+
test_targets:
194+
- "//..."
195+
windows_examples:
196+
name: Hello example on Windows
197+
platform: windows
198+
working_directory: examples/hello
195199
build_flags:
196-
- "--config=incompatible"
200+
# Go requires a C toolchain that accepts options and emits errors like
201+
# gcc or clang. The Go SDK does not support MSVC.
202+
- '--action_env=PATH=C:\tools\msys64\usr\bin;C:\tools\msys64\bin;C:\tools\msys64\mingw64\bin;C:\python3\Scripts\;C:\python3;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\ProgramData\GooGet;C:\Program Files\Google\Compute Engine\metadata_scripts;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\Google\Compute Engine\sysprep;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\tools\msys64\usr\bin;c:\openjdk\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\CMake\bin;c:\ninja;c:\bazel;c:\buildkite'
203+
# NOTE(bazelbuild/bazel#10529): bazel doesn't register the mingw toolchain automatically.
204+
# We also need the host and target platforms to have the mingw constraint value.
205+
build_targets:
206+
- "//..."
207+
test_targets:
208+
- "//..."
197209
test_flags:
198-
- "--config=incompatible"
210+
- '--action_env=PATH=C:\tools\msys64\usr\bin;C:\tools\msys64\bin;C:\tools\msys64\mingw64\bin;C:\python3\Scripts\;C:\python3;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\ProgramData\GooGet;C:\Program Files\Google\Compute Engine\metadata_scripts;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\Google\Compute Engine\sysprep;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\tools\msys64\usr\bin;c:\openjdk\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\CMake\bin;c:\ninja;c:\bazel;c:\buildkite'
211+
# On Windows CI, bazel (bazelisk) needs %LocalAppData% to find the cache directory.
212+
# We invoke bazel in tests, so the tests need this, too.
213+
- "--test_env=LOCALAPPDATA"
214+
# go_bazel_test runs bazel in a test workspace. It needs the same flags as above.
215+
- "--test_env=GO_BAZEL_TEST_BAZELFLAGS=--cpu=x64_windows --compiler=mingw-gcc --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows_mingw --action_env=PATH --host_platform=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo --incompatible_enable_cc_toolchain_resolution"
216+
- "--test_env=PATH"
217+
ubuntu_basic_gazelle_example:
218+
name: Basic Gazelle example on Ubuntu
219+
platform: ubuntu2404
220+
working_directory: examples/basic_gazelle
199221
build_targets:
200222
- "//..."
201223
test_targets:
202224
- "//..."
203-
windows_examples:
204-
name: Examples test on Windows
225+
macos_basic_gazelle_example:
226+
name: Basic Gazelle example on macOS
227+
platform: macos_arm64
228+
working_directory: examples/basic_gazelle
229+
build_targets:
230+
- "//..."
231+
test_targets:
232+
- "//..."
233+
windows_basic_gazelle_example:
234+
name: Basic Gazelle example on Windows
205235
platform: windows
206-
working_directory: examples/basic-gazelle
236+
working_directory: examples/basic_gazelle
207237
build_flags:
208-
- "--config=incompatible"
209238
# Go requires a C toolchain that accepts options and emits errors like
210239
# gcc or clang. The Go SDK does not support MSVC.
211240
- '--action_env=PATH=C:\tools\msys64\usr\bin;C:\tools\msys64\bin;C:\tools\msys64\mingw64\bin;C:\python3\Scripts\;C:\python3;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\ProgramData\GooGet;C:\Program Files\Google\Compute Engine\metadata_scripts;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\Google\Compute Engine\sysprep;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\tools\msys64\usr\bin;c:\openjdk\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\CMake\bin;c:\ninja;c:\bazel;c:\buildkite'
@@ -216,7 +245,6 @@ tasks:
216245
test_targets:
217246
- "//..."
218247
test_flags:
219-
- "--config=incompatible"
220248
- '--action_env=PATH=C:\tools\msys64\usr\bin;C:\tools\msys64\bin;C:\tools\msys64\mingw64\bin;C:\python3\Scripts\;C:\python3;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\ProgramData\GooGet;C:\Program Files\Google\Compute Engine\metadata_scripts;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\Google\Compute Engine\sysprep;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\tools\msys64\usr\bin;c:\openjdk\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\CMake\bin;c:\ninja;c:\bazel;c:\buildkite'
221249
# On Windows CI, bazel (bazelisk) needs %LocalAppData% to find the cache directory.
222250
# We invoke bazel in tests, so the tests need this, too.

README.rst

Lines changed: 19 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Go rules for Bazel_
66
.. _Avoiding conflicts: proto/core.rst#avoiding-conflicts
77
.. _Bazel labels: https://docs.bazel.build/versions/master/build-ref.html#labels
88
.. _Bazel: https://bazel.build/
9+
.. _Bazel Tutorial\: Build a Go Project: https://bazel.build/start/go
910
.. _Build modes: go/modes.rst
1011
.. _Bzlmod: https://bazel.build/external/overview#bzlmod
1112
.. _Go with Bzlmod: docs/go/core/bzlmod.md
@@ -14,6 +15,8 @@ Go rules for Bazel_
1415
.. _Coverage: https://bazel.google.cn/docs/coverage
1516
.. _Dependencies: go/dependencies.rst
1617
.. _Deprecation schedule: https://github.com/bazelbuild/rules_go/wiki/Deprecation-schedule
18+
.. _examples/basic_gazelle: examples/basic_gazelle
19+
.. _examples/hello: examples/hello
1720
.. _Gopher Slack: https://invite.slack.golangbridge.org/
1821
.. _gopls integration: https://github.com/bazelbuild/rules_go/wiki/Editor-setup
1922
.. _Overriding dependencies: go/dependencies.rst#overriding-dependencies
@@ -36,6 +39,7 @@ Go rules for Bazel_
3639
.. Go rules
3740
.. _go_binary: docs/go/core/rules.md#go_binary
3841
.. _go_context: go/toolchains.rst#go_context
42+
.. _go_deps: https://github.com/bazel-contrib/bazel-gazelle/blob/master/extensions.md#go_deps
3943
.. _go_download_sdk: go/toolchains.rst#go_download_sdk
4044
.. _go_host_sdk: go/toolchains.rst#go_host_sdk
4145
.. _go_library: docs/go/core/rules.md#go_library
@@ -70,132 +74,6 @@ Mailing list: `bazel-go-discuss`_
7074

7175
Slack: `#go on Bazel Slack`_, `#bazel on Go Slack`_
7276

73-
Announcements
74-
-------------
75-
2024-07-19
76-
`v0.48.1 <https://github.com/bazelbuild/rules_go/releases/tag/v0.48.1>`_
77-
is now available. This includes a bug fix (https://github.com/bazelbuild/rules_go/pull/3961).
78-
79-
2024-05-21
80-
`v0.48.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.48.0>`_
81-
is now available. This release enables support for `--incompatible_enable_proto_toolchain_resolution`
82-
(https://github.com/bazelbuild/rules_go/pull/3919).
83-
84-
2024-05-06
85-
`v0.47.1 <https://github.com/bazelbuild/rules_go/releases/tag/v0.47.1>`_
86-
is now available. This release includes a bug fix (https://github.com/bazelbuild/rules_go/pull/3929).
87-
88-
2024-04-23
89-
`v0.47.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.47.0>`_
90-
is now available. This release includes bug fixes and enhancements.
91-
92-
2024-02-09
93-
`v0.46.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.46.0>`_
94-
is now available. This release includes a change to emit Nogo facts to a separate archive, and
95-
Bzlmod support for Nogo (https://github.com/bazelbuild/rules_go/pull/3842, https://github.com/bazelbuild/rules_go/pull/3789).
96-
97-
2024-01-19
98-
`v0.45.1 <https://github.com/bazelbuild/rules_go/releases/tag/v0.45.1>`_
99-
is now available. This release includes a bug fix (https://github.com/bazelbuild/rules_go/pull/3832).
100-
101-
2024-01-12
102-
`v0.45.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.45.0>`_
103-
is now available. This release includes bug fixes and enhancements.
104-
105-
2024-01-04
106-
Release
107-
`v0.44.2 <https://github.com/bazelbuild/rules_go/releases/tag/v0.44.2>`_
108-
is now available. This includes two bug fixes (https://github.com/bazelbuild/rules_go/pull/3808 and https://github.com/bazelbuild/rules_go/pull/3810) with
109-
the test timeout handler.
110-
111-
2023-12-29
112-
Release
113-
`v0.44.1 <https://github.com/bazelbuild/rules_go/releases/tag/v0.44.1>`_
114-
is now available. This release reverts a forced upgrade (https://github.com/bazelbuild/rules_go/pull/3803) of genproto from
115-
`v0.44.0`.
116-
117-
2023-12-15
118-
Release
119-
`v0.44.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.44.0>`_
120-
is now available. This release adds a stacktrace when a test times out and
121-
adds support for nogo in Bzlmod.
122-
123-
2023-11-20
124-
Release
125-
`v0.43.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.43.0>`_
126-
is now available. This release fixes a couple bugs and upgrades `x/tools`
127-
to the latest version.
128-
129-
2023-09-28
130-
Release
131-
`v0.42.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.42.0>`_
132-
is now available. This release fixes a couple bugs and adds support for
133-
patching the standard library.
134-
135-
2023-07-10
136-
Release
137-
`v0.41.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.41.0>`_
138-
is now available. rules_go no longer ship with `@go_googleapis` repo.
139-
It requires Gazelle v0.32.0 or later.
140-
141-
2023-06-28
142-
Release
143-
`v0.40.1 <https://github.com/bazelbuild/rules_go/releases/tag/v0.40.1>`_
144-
is now available. This release fixes a couple bugs in
145-
the go packages driver that were introduced in 0.40.0.
146-
147-
2023-06-22
148-
Release
149-
`v0.40.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.40.0>`_
150-
is now available. This release includes support for `//nolint` parsing,
151-
as well as a large set of additional changes. Please check the release notes
152-
for details.
153-
154-
2023-03-27
155-
Release
156-
`v0.39.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.39.0>`_
157-
is now available. This release includes a simpler interface for Bzlmod
158-
`go_sdk` registration, makes the `//go` tool available to users, and
159-
fixes various bugs.
160-
161-
2022-12-06
162-
Release
163-
`v0.37.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.37.0>`_
164-
is now available. This release includes support for generated code in
165-
the packages driver as well as various bugfixes.
166-
167-
2022-11-22
168-
Release
169-
`v0.36.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.36.0>`_
170-
is now available. This release adds a Go library to look up runfiles, and
171-
two command line flags to pass additional compiler and linker options.
172-
173-
2022-09-11
174-
Release
175-
`v0.35.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.35.0>`_
176-
is now available. This release supports switching Go SDK version from a
177-
build flag. Starting this release, rules_go requires Bazel 5.1.0, because
178-
it needs to read CPU architecture from Bazel to better support Apple M1
179-
with Rosetta translation.
180-
181-
2022-07-19
182-
Release
183-
`v0.34.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.34.0>`_
184-
is now available. This release brings very experimental support for bzlmod,
185-
several improvements to nogo and gopackagesdriver.
186-
187-
2022-06-06
188-
Release
189-
`v0.33.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.33.0>`_
190-
is now available. This release consists mostly of bug fixes and deprecates
191-
the `asm`, `compile`, `cover`, and `pack` actions on `go_context`.
192-
2022-05-11
193-
Release
194-
`v0.32.0 <https://github.com/bazelbuild/rules_go/releases/tag/v0.32.0>`_
195-
is now available. This adds `gomock` to rules_go and supports lcov format
196-
for code coverage report, as well as a long list of other changes listed
197-
in the release notes.
198-
19977
Contents
20078
--------
20179

@@ -236,6 +114,7 @@ Documentation
236114
* `go_context`_
237115

238116
* `Extra rules <docs/go/extras/extras.md>`_
117+
239118
* `gomock`_
240119

241120
* `nogo build-time static analysis`_
@@ -251,15 +130,16 @@ Quick links
251130
Overview
252131
--------
253132

254-
The rules are in the beta stage of development. They support:
133+
These rules support:
255134

256135
* Building libraries, binaries, and tests (`go_library`_, `go_binary`_,
257136
`go_test`_)
137+
* Go modules via `go_deps`_.
258138
* Vendoring
259139
* cgo
260140
* Cross-compilation
261141
* Generating BUILD files via gazelle_
262-
* Build-time code analysis via nogo_
142+
* Build-time static code analysis via nogo_
263143
* `Protocol buffers`_
264144
* Remote execution
265145
* `Coverage`_
@@ -273,7 +153,7 @@ They currently do not support or have limited support for:
273153
The Go rules are tested and supported on the following host platforms:
274154

275155
* Linux, macOS, Windows
276-
* amd64
156+
* amd64, arm64
277157

278158
Users have reported success on several other platforms, but the rules are
279159
only tested on those listed above.
@@ -286,9 +166,6 @@ The ``master`` branch is only guaranteed to work with the latest version of Baze
286166
Setup
287167
-----
288168

289-
System setup
290-
~~~~~~~~~~~~
291-
292169
To build Go code with Bazel, you will need:
293170

294171
* A recent version of Bazel.
@@ -301,15 +178,18 @@ You normally won't need a Go toolchain installed. Bazel will download one.
301178
See `Using rules_go on Windows`_ for Windows-specific setup instructions.
302179
Several additional tools need to be installed and configured.
303180

304-
Initial project setup
305-
~~~~~~~~~~~~~~~~~~~~~
181+
If you're new to Bazel, read `Bazel Tutorial: Build a Go Project`_, which
182+
introduces Bazel concepts and shows you how to set up a small Go workspace to
183+
be built with Bazel.
184+
185+
For a quicker "hello world" example, see `examples/hello`_.
186+
187+
For an example that generates build files and retrieves external dependencies
188+
using Gazelle, see `examples/basic_gazelle`_.
306189

307-
There are two ways to setup:
190+
For more detailed `Bzlmod`_ documentation, see `Go with Bzlmod`_.
308191

309-
* With Bazel's external dependency management system `Bzlmod`_,
310-
refer to the dedicated `Go with Bzlmod`_ guide.
311-
* With the older ``WORKSPACE`` dependency file, refer to the
312-
`Go with WORKSPACE`_ setup docs.
192+
For legacy ``WORKSPACE`` instructions, see `Go with WORKSPACE`_.
313193

314194
FAQ
315195
---

examples/.bazelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This helps configure rules_go CI to test the examples.
2+
# It's not part of the examples themselves.
3+
#
4+
# This file is separate from the main .bazelrc because bzlmod is disabled
5+
# there, but it is required to build the examples.
6+
7+
common:ci --enable_bzlmod
8+
common:ci --color=no
9+
common:ci --verbose_failures
10+
common:ci --sandbox_debug
11+
common:ci --spawn_strategy=standalone
12+
common:ci --genrule_strategy=standalone
13+
common:ci --test_strategy=standalone

examples/basic-gazelle/.bazelrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/basic-gazelle/.bazelversion

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/basic-gazelle/.gitignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/basic-gazelle/BUILD.bazel

Lines changed: 0 additions & 33 deletions
This file was deleted.

examples/basic-gazelle/CONTRIBUTING.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

examples/basic-gazelle/README.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)