• Stars
    star
    440
  • Rank 99,050 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tools for building Bazel targets when source files change.

Bazel watcher

Build status

Note: This is not an official Google product.

A source file watcher for Bazel projects

Ever wanted to save a file and have your tests automatically run? How about restart your webserver when one of the source files change? Look no further.

Install ibazel using one of the 3 methods described below. Then:

$ ibazel build //path/to/my:target

Hack hack hack. Save and your target will be rebuilt.

Right now this repo supports build, test, and run.

Installation

There are several ways to install iBazel, documented below.

Mac (Homebrew)

If you run a macOS you can install it from homebrew.

$ brew install ibazel

NPM

If you're a JS developer you can install it as a devDependency or by calling npm install directly in your project

$ npm install @bazel/ibazel

Linux

Packages are available for the following distributions:

Compiling yourself

You can, of course, build iBazel using Bazel.

$ git clone [email protected]:bazelbuild/bazel-watcher
$ cd bazel-watcher
$ bazel build //cmd/ibazel

Now copy the generated binary onto your path:

$ export PATH=$PATH:$PWD/bazel-bin/cmd/ibazel/ibazel_

Running a target

By default, a target started with ibazel run will be terminated and restarted whenever it's notified of source changes. Alternatively, if the build rule for your target contains ibazel_notify_changes in its tags attribute, then the command will stay alive and will receive a notification of the source changes on stdin.

Output Runner

iBazel is capable of producing and running commands from the output of Bazel commands. If iBazel is run with the flag --run_output then it will check for a %WORKSPACE%/.bazel_fix_commands.json and if present run any commands that match the provided regular expressions. For example the commands defined by the following file will match buildozer commands found in the output and provide a prompt to run the command as well as invoke bazel run //:gazelle if it detects a missing import for your go code.

[
  {
    "regex": "^Check that imports in Go sources match importpath attributes in deps.$",
    "command": "bazel",
    "args": [ "run", "//:gazelle" ]
  },
  {
    "regex": "^buildozer '(.*)'\\s+(.*)$",
    "command": "buildozer",
    "args": [ "$1", "$2" ]
  },
  {
    "regex": "^(\\S+)/[\\w-]+\\.proto:\\d+:\\d+: Import \"\\S+\" was not found or had errors\\.$",
    "command": "bazel",
    "args": [ "run", "//:gazelle", "--", "proto/$1" ]
  }
]

Adding the flag --run_output_interactive=false will automatically run the command without prompting for confirmation. The fields in .bazel_fix_commands.json are:

  • regex: a regular expression that will be matched against every line of output.
    • backslash \ characters will need to be escaped once for the regex to be parsed properly.
  • command: a command that will be run from the workspace root.
  • args: a list of arguments to provide to the command, substituting $1 with the first match group of regex, $2 with the second, etc., and $0 for the entire match.

You can disable this feature by adding flag --run_output=false or you can create a .bazel_fix_commands.json that contains an empty json array, []. This will additionally disable the notification providing usage instructions on the first invocation of iBazel.

Profiling

iBazel has a --profile_dev flag which turns on a generated profile output file about the build process and timing. To use it include this flag in the command line. For example,

ibazel --profile_dev=profile.json run devserver

The profile outfile is in concatenated JSON format. Events are outputted one per line.

Profiler events

Event Description Attributes (* optional)
IBAZEL_START Emitted when iBazel is started as part of the first iteration type, iteration, time, iBazelVersion, bazelVersion, maxHeapSize, committedHeapSize
SOURCE_CHANGE A source file change was detected type, iteration, time, targets, elapsed, change
GRAPH_CHANGE A build file change was detected type, iteration, time, targets, elapsed, changes*
RELOAD_TRIGGERED A livereload was triggered to any listening browsers type, iteration, time, targets, elapsed, changes*
RUN_START A run operation started type, iteration, time, targets, elapsed, changes*
RUN_FAILED A run operation failed type, iteration, time, targets, elapsed, changes*
RUN_DONE A run operation completed successfully type, iteration, time, targets, elapsed, changes*
BUILD_START A build operation started type, iteration, time, targets, elapsed, changes*
BUILD_FAILED A build operation failed type, iteration, time, targets, elapsed, changes*
BUILD_DONE A build operation completed successfully type, iteration, time, targets, elapsed, changes*
TEST_START A test operation started type, iteration, time, targets, elapsed, changes*
TEST_FAILED A test operation failed type, iteration, time, targets, elapsed, changes*
TEST_DONE A test operation completed successfully type, iteration, time, targets, elapsed, changes*
REMOTE_EVENT A remote event was received from the browser type, iteration, time, targets, elapsed, remoteType, remoteTime, remoteElapsed, remoteData
REMOTE_EVENT / PAGE_LOAD A remote event emitted by the profiler client-side script on the browser's load event. remoteType is PAGE_LOAD. type, iteration, time, targets, elapsed, remoteType, remoteTime, remoteElapsed, remoteData

