1*9bb1b549SSpandan Das|logo| nogo build-time code analysis 2*9bb1b549SSpandan Das==================================== 3*9bb1b549SSpandan Das 4*9bb1b549SSpandan Das.. _nogo: nogo.rst#nogo 5*9bb1b549SSpandan Das.. _configuring-analyzers: nogo.rst#configuring-analyzers 6*9bb1b549SSpandan Das.. _go_library: /docs/go/core/rules.md#go_library 7*9bb1b549SSpandan Das.. _analysis: https://godoc.org/golang.org/x/tools/go/analysis 8*9bb1b549SSpandan Das.. _Analyzer: https://godoc.org/golang.org/x/tools/go/analysis#Analyzer 9*9bb1b549SSpandan Das.. _GoLibrary: providers.rst#GoLibrary 10*9bb1b549SSpandan Das.. _GoSource: providers.rst#GoSource 11*9bb1b549SSpandan Das.. _GoArchive: providers.rst#GoArchive 12*9bb1b549SSpandan Das.. _vet: https://golang.org/cmd/vet/ 13*9bb1b549SSpandan Das.. _golangci-lint: https://github.com/golangci/golangci-lint 14*9bb1b549SSpandan Das.. _staticcheck: https://staticcheck.io/ 15*9bb1b549SSpandan Das.. _sluongng/nogo-analyzer: https://github.com/sluongng/nogo-analyzer 16*9bb1b549SSpandan Das 17*9bb1b549SSpandan Das.. role:: param(kbd) 18*9bb1b549SSpandan Das.. role:: type(emphasis) 19*9bb1b549SSpandan Das.. role:: value(code) 20*9bb1b549SSpandan Das.. |mandatory| replace:: **mandatory value** 21*9bb1b549SSpandan Das.. |logo| image:: nogo_logo.png 22*9bb1b549SSpandan Das.. footer:: The ``nogo`` logo was derived from the Go gopher, which was designed by Renee French. (http://reneefrench.blogspot.com/) The design is licensed under the Creative Commons 3.0 Attributions license. Read this article for more details: http://blog.golang.org/gopher 23*9bb1b549SSpandan Das 24*9bb1b549SSpandan Das 25*9bb1b549SSpandan Das**WARNING**: This functionality is experimental, so its API might change. 26*9bb1b549SSpandan DasPlease do not rely on it for production use, but feel free to use it and file 27*9bb1b549SSpandan Dasissues. 28*9bb1b549SSpandan Das 29*9bb1b549SSpandan Das``nogo`` is a tool that analyzes the source code of Go programs. It runs 30*9bb1b549SSpandan Dasalongside the Go compiler in the Bazel Go rules and rejects programs that 31*9bb1b549SSpandan Dascontain disallowed coding patterns. In addition, ``nogo`` may report 32*9bb1b549SSpandan Dascompiler-like errors. 33*9bb1b549SSpandan Das 34*9bb1b549SSpandan Das``nogo`` is a powerful tool for preventing bugs and code anti-patterns early 35*9bb1b549SSpandan Dasin the development process. It may be used to run the same analyses as `vet`_, 36*9bb1b549SSpandan Dasand you can write new analyses for your own code base. 37*9bb1b549SSpandan Das 38*9bb1b549SSpandan Das.. contents:: . 39*9bb1b549SSpandan Das :depth: 2 40*9bb1b549SSpandan Das 41*9bb1b549SSpandan Das----- 42*9bb1b549SSpandan Das 43*9bb1b549SSpandan DasSetup 44*9bb1b549SSpandan Das----- 45*9bb1b549SSpandan Das 46*9bb1b549SSpandan DasCreate a `nogo`_ target in a ``BUILD`` file in your workspace. The ``deps`` 47*9bb1b549SSpandan Dasattribute of this target must contain labels all the analyzers targets that you 48*9bb1b549SSpandan Daswant to run. 49*9bb1b549SSpandan Das 50*9bb1b549SSpandan Das.. code:: bzl 51*9bb1b549SSpandan Das 52*9bb1b549SSpandan Das load("@io_bazel_rules_go//go:def.bzl", "nogo") 53*9bb1b549SSpandan Das 54*9bb1b549SSpandan Das nogo( 55*9bb1b549SSpandan Das name = "my_nogo", 56*9bb1b549SSpandan Das deps = [ 57*9bb1b549SSpandan Das # analyzer from the local repository 58*9bb1b549SSpandan Das ":importunsafe", 59*9bb1b549SSpandan Das # analyzer from a remote repository 60*9bb1b549SSpandan Das "@org_golang_x_tools//go/analysis/passes/printf:go_default_library", 61*9bb1b549SSpandan Das ], 62*9bb1b549SSpandan Das visibility = ["//visibility:public"], # must have public visibility 63*9bb1b549SSpandan Das ) 64*9bb1b549SSpandan Das 65*9bb1b549SSpandan Das go_library( 66*9bb1b549SSpandan Das name = "importunsafe", 67*9bb1b549SSpandan Das srcs = ["importunsafe.go"], 68*9bb1b549SSpandan Das importpath = "importunsafe", 69*9bb1b549SSpandan Das deps = ["@org_golang_x_tools//go/analysis:go_default_library"], 70*9bb1b549SSpandan Das visibility = ["//visibility:public"], 71*9bb1b549SSpandan Das ) 72*9bb1b549SSpandan Das 73*9bb1b549SSpandan DasPass a label for your `nogo`_ target to ``go_register_toolchains`` in your 74*9bb1b549SSpandan Das``WORKSPACE`` file. 75*9bb1b549SSpandan Das 76*9bb1b549SSpandan Das.. code:: bzl 77*9bb1b549SSpandan Das 78*9bb1b549SSpandan Das load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") 79*9bb1b549SSpandan Das go_rules_dependencies() 80*9bb1b549SSpandan Das go_register_toolchains(nogo = "@//:my_nogo") # my_nogo is in the top-level BUILD file of this workspace 81*9bb1b549SSpandan Das 82*9bb1b549SSpandan Das**NOTE**: You must include ``"@//"`` prefix when referring to targets in the local 83*9bb1b549SSpandan Dasworkspace. 84*9bb1b549SSpandan Das 85*9bb1b549SSpandan DasThe `nogo`_ rule will generate a program that executes all the supplied 86*9bb1b549SSpandan Dasanalyzers at build-time. The generated ``nogo`` program will run alongside the 87*9bb1b549SSpandan Dascompiler when building any Go target (e.g. `go_library`_) within your workspace, 88*9bb1b549SSpandan Daseven if the target is imported from an external repository. However, ``nogo`` 89*9bb1b549SSpandan Daswill not run when targets from the current repository are imported into other 90*9bb1b549SSpandan Dasworkspaces and built there. 91*9bb1b549SSpandan Das 92*9bb1b549SSpandan DasTo run all the ``golang.org/x/tools`` analyzers, use ``@io_bazel_rules_go//:tools_nogo``. 93*9bb1b549SSpandan Das 94*9bb1b549SSpandan Das.. code:: bzl 95*9bb1b549SSpandan Das 96*9bb1b549SSpandan Das load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") 97*9bb1b549SSpandan Das go_rules_dependencies() 98*9bb1b549SSpandan Das go_register_toolchains(nogo = "@io_bazel_rules_go//:tools_nogo") 99*9bb1b549SSpandan Das 100*9bb1b549SSpandan DasTo run the analyzers from ``tools_nogo`` together with your own analyzers, use 101*9bb1b549SSpandan Dasthe ``TOOLS_NOGO`` list of dependencies. 102*9bb1b549SSpandan Das 103*9bb1b549SSpandan Das.. code:: bzl 104*9bb1b549SSpandan Das 105*9bb1b549SSpandan Das load("@io_bazel_rules_go//go:def.bzl", "nogo", "TOOLS_NOGO") 106*9bb1b549SSpandan Das 107*9bb1b549SSpandan Das nogo( 108*9bb1b549SSpandan Das name = "my_nogo", 109*9bb1b549SSpandan Das deps = TOOLS_NOGO + [ 110*9bb1b549SSpandan Das # analyzer from the local repository 111*9bb1b549SSpandan Das ":importunsafe", 112*9bb1b549SSpandan Das ], 113*9bb1b549SSpandan Das visibility = ["//visibility:public"], # must have public visibility 114*9bb1b549SSpandan Das ) 115*9bb1b549SSpandan Das 116*9bb1b549SSpandan Das go_library( 117*9bb1b549SSpandan Das name = "importunsafe", 118*9bb1b549SSpandan Das srcs = ["importunsafe.go"], 119*9bb1b549SSpandan Das importpath = "importunsafe", 120*9bb1b549SSpandan Das deps = ["@org_golang_x_tools//go/analysis:go_library"], 121*9bb1b549SSpandan Das visibility = ["//visibility:public"], 122*9bb1b549SSpandan Das ) 123*9bb1b549SSpandan Das 124*9bb1b549SSpandan DasUsage 125*9bb1b549SSpandan Das--------------------------------- 126*9bb1b549SSpandan Das 127*9bb1b549SSpandan Das``nogo``, upon configured, will be invoked automatically when building any Go target in your 128*9bb1b549SSpandan Dasworkspace. If any of the analyzers reject the program, the build will fail. 129*9bb1b549SSpandan Das 130*9bb1b549SSpandan Das``nogo`` will run on all Go targets in your workspace, including tests and binary targets. 131*9bb1b549SSpandan DasIt will also run on targets that are imported from other workspaces by default. You could 132*9bb1b549SSpandan Dasexclude the external repositories from ``nogo`` by using the `exclude_files` regex in 133*9bb1b549SSpandan Das`configuring-analyzers`_. 134*9bb1b549SSpandan Das 135*9bb1b549SSpandan DasRelationship with other linters 136*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~~~~ 137*9bb1b549SSpandan Das 138*9bb1b549SSpandan DasIn Golang, a linter is composed of multiple parts: 139*9bb1b549SSpandan Das 140*9bb1b549SSpandan Das- A collection of rules (checks) that define different validations against the source code 141*9bb1b549SSpandan Das 142*9bb1b549SSpandan Das- Optionally, each rules could be coupled with a fixer that can automatically fix the code. 143*9bb1b549SSpandan Das 144*9bb1b549SSpandan Das- A configuration framework that allows users to enable/disable rules, and configure the rules. 145*9bb1b549SSpandan Das 146*9bb1b549SSpandan Das- A runner binary that orchestrate the above components. 147*9bb1b549SSpandan Das 148*9bb1b549SSpandan DasTo help with the above, Go provides a framework called `analysis`_ that allows 149*9bb1b549SSpandan Dasyou to write a linter in a modular way. In which, you could define each rules as a separate 150*9bb1b549SSpandan Das`Analyzer`_, and then compose them together in a runner binary. 151*9bb1b549SSpandan Das 152*9bb1b549SSpandan DasFor example, `golangci-lint`_ or `staticcheck`_ are popular linters that are composed of multiple 153*9bb1b549SSpandan Dasanalyzers, each of which is a collection of rules. 154*9bb1b549SSpandan Das 155*9bb1b549SSpandan Das``nogo`` is a runner binary that runs a collection of analyzers while leveraging Bazel's 156*9bb1b549SSpandan Dasaction orchestration framework. In particular, ``nogo`` is run as part of rules_go GoCompilePkg 157*9bb1b549SSpandan Dasaction, and it is run in parallel with the Go compiler. This allows ``nogo`` to benefit from 158*9bb1b549SSpandan DasBazel's incremental build and caching as well as the Remote Build Execution framework. 159*9bb1b549SSpandan Das 160*9bb1b549SSpandan DasThere are examples of how to re-use the analyzers from `golangci-lint`_ and `staticcheck`_ in 161*9bb1b549SSpandan Das`nogo`_ here: `sluongng/nogo-analyzer`_. 162*9bb1b549SSpandan Das 163*9bb1b549SSpandan DasShould I use ``nogo`` or ``golangci-lint``? 164*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~~~~ 165*9bb1b549SSpandan Das 166*9bb1b549SSpandan DasBecause ``nogo`` benefits from Bazel's incremental build and caching, it is more suitable for 167*9bb1b549SSpandan Daslarge code bases. If you have a smaller code base, you could use ``golangci-lint`` instead. 168*9bb1b549SSpandan Das 169*9bb1b549SSpandan DasIf ``golangci-lint`` takes a really long time to run in your repository, you could try to use 170*9bb1b549SSpandan Das``nogo`` instead. 171*9bb1b549SSpandan Das 172*9bb1b549SSpandan DasAs of the moment of this writing, there is no way for ``nogo`` to apply the fixers coupled 173*9bb1b549SSpandan Daswith the analyzers. So separate linters such as ``golangci-lint`` or ``staticcheck`` are more 174*9bb1b549SSpandan Dasergonomic to apply the fixes to the code base. 175*9bb1b549SSpandan Das 176*9bb1b549SSpandan DasWriting and registering analyzers 177*9bb1b549SSpandan Das--------------------------------- 178*9bb1b549SSpandan Das 179*9bb1b549SSpandan Das``nogo`` analyzers are Go packages that declare a variable named ``Analyzer`` 180*9bb1b549SSpandan Dasof type `Analyzer`_ from package `analysis`_. Each analyzer is invoked once per 181*9bb1b549SSpandan DasGo package, and is provided the abstract syntax trees (ASTs) and type 182*9bb1b549SSpandan Dasinformation for that package, as well as relevant results of analyzers that have 183*9bb1b549SSpandan Dasalready been run. For example: 184*9bb1b549SSpandan Das 185*9bb1b549SSpandan Das.. code:: go 186*9bb1b549SSpandan Das 187*9bb1b549SSpandan Das // package importunsafe checks whether a Go package imports package unsafe. 188*9bb1b549SSpandan Das package importunsafe 189*9bb1b549SSpandan Das 190*9bb1b549SSpandan Das import ( 191*9bb1b549SSpandan Das "strconv" 192*9bb1b549SSpandan Das 193*9bb1b549SSpandan Das "golang.org/x/tools/go/analysis" 194*9bb1b549SSpandan Das ) 195*9bb1b549SSpandan Das 196*9bb1b549SSpandan Das var Analyzer = &analysis.Analyzer{ 197*9bb1b549SSpandan Das Name: "importunsafe", 198*9bb1b549SSpandan Das Doc: "reports imports of package unsafe", 199*9bb1b549SSpandan Das Run: run, 200*9bb1b549SSpandan Das } 201*9bb1b549SSpandan Das 202*9bb1b549SSpandan Das func run(pass *analysis.Pass) (interface{}, error) { 203*9bb1b549SSpandan Das for _, f := range pass.Files { 204*9bb1b549SSpandan Das for _, imp := range f.Imports { 205*9bb1b549SSpandan Das path, err := strconv.Unquote(imp.Path.Value) 206*9bb1b549SSpandan Das if err == nil && path == "unsafe" { 207*9bb1b549SSpandan Das pass.Reportf(imp.Pos(), "package unsafe must not be imported") 208*9bb1b549SSpandan Das } 209*9bb1b549SSpandan Das } 210*9bb1b549SSpandan Das } 211*9bb1b549SSpandan Das return nil, nil 212*9bb1b549SSpandan Das } 213*9bb1b549SSpandan Das 214*9bb1b549SSpandan DasAny diagnostics reported by the analyzer will stop the build. Do not emit 215*9bb1b549SSpandan Dasdiagnostics unless they are severe enough to warrant stopping the build. 216*9bb1b549SSpandan Das 217*9bb1b549SSpandan DasPass labels for these targets to the ``deps`` attribute of your `nogo`_ target, 218*9bb1b549SSpandan Dasas described in the `Setup`_ section. 219*9bb1b549SSpandan Das 220*9bb1b549SSpandan DasConfiguring analyzers 221*9bb1b549SSpandan Das~~~~~~~~~~~~~~~~~~~~~ 222*9bb1b549SSpandan Das 223*9bb1b549SSpandan DasBy default, ``nogo`` analyzers will emit diagnostics for all Go source files 224*9bb1b549SSpandan Dasbuilt by Bazel. This behavior can be changed with a JSON configuration file. 225*9bb1b549SSpandan Das 226*9bb1b549SSpandan DasThe top-level JSON object in the file must be keyed by the name of the analyzer 227*9bb1b549SSpandan Dasbeing configured. These names must match the ``Analyzer.Name`` of the registered 228*9bb1b549SSpandan Dasanalysis package. The JSON object's values are themselves objects which may 229*9bb1b549SSpandan Dascontain the following key-value pairs: 230*9bb1b549SSpandan Das 231*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 232*9bb1b549SSpandan Das| **Key** | **Type** | 233*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 234*9bb1b549SSpandan Das| ``"description"`` | :type:`string` | 235*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 236*9bb1b549SSpandan Das| Description of this analyzer configuration. | 237*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 238*9bb1b549SSpandan Das| ``"only_files"`` | :type:`dictionary, string to string` | 239*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 240*9bb1b549SSpandan Das| Specifies files that this analyzer will emit diagnostics for. | 241*9bb1b549SSpandan Das| Its keys are regular expression strings matching Go file names, and its values are strings | 242*9bb1b549SSpandan Das| containing a description of the entry. | 243*9bb1b549SSpandan Das| If both ``only_files`` and ``exclude_files`` are empty, this analyzer will emit diagnostics for | 244*9bb1b549SSpandan Das| all Go files built by Bazel. | 245*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 246*9bb1b549SSpandan Das| ``"exclude_files"`` | :type:`dictionary, string to string` | 247*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 248*9bb1b549SSpandan Das| Specifies files that this analyzer will not emit diagnostics for. | 249*9bb1b549SSpandan Das| Its keys and values are strings that have the same semantics as those in ``only_files``. | 250*9bb1b549SSpandan Das| Keys in ``exclude_files`` override keys in ``only_files``. If a .go file matches a key present | 251*9bb1b549SSpandan Das| in both ``only_files`` and ``exclude_files``, the analyzer will not emit diagnostics for that | 252*9bb1b549SSpandan Das| file. | 253*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 254*9bb1b549SSpandan Das| ``"analyzer_flags"`` | :type:`dictionary, string to string` | 255*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 256*9bb1b549SSpandan Das| Passes on a set of flags as defined by the Go ``flag`` package to the analyzer via the | 257*9bb1b549SSpandan Das| ``analysis.Analyzer.Flags`` field. Its keys are the flag names *without* a ``-`` prefix, and its | 258*9bb1b549SSpandan Das| values are the flag values. nogo will exit with an error upon receiving flags not recognized by | 259*9bb1b549SSpandan Das| the analyzer or upon receiving ill-formatted flag values as defined by the corresponding | 260*9bb1b549SSpandan Das| ``flag.Value`` specified by the analyzer. | 261*9bb1b549SSpandan Das+----------------------------+---------------------------------------------------------------------+ 262*9bb1b549SSpandan Das 263*9bb1b549SSpandan Das``nogo`` also supports a special key to specify the same config for all analyzers, even if they are 264*9bb1b549SSpandan Dasnot explicitly specified called ``_base``. See below for an example of its usage. 265*9bb1b549SSpandan Das 266*9bb1b549SSpandan DasExample 267*9bb1b549SSpandan Das^^^^^^^ 268*9bb1b549SSpandan Das 269*9bb1b549SSpandan DasThe following configuration file configures the analyzers named ``importunsafe`` 270*9bb1b549SSpandan Dasand ``unsafedom``. Since the ``loopclosure`` analyzer is not explicitly 271*9bb1b549SSpandan Dasconfigured, it will emit diagnostics for all Go files built by Bazel. 272*9bb1b549SSpandan Das``unsafedom`` will receive a flag equivalent to ``-block-unescaped-html=false`` 273*9bb1b549SSpandan Dason a command line driver. 274*9bb1b549SSpandan Das 275*9bb1b549SSpandan Das.. code:: json 276*9bb1b549SSpandan Das 277*9bb1b549SSpandan Das { 278*9bb1b549SSpandan Das "_base": { 279*9bb1b549SSpandan Das "description": "Base config that all subsequent analyzers, even unspecified will inherit.", 280*9bb1b549SSpandan Das "exclude_files": { 281*9bb1b549SSpandan Das "third_party/": "exclude all third_party code for all analyzers" 282*9bb1b549SSpandan Das } 283*9bb1b549SSpandan Das }, 284*9bb1b549SSpandan Das "importunsafe": { 285*9bb1b549SSpandan Das "exclude_files": { 286*9bb1b549SSpandan Das "src/foo\\.go": "manually verified that behavior is working-as-intended", 287*9bb1b549SSpandan Das "src/bar\\.go": "see issue #1337" 288*9bb1b549SSpandan Das } 289*9bb1b549SSpandan Das }, 290*9bb1b549SSpandan Das "unsafedom": { 291*9bb1b549SSpandan Das "only_files": { 292*9bb1b549SSpandan Das "src/js/.*": "" 293*9bb1b549SSpandan Das }, 294*9bb1b549SSpandan Das "exclude_files": { 295*9bb1b549SSpandan Das "src/(third_party|vendor)/.*": "enforce DOM safety requirements only on first-party code" 296*9bb1b549SSpandan Das }, 297*9bb1b549SSpandan Das "analyzer_flags": { 298*9bb1b549SSpandan Das "block-unescaped-html": "false", 299*9bb1b549SSpandan Das }, 300*9bb1b549SSpandan Das } 301*9bb1b549SSpandan Das } 302*9bb1b549SSpandan Das 303*9bb1b549SSpandan DasThis label referencing this configuration file must be provided as the 304*9bb1b549SSpandan Das``config`` attribute value of the ``nogo`` rule. 305*9bb1b549SSpandan Das 306*9bb1b549SSpandan Das.. code:: bzl 307*9bb1b549SSpandan Das 308*9bb1b549SSpandan Das nogo( 309*9bb1b549SSpandan Das name = "my_nogo", 310*9bb1b549SSpandan Das deps = [ 311*9bb1b549SSpandan Das ":importunsafe", 312*9bb1b549SSpandan Das ":unsafedom", 313*9bb1b549SSpandan Das "@analyzers//:loopclosure", 314*9bb1b549SSpandan Das ], 315*9bb1b549SSpandan Das config = "config.json", 316*9bb1b549SSpandan Das visibility = ["//visibility:public"], 317*9bb1b549SSpandan Das ) 318*9bb1b549SSpandan Das 319*9bb1b549SSpandan DasRunning vet 320*9bb1b549SSpandan Das----------- 321*9bb1b549SSpandan Das 322*9bb1b549SSpandan Das`vet`_ is a tool that examines Go source code and reports correctness issues not 323*9bb1b549SSpandan Dascaught by Go compilers. It is included in the official Go distribution. Vet 324*9bb1b549SSpandan Dasruns analyses built with the Go `analysis`_ framework. nogo uses the 325*9bb1b549SSpandan Dassame framework, which means vet checks can be run with nogo. 326*9bb1b549SSpandan Das 327*9bb1b549SSpandan DasYou can choose to run a safe subset of vet checks alongside the Go compiler by 328*9bb1b549SSpandan Dassetting ``vet = True`` in your `nogo`_ target. This will only run vet checks 329*9bb1b549SSpandan Dasthat are believed to be 100% accurate (the same set run by ``go test`` by 330*9bb1b549SSpandan Dasdefault). 331*9bb1b549SSpandan Das 332*9bb1b549SSpandan Das.. code:: bzl 333*9bb1b549SSpandan Das 334*9bb1b549SSpandan Das nogo( 335*9bb1b549SSpandan Das name = "my_nogo", 336*9bb1b549SSpandan Das vet = True, 337*9bb1b549SSpandan Das visibility = ["//visibility:public"], 338*9bb1b549SSpandan Das ) 339*9bb1b549SSpandan Das 340*9bb1b549SSpandan DasSetting ``vet = True`` is equivalent to adding the ``atomic``, ``bools``, 341*9bb1b549SSpandan Das``buildtag``, ``nilfunc``, and ``printf`` analyzers from 342*9bb1b549SSpandan Das``@org_golang_x_tools//go/analysis/passes`` to the ``deps`` list of your 343*9bb1b549SSpandan Das``nogo`` rule. 344*9bb1b549SSpandan Das 345*9bb1b549SSpandan Das 346*9bb1b549SSpandan DasSee the full list of available nogo checks: 347*9bb1b549SSpandan Das 348*9bb1b549SSpandan Das.. code:: shell 349*9bb1b549SSpandan Das 350*9bb1b549SSpandan Das bazel query 'kind(go_library, @org_golang_x_tools//go/analysis/passes/...)' 351*9bb1b549SSpandan Das 352*9bb1b549SSpandan Das 353*9bb1b549SSpandan DasAPI 354*9bb1b549SSpandan Das--- 355*9bb1b549SSpandan Das 356*9bb1b549SSpandan Dasnogo 357*9bb1b549SSpandan Das~~~~ 358*9bb1b549SSpandan Das 359*9bb1b549SSpandan DasThis generates a program that analyzes the source code of Go programs. It 360*9bb1b549SSpandan Dasruns alongside the Go compiler in the Bazel Go rules and rejects programs that 361*9bb1b549SSpandan Dascontain disallowed coding patterns. 362*9bb1b549SSpandan Das 363*9bb1b549SSpandan DasAttributes 364*9bb1b549SSpandan Das^^^^^^^^^^ 365*9bb1b549SSpandan Das 366*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 367*9bb1b549SSpandan Das| **Name** | **Type** | **Default value** | 368*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 369*9bb1b549SSpandan Das| :param:`name` | :type:`string` | |mandatory| | 370*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 371*9bb1b549SSpandan Das| A unique name for this rule. | 372*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 373*9bb1b549SSpandan Das| :param:`deps` | :type:`label_list` | :value:`None` | 374*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 375*9bb1b549SSpandan Das| List of Go libraries that will be linked to the generated nogo binary. | 376*9bb1b549SSpandan Das| | 377*9bb1b549SSpandan Das| These libraries must declare an ``analysis.Analyzer`` variable named `Analyzer` to ensure that | 378*9bb1b549SSpandan Das| the analyzers they implement are called by nogo. | 379*9bb1b549SSpandan Das| | 380*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 381*9bb1b549SSpandan Das| :param:`config` | :type:`label` | :value:`None` | 382*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 383*9bb1b549SSpandan Das| JSON configuration file that configures one or more of the analyzers in ``deps``. | 384*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 385*9bb1b549SSpandan Das| :param:`vet` | :type:`bool` | :value:`False` | 386*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 387*9bb1b549SSpandan Das| If true, a safe subset of vet checks will be run by nogo (the same subset run | 388*9bb1b549SSpandan Das| by ``go test ``). | 389*9bb1b549SSpandan Das+----------------------------+-----------------------------+---------------------------------------+ 390*9bb1b549SSpandan Das 391*9bb1b549SSpandan DasExample 392*9bb1b549SSpandan Das^^^^^^^ 393*9bb1b549SSpandan Das 394*9bb1b549SSpandan Das.. code:: bzl 395*9bb1b549SSpandan Das 396*9bb1b549SSpandan Das nogo( 397*9bb1b549SSpandan Das name = "my_nogo", 398*9bb1b549SSpandan Das deps = [ 399*9bb1b549SSpandan Das ":importunsafe", 400*9bb1b549SSpandan Das ":otheranalyzer", 401*9bb1b549SSpandan Das "@analyzers//:unsafedom", 402*9bb1b549SSpandan Das ], 403*9bb1b549SSpandan Das config = ":config.json", 404*9bb1b549SSpandan Das vet = True, 405*9bb1b549SSpandan Das visibility = ["//visibility:public"], 406*9bb1b549SSpandan Das ) 407