@@ -9,21 +9,20 @@ Go rules for Bazel_
9
9
.. _Build modes : go/modes.rst
10
10
.. _Bzlmod : https://bazel.build/external/overview#bzlmod
11
11
.. _Go with Bzlmod : docs/go/core/bzlmod.md
12
+ .. _Go with WORKSPACE : docs/go/core/workspace.md
12
13
.. _Core rules : docs/go/core/rules.md
13
14
.. _Coverage : https://bazel.google.cn/docs/coverage
14
15
.. _Dependencies : go/dependencies.rst
15
16
.. _Deprecation schedule : https://github.com/bazelbuild/rules_go/wiki/Deprecation-schedule
16
17
.. _Gopher Slack : https://invite.slack.golangbridge.org/
17
18
.. _gopls integration : https://github.com/bazelbuild/rules_go/wiki/Editor-setup
18
19
.. _Overriding dependencies : go/dependencies.rst#overriding-dependencies
19
- .. _Proto dependencies : go/dependencies.rst#proto-dependencies
20
20
.. _Proto rules : proto/core.rst
21
21
.. _Protocol buffers : proto/core.rst
22
22
.. _Toolchains : go/toolchains.rst
23
23
.. _Using rules_go on Windows : windows.rst
24
24
.. _bazel-go-discuss : https://groups.google.com/forum/#!forum/bazel-go-discuss
25
25
.. _configuration transition : https://docs.bazel.build/versions/master/skylark/lib/transition.html
26
- .. _gRPC dependencies : go/dependencies.rst#grpc-dependencies
27
26
.. _gazelle update-repos : https://github.com/bazelbuild/bazel-gazelle#update-repos
28
27
.. _gazelle : https://github.com/bazelbuild/bazel-gazelle
29
28
.. _github.com/bazelbuild/bazel-gazelle : https://github.com/bazelbuild/bazel-gazelle
@@ -202,7 +201,6 @@ Contents
202
201
203
202
* `Overview `_
204
203
* `Setup `_
205
- * `protobuf and gRPC `_
206
204
* `FAQ `_
207
205
208
206
Documentation
@@ -306,302 +304,12 @@ Several additional tools need to be installed and configured.
306
304
Initial project setup
307
305
~~~~~~~~~~~~~~~~~~~~~
308
306
309
- If you are using Bazel's new external dependency management system `Bzlmod `_,
310
- refer to the dedicated `Go with Bzlmod `_ guide instead.
307
+ There are two ways to setup:
311
308
312
- Create a file at the top of your repository named ``WORKSPACE ``, and add the
313
- snippet below (or add to your existing ``WORKSPACE ``). This tells Bazel to
314
- fetch rules_go and its dependencies. Bazel will download a recent supported
315
- Go toolchain and register it for use.
316
-
317
- .. code :: bzl
318
-
319
- load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
320
-
321
- http_archive(
322
- name = "io_bazel_rules_go",
323
- integrity = "sha256-M6zErg9wUC20uJPJ/B3Xqb+ZjCPn/yxFF3QdQEmpdvg=",
324
- urls = [
325
- "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
326
- "https://github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
327
- ],
328
- )
329
-
330
- load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
331
-
332
- go_rules_dependencies()
333
-
334
- go_register_toolchains(version = "1.23.1")
335
-
336
- You can use rules_go at ``master `` by using `git_repository `_ instead of
337
- `http_archive `_ and pointing to a recent commit.
338
-
339
- Add a file named ``BUILD.bazel `` in the root directory of your project.
340
- You'll need a build file in each directory with Go code, but you'll also need
341
- one in the root directory, even if your project doesn't have Go code there.
342
- For a "Hello, world" binary, the file should look like this:
343
-
344
- .. code :: bzl
345
-
346
- load("@io_bazel_rules_go//go:def.bzl", "go_binary")
347
-
348
- go_binary(
349
- name = "hello",
350
- srcs = ["hello.go"],
351
- )
352
-
353
- You can build this target with ``bazel build //:hello ``.
354
-
355
- Generating build files
356
- ~~~~~~~~~~~~~~~~~~~~~~
357
-
358
- If your project can be built with ``go build ``, you can generate and update your
359
- build files automatically using gazelle _.
360
-
361
- Add the ``bazel_gazelle `` repository and its dependencies to your
362
- ``WORKSPACE ``. It should look like this:
363
-
364
- .. code :: bzl
365
-
366
- load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
367
-
368
- http_archive(
369
- name = "io_bazel_rules_go",
370
- integrity = "sha256-M6zErg9wUC20uJPJ/B3Xqb+ZjCPn/yxFF3QdQEmpdvg=",
371
- urls = [
372
- "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
373
- "https://github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
374
- ],
375
- )
376
-
377
- http_archive(
378
- name = "bazel_gazelle",
379
- integrity = "sha256-12v3pg/YsFBEQJDfooN6Tq+YKeEWVhjuNdzspcvfWNU=",
380
- urls = [
381
- "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
382
- "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
383
- ],
384
- )
385
-
386
- load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
387
- load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
388
-
389
- go_rules_dependencies()
390
-
391
- go_register_toolchains(version = "1.23.1")
392
-
393
- gazelle_dependencies()
394
-
395
- Add the code below to the ``BUILD.bazel `` file in your project's root directory.
396
- Replace the string after ``prefix `` with an import path prefix that matches your
397
- project. It should be the same as your module path, if you have a ``go.mod ``
398
- file.
399
-
400
- .. code :: bzl
401
-
402
- load("@bazel_gazelle//:def.bzl", "gazelle")
403
-
404
- # gazelle:prefix github.com/example/project
405
- gazelle(name = "gazelle")
406
-
407
- This declares a ``gazelle `` binary rule, which you can run using the command
408
- below:
409
-
410
- .. code :: bash
411
-
412
- bazel run //:gazelle
413
-
414
- This will generate a ``BUILD.bazel `` file with `go_library `_, `go_binary `_, and
415
- `go_test `_ targets for each package in your project. You can run the same
416
- command in the future to update existing build files with new source files,
417
- dependencies, and options.
418
-
419
- Writing build files by hand
420
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
421
-
422
- If your project doesn't follow ``go build `` conventions or you prefer not to use
423
- gazelle _, you can write build files by hand.
424
-
425
- In each directory that contains Go code, create a file named ``BUILD.bazel ``
426
- Add a ``load `` statement at the top of the file for the rules you use.
427
-
428
- .. code :: bzl
429
-
430
- load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
431
-
432
- For each library, add a `go_library `_ rule like the one below. Source files are
433
- listed in the ``srcs `` attribute. Imported packages outside the standard library
434
- are listed in the ``deps `` attribute using `Bazel labels `_ that refer to
435
- corresponding `go_library `_ rules. The library's import path must be specified
436
- with the ``importpath `` attribute.
437
-
438
- .. code :: bzl
439
-
440
- go_library(
441
- name = "foo_library",
442
- srcs = [
443
- "a.go",
444
- "b.go",
445
- ],
446
- importpath = "github.com/example/project/foo",
447
- deps = [
448
- "//tools",
449
- "@org_golang_x_utils//stuff",
450
- ],
451
- visibility = ["//visibility:public"],
452
- )
453
-
454
- For tests, add a `go_test `_ rule like the one below. The library being tested
455
- should be listed in an ``embed `` attribute.
456
-
457
- .. code :: bzl
458
-
459
- go_test(
460
- name = "foo_test",
461
- srcs = [
462
- "a_test.go",
463
- "b_test.go",
464
- ],
465
- embed = [":foo_lib"],
466
- deps = [
467
- "//testtools",
468
- "@org_golang_x_utils//morestuff",
469
- ],
470
- )
471
-
472
- For binaries, add a `go_binary `_ rule like the one below.
473
-
474
- .. code :: bzl
475
-
476
- go_binary(
477
- name = "foo",
478
- srcs = ["main.go"],
479
- )
480
-
481
- Adding external repositories
482
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
483
-
484
- For each Go repository, add a `go_repository `_ rule to ``WORKSPACE `` like the
485
- one below. This rule comes from the Gazelle repository, so you will need to
486
- load it. `gazelle update-repos `_ can generate or update these rules
487
- automatically from a go.mod or Gopkg.lock file.
488
-
489
- .. code :: bzl
490
-
491
- load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
492
-
493
- # Download the Go rules.
494
- http_archive(
495
- name = "io_bazel_rules_go",
496
- integrity = "sha256-M6zErg9wUC20uJPJ/B3Xqb+ZjCPn/yxFF3QdQEmpdvg=",
497
- urls = [
498
- "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
499
- "https://github.com/bazelbuild/rules_go/releases/download/v0.48.0/rules_go-v0.48.0.zip",
500
- ],
501
- )
502
-
503
- # Download Gazelle.
504
- http_archive(
505
- name = "bazel_gazelle",
506
- integrity = "sha256-12v3pg/YsFBEQJDfooN6Tq+YKeEWVhjuNdzspcvfWNU=",
507
- urls = [
508
- "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
509
- "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
510
- ],
511
- )
512
-
513
- # Load macros and repository rules.
514
- load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
515
- load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
516
-
517
- # Declare Go direct dependencies.
518
- go_repository(
519
- name = "org_golang_x_net",
520
- importpath = "golang.org/x/net",
521
- sum = "h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=",
522
- version = "v0.23.0",
523
- )
524
-
525
- # Declare indirect dependencies and register toolchains.
526
- go_rules_dependencies()
527
-
528
- go_register_toolchains(version = "1.23.1")
529
-
530
- gazelle_dependencies()
531
-
532
-
533
- protobuf and gRPC
534
- -----------------
535
-
536
- To generate code from protocol buffers, you'll need to add a dependency on
537
- ``com_google_protobuf `` to your ``WORKSPACE ``.
538
-
539
- .. code :: bzl
540
-
541
- load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
542
-
543
- http_archive(
544
- name = "com_google_protobuf",
545
- sha256 = "535fbf566d372ccf3a097c374b26896fa044bf4232aef9cab37bd1cc1ba4e850",
546
- strip_prefix = "protobuf-3.15.0",
547
- urls = [
548
- "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.15.0.tar.gz",
549
- "https://github.com/protocolbuffers/protobuf/archive/v3.15.0.tar.gz",
550
- ],
551
- )
552
-
553
- load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
554
-
555
- protobuf_deps()
556
-
557
- You'll need a C/C++ toolchain registered for the execution platform (the
558
- platform where Bazel runs actions) to build protoc.
559
-
560
- The `proto_library `_ rule is provided by the ``rules_proto `` repository.
561
- ``protoc-gen-go ``, the Go proto compiler plugin, is provided by the
562
- ``com_github_golang_protobuf `` repository. Both are declared by
563
- `go_rules_dependencies `_. You won't need to declare an explicit dependency
564
- unless you specifically want to use a different version. See `Overriding
565
- dependencies `_ for instructions on using a different version.
566
-
567
- gRPC dependencies are not declared by default (there are too many). You can
568
- declare them in WORKSPACE using `go_repository `_. You may want to use
569
- `gazelle update-repos `_ to import them from ``go.mod ``.
570
-
571
- See `Proto dependencies `_, `gRPC dependencies `_ for more information. See also
572
- `Avoiding conflicts `_.
573
-
574
- Once all dependencies have been registered, you can declare `proto_library `_
575
- and `go_proto_library `_ rules to generate and compile Go code from .proto
576
- files.
577
-
578
- .. code :: bzl
579
-
580
- load("@rules_proto//proto:defs.bzl", "proto_library")
581
- load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
582
-
583
- proto_library(
584
- name = "foo_proto",
585
- srcs = ["foo.proto"],
586
- deps = ["//bar:bar_proto"],
587
- visibility = ["//visibility:public"],
588
- )
589
-
590
- go_proto_library(
591
- name = "foo_go_proto",
592
- importpath = "github.com/example/protos/foo_proto",
593
- protos = [":foo_proto"],
594
- visibility = ["//visibility:public"],
595
- )
596
-
597
- A ``go_proto_library `` target may be imported and depended on like a normal
598
- ``go_library ``.
599
-
600
- Note that recent versions of rules_go support both APIv1
601
- (``github.com/golang/protobuf ``) and APIv2 (``google.golang.org/protobuf ``).
602
- By default, code is generated with
603
- ``github.com/golang/protobuf/cmd/protoc-gen-gen `` for compatibility with both
604
- interfaces. Client code may import use either runtime library or both.
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.
605
313
606
314
FAQ
607
315
---
0 commit comments