Event attributes

Attribute Type Description
type string Event type.
iteration string Unique build iteration key.
time integer Time of event.
targets string[] List of targets that are being built (Note: this is a complete list and includes targets that were already built prior to an iteration).
elapsed integer Elapsed time in ms since the start of the iteration.
change string The file changed on a SOURCE_CHANGE or GRAPH_CHANGE event.
changes string[] A cumulative list of files changed during a build iteration.
iBazelVersion string Version of iBazel that generated this event.
bazelVersion string Version of bazel in use.
maxHeapSize string Max heap size as reported by Bazel.
committedHeapSize string Committed heap size as reported by Bazel.
remoteType string Sub-type for REMOTE_EVENT type.
remoteTime number Browser time for REMOTE_EVENT type.
remoteElapsed number Elapsed time in browser since navigationStart for REMOTE_EVENT type.
remoteData string Data sent from browser for REMOTE_EVENT type. This may be in escaped JSON format for some remote events.

Example profile output file

You can find an example profile output JSON file here. Below is the file in pretty print JSON format:

{
  "type": "IBAZEL_START",
  "iteration": "4214114686684e0f",
  "time": 1513706108351,
  "iBazelVersion": "v0.2.0-dirty",
  "bazelVersion": "release 0.8.1-homebrew",
  "maxHeapSize": "3817MB",
  "committedHeapSize": "1372MB"
}
{
  "type": "RUN_START",
  "iteration": "4214114686684e0f",
  "time": 1513706109329,
  "targets": [
    "//src:devserver"
  ],
  "elapsed": 978
}
{
  "type": "RELOAD_TRIGGERED",
  "iteration": "4214114686684e0f",
  "time": 1513706114595,
  "targets": [
    "//src:devserver"
  ],
  "elapsed": 6244
}
{
  "type": "RUN_DONE",
  "iteration": "4214114686684e0f",
  "time": 1513706114595,
  "targets": [
    "//src:devserver"
  ],
  "elapsed": 6244
}
{
  "type": "SOURCE_CHANGE",
  "iteration": "7e6f8e150e9a8367",
  "time": 1513706129384,
  "targets": [
    "//src:devserver"
  ],
  "change": "/Users/greg/google/gregmagolan/angular-bazel-example/src/hello-world/hello-world.component.ts"
}
{
  "type": "RUN_START",
  "iteration": "7e6f8e150e9a8367",
  "time": 1513706129484,
  "targets": [
    "//src:devserver"
  ],
  "elapsed": 100,
  "changes": [
    "/Users/greg/google/gregmagolan/angular-bazel-example/src/hello-world/hello-world.component.ts"
  ]
}
{
  "type": "RELOAD_TRIGGERED",
  "iteration": "7e6f8e150e9a8367",
  "time": 1513706133947,
  "targets": [
    "//src:devserver"
  ],
  "elapsed": 4563,
  "changes": [
    "/Users/greg/google/gregmagolan/angular-bazel-example/src/hello-world/hello-world.component.ts"
  ]
}
{
  "type": "RUN_DONE",
  "iteration": "7e6f8e150e9a8367",
  "time": 1513706133947,
  "targets": [
    "//src:devserver"
  ],
  "elapsed": 4563,
  "changes": [
    "/Users/greg/google/gregmagolan/angular-bazel-example/src/hello-world/hello-world.component.ts"
  ]
}
{
  "type": "REMOTE_EVENT",
  "iteration": "7e6f8e150e9a8367",
  "time": 1513706134297,
  "targets": [
    "//src:devserver"
  ],
  "elapsed": 4913,
  "remoteType": "PAGE_LOAD",
  "remoteTime": 1513706134294,
  "remoteElapsed": 346,
  "remoteData": "{\"pageLoadTime\":344,\"fetchTime\":9,\"connectTime\":0,\"requestTime\":6,\"responseTime\":6,\"renderTime\":325,\"navigationStart\":1513706133948,\"unloadEventStart\":1513706133962,\"unloadEventEnd\":1513706133962,\"redirectStart\":0,\"redirectEnd\":0,\"fetchStart\":1513706133952,\"domainLookupStart\":1513706133952,\"domainLookupEnd\":1513706133952,\"connectStart\":1513706133952,\"connectEnd\":1513706133952,\"secureConnectionStart\":0,\"requestStart\":1513706133955,\"responseStart\":1513706133955,\"responseEnd\":1513706133961,\"domLoading\":1513706133967,\"domInteractive\":1513706134222,\"domContentLoadedEventStart\":1513706134222,\"domContentLoadedEventEnd\":1513706134222,\"domComplete\":1513706134292,\"loadEventStart\":1513706134292}"
}

