Skip to content

Commit

Permalink
nogo is a tool that analyzes the source code of Go programs. (bazel-c…
Browse files Browse the repository at this point in the history
…ontrib#1759)

It runs alongside the Go compiler in the Bazel Go rules and rejects
programs that contain disallowed coding patterns.

nogo is a powerful tool for preventing bugs and code anti-patterns
early in the development process.
  • Loading branch information
jayconrod authored Oct 11, 2018
1 parent 1189392 commit a3c4fd9
Show file tree
Hide file tree
Showing 47 changed files with 2,266 additions and 23 deletions.
7 changes: 7 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_path", "go_vet_test")
load("@io_bazel_rules_go//go/private:tools/lines_sorted_test.bzl", "lines_sorted_test")
load("@io_bazel_rules_go//go/private:rules/nogo.bzl", "nogo")
load("@io_bazel_rules_go//go/private:rules/info.bzl", "go_info")
load("@io_bazel_rules_go//go/private:context.bzl", "go_context_data")
load("@io_bazel_rules_go//go/private:rules/stdlib.bzl", "stdlib")
Expand All @@ -15,6 +16,11 @@ builders(
visibility = ["//visibility:public"],
)

nogo(
name = "default_nogo",
visibility = ["//visibility:public"],
)

go_context_data(
name = "go_context_data",
strip = select({
Expand Down Expand Up @@ -52,6 +58,7 @@ go_path(
"//go/tools/builders:cgo",
"//go/tools/builders:compile",
"//go/tools/builders:embed",
"//go/tools/builders:generate_nogo_main",
"//go/tools/builders:generate_test_main",
"//go/tools/builders:link",
"//go/tools/builders:md5sum",
Expand Down
9 changes: 6 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Go rules for Bazel_
.. _Deprecation schedule: deprecation.rst
.. _Avoiding conflicts: proto/core.rst#avoiding-conflicts
.. _Overriding dependencies: go/workspace.rst#overriding-dependencies
.. _nogo: go/nogo.rst

.. ;; And now we continue with the actual content
Expand Down Expand Up @@ -76,7 +77,7 @@ Documentation
~~~~~~~~~~~~~

* `Core API <go/core.rst>`_

* `go_binary`_
* `go_library`_
* `go_test`_
Expand All @@ -89,6 +90,7 @@ Documentation

* `Toolchains <go/toolchains.rst>`_
* `Extra rules <go/extras.rst>`_
* `nogo build-time code analysis <go/nogo.rst>`_
* `Build modes <go/modes.rst>`_

Quick links
Expand All @@ -109,6 +111,7 @@ The rules are in the alpha stage of development. They support:
* cgo
* cross compilation
* auto generating BUILD files via gazelle_
* build-time code analysis via nogo_
* `protocol buffers`_

They currently do not support (in order of importance):
Expand Down Expand Up @@ -397,7 +400,7 @@ work in some cases.

In some cases, you may want to set the ``goos`` and ``goarch`` attributes of
``go_binary``. This will cross-compile a binary for a specific platform.
This is necessary when you need to produce multiple binaries for different
This is necessary when you need to produce multiple binaries for different
platforms in a single build. However, note that ``select`` expressions will
not work correctly when using these attributes.

Expand Down Expand Up @@ -526,7 +529,7 @@ must be named ``go_sdk``, and it must come *before* the call to
go_register_toolchains()
How do I get information about the Go SDK used by rules_go?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
70 changes: 70 additions & 0 deletions go/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Core go rules
.. _build constraints: https://golang.org/pkg/go/build/#hdr-Build_Constraints
.. _select: https://docs.bazel.build/versions/master/be/functions.html#select
.. _config_setting: https://docs.bazel.build/versions/master/be/general.html#config_setting
.. _nogo: nogo.rst#nogo

.. role:: param(kbd)
.. role:: type(emphasis)
Expand Down Expand Up @@ -272,6 +273,75 @@ Example
visibility = ["//visibility:public"],
)
go_tool_library
~~~~~~~~~~~~~~~

This builds a Go library from a set of source files that are all part of
the same package.

This rule is a limited variant of ``go_library`` which may be used to
bootstrap tools used by rules_go. This avoids a circular dependency.
If you are building analyzers to be linked into a `nogo`_ binary, you'll
need to use ``go_tool_library`` since ``go_library`` depends on `nogo`_
implicitly.

Providers
^^^^^^^^^

* GoLibrary_
* GoSource_
* GoArchive_

Attributes
^^^^^^^^^^

+----------------------------+-----------------------------+---------------------------------------+
| **Name** | **Type** | **Default value** |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`name` | :type:`string` | |mandatory| |
+----------------------------+-----------------------------+---------------------------------------+
| A unique name for this rule. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`srcs` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| The list of Go source files that are compiled to create the package. |
| Only :value:`.go` files are permitted. Cgo, assembly, and build constraints |
| are not supported. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`deps` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| List of Go libraries this library imports directly. |
| These must be ``go_tool_library`` targets to avoid circular dependencies. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`embed` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| List of Go libraries this library embeds. Embedded sources will be compiled |
| together with this library's sources, and the combined list of dependencies |
| will be available for import. These libraries must be ``go_tool_library`` |
| targets to avoid circular dependencies. See Embedding_ for more information |
| about how and when to use this. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`data` | :type:`label_list` | :value:`None` |
+----------------------------+-----------------------------+---------------------------------------+
| The list of files needed by this rule at runtime. Targets named in the data attribute will |
| appear in the *.runfiles area of this rule, if it has one. This may include data files needed |
| by the binary, or other programs needed by it. See `data dependencies`_ for more information |
| about how to depend on and use data files. |
+----------------------------+-----------------------------+---------------------------------------+

Example
^^^^^^^

.. code:: bzl
go_tool_library(
name = "importunsafe",
srcs = ["importunsafe.go"],
importpath = "importunsafe",
deps = ["@org_golang_x_tools//go/analysis:go_tool_library"],
visibility = ["//visibility:public"],
)
go_binary
~~~~~~~~~

Expand Down
13 changes: 12 additions & 1 deletion go/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ load(
)
load(
"@io_bazel_rules_go//go/private:repositories.bzl",
"go_register_toolchains",
"go_rules_dependencies",
)
load(
"@io_bazel_rules_go//go/toolchain:toolchains.bzl",
"go_register_toolchains",
)
load(
"@io_bazel_rules_go//go/private:sdk.bzl",
"go_download_sdk",
Expand Down Expand Up @@ -71,6 +74,14 @@ load(
"@io_bazel_rules_go//go/private:rules/rule.bzl",
_go_rule = "go_rule",
)
load(
"@io_bazel_rules_go//go/private:rules/library.bzl",
"go_tool_library",
)
load(
"@io_bazel_rules_go//go/private:rules/nogo.bzl",
"nogo",
)

# Current version or next version to be tagged. Gazelle and other tools may
# check this to determine compatibility.
Expand Down
Loading

0 comments on commit a3c4fd9

Please sign in to comment.