Bazel Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(23 intermediate revisions by the same user not shown)
Line 9: Line 9:
=Package=
=Package=
{{External|https://bazel.build/concepts/build-ref#packages}}
{{External|https://bazel.build/concepts/build-ref#packages}}
A package is the primary unit of code organization in a repository. A package is a collection of related files, and a specification, embodied by the [[Bazel_BUILD_Files#Overview|BUILD file]], of how these files can be used to produce artifacts.
The directory that contains the BUILD file is called the  <span id='Package_Directory'></span>'''package directory'''. The '''package name''' is given by the name of the package directory.
The package contains all the files from the package directory, and recursively, from all its subdirectories, except those which themselves contain a BUILD file. From this definition, no file or directory may be a part of two different packages.
==<span id='BUILD_files'></span>BUILD File==
{{Internal|Bazel_BUILD_Files#Overview|Bazel BUILD Files}}
==Package Group==
A package group is a set of packages whose purpose is to limit accessibility of certain [[#Rule|rules]]. Package groups are defined by the <code>package_group</code> function. <font color=darkkhaki>TODO: https://bazel.build/concepts/build-ref#targets</font>.
=Target=
=Target=
{{External|https://bazel.build/concepts/build-ref#targets}}
{{External|https://bazel.build/concepts/build-ref#targets}}
A target is either a [[#File|file]] or a [[#Rule|rule]].
A package contains a collection of targets, defined in the package's [[Bazel_BUILD_Files#Overview|BUILD file]].
==File==
A file is a kind of [[#Target|target]]. There are two kinds of files: [[#Source_File|source files]] and [[#Generated_File|generated files]].
===Source File===
Source files are usually written by the efforts of people, and checked in to the repository.
===Generated File===
Generated files, sometimes called derived files or output files, are not checked in the repository, but are generated from [[#Source_File|source files]].
==Rule==
A rule specifies the relationship between a set of input and a set of output files.
The inputs to a rule may be [[#Source_File|source files]], but they also may be the outputs of other rules. <font color=darkkhaki>It this case is said that the input of a rule is other rule.</font>
The output files of rule always belong to the same [[#Package|package]] as the rule itself. It is not possible to generate files into another package. It is not uncommon for a rule's inputs to come from another package, though.
=Label=
=Label=
{{External|https://bazel.build/concepts/labels}}
{{External|https://bazel.build/concepts/labels}}
=BUILD files=
{{Internal|Bazel_BUILD_Files#Overview|Bazel BUILD Files}}
=<span id='Dependency'></span>Dependencies=
=<span id='Dependency'></span>Dependencies=
{{External|https://bazel.build/concepts/dependencies}}
{{External|https://bazel.build/concepts/dependencies}}
Line 114: Line 148:
==<tt>nogo</tt>==
==<tt>nogo</tt>==
{{External|https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst#}}
{{External|https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst#}}
<code>nogo</code> 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, <code>nogo</code> may report compiler-like errors.  
<code>nogo</code> 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, <code>nogo</code> may report compiler-like errors. It may be used to run the same analyses as <code>[[#vet|vet</code>]]. The <code>nogo</code> [[#nogo_Setup|rule]] will generate a program that executes all the supplied [[#Analyzers|analyzers]] at build-time. The generated <code>nogo</code> program will run alongside the compiler when building any Go target (e.g. <code>go_library</code>) 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===
===Analyzers===
{{External|https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst#configuring-analyzers}}
{{External|https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst#configuring-analyzers}}
===<tt>nogo</tt> Setup===
To activate <code>nogo</code>, declare a <code>nogo</code> target in the BUILD file.
<syntaxhighlight lang='go'>
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"],
)
</syntaxhighlight>
===<tt>nogo</tt> Configuration===
===<tt>nogo</tt> Configuration===
{{External|https://github.com/bazelbuild/rules_go/blob/master/go/nogo.rst#configuring-analyzers}}
<code>nogo</code> is configured with a JSON file that specifies [[#Analyzer|analyzers]]:
<code>nogo</code> is configured with a JSON file that specifies [[#Analyzer|analyzers]]:
<syntaxhighlight lang='json'>
<syntaxhighlight lang='json'>
Line 163: Line 221:
)
)
</syntaxhighlight>
</syntaxhighlight>
=<tt>vet</tt>=
{{External|https://golang.org/cmd/vet/}}
Also see <code>[[#nogo|nogo]]</code>.

Latest revision as of 21:30, 20 September 2024

Internal

Repository

https://bazel.build/concepts/build-ref#repositories

repositories.bzl

Package

https://bazel.build/concepts/build-ref#packages

A package is the primary unit of code organization in a repository. A package is a collection of related files, and a specification, embodied by the BUILD file, of how these files can be used to produce artifacts.

The directory that contains the BUILD file is called the package directory. The package name is given by the name of the package directory.

The package contains all the files from the package directory, and recursively, from all its subdirectories, except those which themselves contain a BUILD file. From this definition, no file or directory may be a part of two different packages.

BUILD File

Bazel BUILD Files

Package Group

A package group is a set of packages whose purpose is to limit accessibility of certain rules. Package groups are defined by the package_group function. TODO: https://bazel.build/concepts/build-ref#targets.

Target

https://bazel.build/concepts/build-ref#targets

A target is either a file or a rule.

A package contains a collection of targets, defined in the package's BUILD file.

File

A file is a kind of target. There are two kinds of files: source files and generated files.

Source File

Source files are usually written by the efforts of people, and checked in to the repository.

Generated File

Generated files, sometimes called derived files or output files, are not checked in the repository, but are generated from source files.

Rule

A rule specifies the relationship between a set of input and a set of output files.

The inputs to a rule may be source files, but they also may be the outputs of other rules. It this case is said that the input of a rule is other rule.

The output files of rule always belong to the same package as the rule itself. It is not possible to generate files into another package. It is not uncommon for a rule's inputs to come from another package, though.

Label

https://bazel.build/concepts/labels

Dependencies

https://bazel.build/concepts/dependencies

Build Dependency Graph

Also see:

bazel query

Visibility

https://bazel.build/concepts/visibility

Platforms

https://bazel.build/concepts/platforms

Rules

Hermeticity

https://bazel.build/basics/hermeticity

Directories

Workspace

https://bazel.build/concepts/build-ref#workspace

A project's workspace is the directory where bazel looks for build inputs and BUILD files, and where it stores build outputs. The actual location is obtained with bazel info.

Bin

The actual location is obtained with bazel info.

Genfiles

The actual location is obtained with bazel info.

Testlogs

The actual location is obtained with bazel info.

Execution Root

The actual location is obtained with bazel info.

Install Base

The actual location is obtained with bazel info.

Output Base

The actual location is obtained with bazel info.

Output Path

The actual location is obtained with bazel info.

Package Path

%workspace%. See Workspace.

Repository Cache

The actual location is obtained with bazel info.

Files

Server Log

The actual location is obtained with bazel info.

Command Log

The actual location is obtained with bazel info.

.bazelrc

.bazelrc

Project View File

Seems to be this one:

directories:
 # Add the directories you want added as source here
 # By default, we've added your entire workspace ('.')
 .

# Automatically includes all relevant targets under the 'directories' above
derive_targets_from_directories: true

targets:
  # If source code isn't resolving, add additional targets that compile it here

additional_languages:
 # Uncomment any additional languages you want supported
 # android
 # dart
 # go
 # javascript
 # kotlin
 # python
 # scala
 # typescript

# Uncomment to run this target before every sync
# gazelle_target: //:gazelle

BUILD File

Bazel BUILD Files

Starlark

Starlark

Bazelisk

A bazel runner. as "Bazel binary location" in the Bazel IntelliJ Plugin.

Bazel and IntelliJ

Sync Process

The sync process queries Bazel for information and builds IntelliJ project structure to fid Bazel's model. It runs automatically during a project import, and manually by clicking on the sync icon in the menu bar, or partially syncing packages and individual files in contextual menus.

If the file is not reachable from Bazel targets, it will show up as unsynced in the IDE.

TODO: https://blog.bazel.build/2019/09/29/intellij-bazel-sync.html

rules_go

https://github.com/bazelbuild/rules_go/

Reconcile with Bazel_BUILD_Files#Build_Rules.

nogo

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

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"
    }
  },

gazelle

Updates Bazel configuration files.

Example:

bazel run //:gazelle -- update-repos golang.org/x/sys@v0.13.0

adds the following line to the WORKSPACE file:

go_repository(
    name = "org_golang_x_sys",
    importpath = "golang.org/x/sys",
    sum = "h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=",
    version = "v0.13.0",
)

vet

https://golang.org/cmd/vet/

Also see nogo.