Remote events

Remote events require the client-side profiling script. If you are using the ts_devserver bazel rule, this script will automatically be included in the development bundle so you don't have to worry about including it. If you're not using ts_devserver for development mode, you can include the following script tag to pull in the client-side profiling script:

<script src="http://localhost:30000/profiler.js"></script>

The script is served by iBazel on port 30000 by default. If port 30000 is not available, iBazel will attempt to find another available port between 30001 and 30099.

Remote events in the profiler script are sent using the Beacon API. This API is available in Chrome 39, Firefox 31, Edge and Opera 26. It is not available in Internet Explorer or Safari. Browser compatability can be checked here.

If your browser does not support the Beacon API, you will see the following error in the console when including the profiler script: iBazel profiler disabled because Beacon API is not available.

Custom remote events

When the profiler script is loaded, a window.IBazelProfileEvent(eventType, data) public API is made available for generating custom remote events. This function sends a custom REMOTE_EVENT to the iBazel profiler log.

Param Type Description
eventType string The event type that ends up in the 'remoteType' attribute of the REMOTE_EVENT.
data any Optional data associated with the event. This is converted to a string. If it is an object it will be converted to escaped JSON in the profiler log.

Additional notes

Termination

SIGINT has to be sent twice to kill ibazel: once to terminate the subprocess, and the second time for ibazel itself. Also, ibazel will exit on its own when a bazel query fails, but it will stay alive when a build, test, or run fails. We use an exit code of 3 for a signal termination, and 4 for a query failure. These codes are not an API and may change at any point.

What about the --watchfs flag?

Bazel has a flag called --watchfs which, according to the bazel command-line help does:

If true, Bazel tries to use the operating system's file watch service for local changes instead of scanning every file for a change

Unfortunately, this option does not rebuild the project on save like the Bazel watcher does, but instead queries the file system for a list of files that have been invalidated since last build and will require reinspection by the Bazel server.

Big thanks

  • Google for cross-platform build/test CI instances.
  • Sauce Labs for cross-browser testing.

Copyright 2017 The Bazel Authors. All right reserved.

More Repositories

1

bazel

a fast, scalable, multi-language and extensible build system
Java
22,794
star
2

starlark

Starlark Language
Starlark
2,435
star
3

bazelisk

A user-friendly launcher for Bazel.
Go
2,060
star
4

rules_go

Go rules for Bazel
Go
1,356
star
5

bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Go
1,156
star
6

rules_docker

Rules for building and handling Docker images with Bazel
Starlark
1,074
star
7

buildtools

A bazel BUILD file formatter and editor
Go
1,007
star
8

examples

Examples for Bazel
Starlark
831
star
9

intellij

IntelliJ plugin for Bazel projects
Java
762
star
10

rules_rust

Rust rules for Bazel
Starlark
660
star
11

bazel-buildfarm

Bazel remote caching and execution service
Java
650
star
12

tulsi

An Xcode Project Generator For Bazel
Swift
547
star
13

rules_python

Bazel Python Rules
Starlark
521
star
14

rules_apple

Bazel rules to build apps for Apple platforms.
Starlark
509
star
15

bazel-skylib

Common useful functions and rules for Bazel
Starlark
393
star
16

sandboxfs

A virtual file system for sandboxing
Rust
371
star
17

rules_scala

Scala rules for Bazel
Starlark
360
star
18

rules_kotlin

Bazel rules for Kotlin
Kotlin
331
star
19

remote-apis

An API for caching and execution of actions on a remote system.
Go
328
star
20

rules_swift

Bazel rules to build Swift on Apple and Linux platforms
Starlark
311
star
21

