Nogo: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 3: Line 3:
=Internal=
=Internal=
* [[Bazel_Concepts#nogo|Bazel Concepts]]
* [[Bazel_Concepts#nogo|Bazel Concepts]]
* [[Bazel_BUILD_Files#Build-time_Code_Analysis_with_nogo|<tt>BUILD</tt> Files]]


=Overview=
=Overview=

Revision as of 07:06, 23 November 2024

External

Internal

Overview

nogo is a tool that analyzes source code of Go programs. It runs alongside the Go compiler in the Bazel Go rules and rejects programs that contain disallowing coding patterns. In addition, nogo may report compiler-like errors. It may be used to run the same analyses as vet. The nogo rule will generate a program that executes all the supplied analyzers at build-time. The generated nogo program will run alongside the compiler when building any Go target (e.g. go_library) within your workspace, even if the target is imported from an external repository. However, nogo will not run when targets from the current repository are imported into other workspaces and built there.

Analyzers

https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst#configuring-analyzers

nogo Setup

To activate nogo, declare a nogo target in the BUILD file.

load("@io_bazel_rules_go//go:def.bzl", "nogo")

nogo(
    name = "my_nogo",
    deps = [
        # analyzer from the local repository
        ":importunsafe",
        # analyzer from a remote repository
        "@org_golang_x_tools//go/analysis/passes/printf:go_default_library",
    ],
    visibility = ["//visibility:public"], # must have public visibility
)
go_library(
    name = "importunsafe",
    srcs = ["importunsafe.go"],
    importpath = "importunsafe",
    deps = ["@org_golang_x_tools//go/analysis:go_default_library"],
    visibility = ["//visibility:public"],
)

nogo Configuration

https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst#configuring-analyzers

nogo is configured with a JSON file that specifies analyzers:

{
  "structtag": {
    "exclude_files": {
      "vendor/": "vendor tools don't pass vet",
      "external/": "external tools don't pass vet"
    }
  },
  "exhaustive": {
    "exclude_files": {
      "vendor/": "vendor tools don't pass vet",
      "external/": "external tools don't pass vet"
    }
  },
  ...
}

To exclude completely all files:

  "exhaustive": {
    "exclude_files": {
      ".*": "disable completely"
    }
  },