Skip to content

Commit

Permalink
Add staticcheck example. (#35)
Browse files Browse the repository at this point in the history
Includes list of checks and check overrides.
  • Loading branch information
ekhabarov authored Jan 31, 2024
1 parent ed3de54 commit 7dc9ac9
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 0 deletions.
12 changes: 12 additions & 0 deletions buildbuddy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ actions:
- "*"
bazel_commands:
- "test --config=workflows //..."

- name: "Test example/staticcheck targets"
triggers:
push:
branches:
- "master"
pull_request:
branches:
- "*"
bazel_workspace_dir: "examples/staticcheck"
bazel_commands:
- "test --config=workflows //tests/..."
Empty file added examples/BUILD.bazel
Empty file.
13 changes: 13 additions & 0 deletions examples/staticcheck/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copied part from the repo root.
#
test --test_output=errors
test --test_verbose_timeout_warnings
# BuildBuddy config
build:workflows --config=buildbuddy_bes_backend
build:workflows --config=buildbuddy_bes_results_url
build:workflows --config=buildbuddy_remote_cache
## TODO: remove this once BuildBuddy folks confirmed that
## the bug has been fixed on their end regarding command is
## not publicly displayed.
build:workflows --build_metadata=VISIBILITY=PUBLIC
test:workflows --keep_going
1 change: 1 addition & 0 deletions examples/staticcheck/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
32 changes: 32 additions & 0 deletions examples/staticcheck/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", "staticcheck_analyzers")
load("@com_github_sluongng_nogo_analyzer//:def.bzl", "nogo_config")
load("@io_bazel_rules_go//go:def.bzl", "TOOLS_NOGO", "nogo")
load(":staticcheck.bzl", "STATICCHECK_ANALYZERS", "STATICCHECK_OVERRIDE")

# gazelle:prefix github.com/sluongng/nogo-analyzer/examples/staticcheck
gazelle(name = "gazelle")

gazelle(
name = "deps",
args = [
"-from_file=go.mod",
"-to_macro=go_deps.bzl%go_deps",
"-prune",
],
command = "update-repos",
)

nogo_config(
name = "nogo_config",
out = "nogo_config.json",
analyzers = STATICCHECK_ANALYZERS,
override = STATICCHECK_OVERRIDE,
)

nogo(
name = "nogo",
config = ":nogo_config.json",
visibility = ["//visibility:public"],
deps = staticcheck_analyzers(STATICCHECK_ANALYZERS),
)
14 changes: 14 additions & 0 deletions examples/staticcheck/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Nogo/staticcheck example.

This example setup includes initial nogo configuration with sevral checks and
check overrides.

Targets:
* `bazel build //cmd/pass` built without errors.
* `bazel build //cmd/failed` fails with the following errors:

```
compilepkg: nogo: errors found by nogo during build-time code analysis:
cmd/failed/main.go:8:20: parsing time "01-01-2023" as "01-01-2023": cannot parse "" as "3" (SA1002)
cmd/failed/main.go:10:14: sleeping for 1 nanoseconds is probably a bug; be explicit if it isn't (SA1004)
```
56 changes: 56 additions & 0 deletions examples/staticcheck/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
workspace(name = "examples_staticcheck")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# http_archive(
# name = "com_github_sluongng_nogo_analyzer",
# sha256 = "a74a5e44751d292d17bd879e5aa8b40baa94b5dc2f043df1e3acbb3e23ead073",
# strip_prefix = "nogo-analyzer-0.0.2",
# urls = [
# "https://github.com/sluongng/nogo-analyzer/archive/refs/tags/v0.0.2.tar.gz",
# ],
# )

# for dev usage only, see an example above for external usage.
local_repository(
name = "com_github_sluongng_nogo_analyzer",
path = "../..",
)

http_archive(
name = "io_bazel_rules_go",
sha256 = "91585017debb61982f7054c9688857a2ad1fd823fc3f9cb05048b0025c47d023",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(
nogo = "@//:nogo",
version = "1.21.4",
)

http_archive(
name = "bazel_gazelle",
sha256 = "b7387f72efb59f876e4daae42f1d3912d0d45563eac7cb23d1de0b094ab588cf",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.34.0/bazel-gazelle-v0.34.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.34.0/bazel-gazelle-v0.34.0.tar.gz",
],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("//:go_deps.bzl", "go_deps")

# gazelle:repository_macro go_deps.bzl%go_deps
go_deps()

load("@com_github_sluongng_nogo_analyzer//staticcheck:deps.bzl", "staticcheck")

staticcheck()

gazelle_dependencies()
21 changes: 21 additions & 0 deletions examples/staticcheck/cmd/failed/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

# fails with an error:
#
# compilepkg: nogo: errors found by nogo during build-time code analysis:
# cmd/failed/main.go:8:20: parsing time "01-01-2023" as "01-01-2023": cannot parse "" as "3" (SA1002)
# cmd/failed/main.go:10:14: sleeping for 1 nanoseconds is probably a bug; be explicit if it isn't (SA1004)
#
# Check SA4013 is excluded
go_library(
name = "failed_lib",
srcs = ["main.go"],
importpath = "github.com/sluongng/nogo-analyzer/examples/staticcheck/cmd/failed",
visibility = ["//visibility:private"],
)

go_binary(
name = "failed",
embed = [":failed_lib"],
visibility = ["//visibility:public"],
)
12 changes: 12 additions & 0 deletions examples/staticcheck/cmd/failed/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"time"
)

func main() {
_, _ = time.Parse("01-01-2023", "2023-01-01")
if !!true {
time.Sleep(1)
}
}
14 changes: 14 additions & 0 deletions examples/staticcheck/cmd/pass/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "pass_lib",
srcs = ["main.go"],
importpath = "github.com/sluongng/nogo-analyzer/examples/staticcheck/cmd/pass",
visibility = ["//visibility:private"],
)

go_binary(
name = "pass",
embed = [":pass_lib"],
visibility = ["//visibility:public"],
)
15 changes: 15 additions & 0 deletions examples/staticcheck/cmd/pass/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"time"
)

func main() {
d, err := time.Parse("01-02-2006", "01-01-2023")
if err != nil {
panic(err)
}

fmt.Printf("date: %s\n", d)
}
4 changes: 4 additions & 0 deletions examples/staticcheck/go_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load("@bazel_gazelle//:deps.bzl", "go_repository")

def go_deps():
pass
18 changes: 18 additions & 0 deletions examples/staticcheck/staticcheck.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SA1 = [
"SA1002", # Invalid format in time.Parse
"SA1004", # Suspiciously small untyped constant in time.Sleep
]

SA4 = [
"SA4013", # Negating a boolean twice (!!b) is the same as writing b. This is either redundant, or a typo.
]

STATICCHECK_ANALYZERS = SA1 + SA4

STATICCHECK_OVERRIDE = {
"SA4013": {
"exclude_files": {
"/": "excluded",
}
},
}
22 changes: 22 additions & 0 deletions examples/staticcheck/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")

go_bazel_test(
name = "tests",
size = "medium",
srcs = ["staticcheck_test.go"],
rule_files = [
"@com_github_sluongng_nogo_analyzer//:all_files",
"@io_bazel_rules_go//:all_files",
"@bazel_gazelle//:all_files",
],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"staticcheck_test.go",
],
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 7dc9ac9

Please sign in to comment.