rules_k8s

This repository contains rules for interacting with Kubernetes configurations / clusters.
Starlark
289
star
22

rules_typescript

MOVED to https://github.com/bazelbuild/rules_nodejs/tree/3.x/third_party/github.com/bazelbuild/rules_typescript
TypeScript
275
star
23

continuous-integration

Bazel's Continuous Integration Setup
Python
258
star
24

bazel-central-registry

The central registry of Bazel modules for the Bzlmod external dependency system.
Starlark
247
star
25

rules_pkg

Bazel rules for creating packages of many types (zip, tar, deb, rpm, ...)
Starlark
225
star
26

rules_cc

C++ Rules for Bazel
Starlark
184
star
27

bazel-toolchains

Repository that hosts Bazel toolchain configs for remote execution and related support tools.
Go
183
star
28

rules_android

Android rules for Bazel
Starlark
175
star
29

rules_proto

Protocol buffer rules for Bazel
Starlark
163
star
30

rules_closure

Closure rules for Bazel
Java
153
star
31

vim-bazel

Vim support for Bazel
Vim Script
144
star
32

platforms

Constraint values for specifying platforms and toolchains
Starlark
108
star
33

stardoc

Stardoc: Starlark Documentation Generator
Java
107
star
34

proposals

Index of all Bazel proposals and design documents
106
star
35

rules_webtesting

Bazel rules to allow testing against a browser with WebDriver.
Go
96
star
36

apple_support

Apple support for Bazel rules
Starlark
83
star
37

emacs-bazel-mode

Emacs mode for Bazel
Emacs Lisp
80
star
38

rules_license

Starlark
77
star
39

rules_java

Java rules for Bazel
Starlark
72
star
40

reclient

Go
65
star
41

setup-bazelisk

Set up your GitHub Actions workflow with a specific version of Bazelisk
TypeScript
56
star
42

rules_sass

Sass rules for Bazel
Starlark
51
star
43

remote-apis-sdks

This repository contains client libraries for the Remote Execution API https://github.com/bazelbuild/remote-apis
Go
48
star
44

bazel-federation

Starlark
47
star
45

skydoc

Documentation generator for Skylark
Python
46
star
46

migration-tooling

Migration tools for Bazel
Java
44
star
47

codelabs

Shell
39
star
48

BUILD_file_generator

Generate BUILD files for your Java files
Java
39
star
49

eclipse

Eclipse For Bazel (deprecated, see https://github.com/salesforce/bazel-eclipse instead)
Java
32
star
50

bazel-integration-testing

Framework for integration tests that call Bazel
Java
32
star
51

rules_appengine

AppEngine rules for Bazel
Starlark
30
star
52

rules_android_ndk

Starlark
29
star
53

tools_remote

Java
28
star
54

bazel-bench

Benchmarking tool for bazel
Python
27
star
55

vim-ft-bzl

Vim Script
27
star
56

rules_perl

Perl rules for Bazel
Starlark
25
star
57

tools_android

Tools for use with building Android apps with Bazel
Starlark
24
star
58

homebrew-tap

This repository contains a collection of Homebrew (aka, Brew) "formulae" for Bazel
24
star
59

rules_d

D rules for Bazel
Starlark
24
star
60

bazel-blog

Content of the Bazel blog
HTML
20
star
61

bzlmod

Go
20
star
62

rules_testing

Starlark testing framework and utility libraries
Starlark
20
star
63

rules_gwt

Bazel rules for GWT
Starlark
19
star
64

bazel-website

Website for Bazel, a fast, scalable, multi-language and extensible build system
HTML
17
star
65

bazelcon

Artifacts from BazelCon
15
star
66

java_tools

Python
12
star
67

rules_groovy

Groovy rules for Bazel
Starlark
11
star
68

rules_postcss

PostCSS rules for Bazel
Starlark
10
star
69

rules_platform

Starlark
8
star
70

bazel_metrics

Python
7
star
71

community

Resources for community management efforts, such as SIGs
6
star
72

rules_angular

OBSOLETE see https://github.com/angular/angular/tree/master/packages/bazel
Python
5
star
73

gmaven_rules

This repository is deprecated. Please instead use https://github.com/bazelbuild/rules_jvm_external
Python
3
star
74

rules_utp

Starlark
2
star
75

.allstar

1
star
76

.github

1
star
77

bazel-worker-api

Java
1
star
78

build-event-stream

1
star
79

rules_sh

1
star