• Stars
    star
    354
  • Rank 116,246 (Top 3 %)
  • Language Starlark
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated 10 days ago

Reviews

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

Repository Details

Scala rules for Bazel

Scala Rules for Bazel

Build status

Where to get help

Overview

Bazel is a tool for building and testing software and can handle large, multi-language projects at scale.

This project defines core build rules for Scala that can be used to build, test, and package Scala projects.

Rules

Getting started

  1. Install Bazel, see the compatibility table.
  2. Add the following to your WORKSPACE file and update versions with their sha256s if needed:
# WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "bazel_skylib",
    sha256 = "b8a1527901774180afc798aeb28c4634bdccf19c4d98e7bdd1ce79d1fe9aaad7",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.1/bazel-skylib-1.4.1.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.1/bazel-skylib-1.4.1.tar.gz",
    ],
)

# See https://github.com/bazelbuild/rules_scala/releases for up to date version information.
http_archive(
    name = "io_bazel_rules_scala",
    sha256 = "71324bef9bc5a885097e2960d5b8effed63399b55572219919d25f43f468c716",
    strip_prefix = "rules_scala-6.2.1",
    url = "https://github.com/bazelbuild/rules_scala/releases/download/v6.2.1/rules_scala-v6.2.1.tar.gz",
)

load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")
# Stores Scala version and other configuration
# 2.12 is a default version, other versions can be use by passing them explicitly:
# scala_config(scala_version = "2.11.12")
# Scala 3 requires extras...
#   3.2 should be supported on master. Please note that Scala artifacts for version (3.2.2) are not defined in
#   Rules Scala, they need to be provided by your WORKSPACE. You can use external loader like
#   https://github.com/bazelbuild/rules_jvm_external
scala_config()

load("@io_bazel_rules_scala//scala:scala.bzl", "rules_scala_setup", "rules_scala_toolchain_deps_repositories")

# loads other rules Rules Scala depends on 
rules_scala_setup()

# Loads Maven deps like Scala compiler and standard libs. On production projects you should consider 
# defining a custom deps toolchains to use your project libs instead 
rules_scala_toolchain_deps_repositories(fetch_sources = True)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")
scala_register_toolchains()

# optional: setup ScalaTest toolchain and dependencies
load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")
scalatest_repositories()
scalatest_toolchain()

This will load the rules_scala repository at the commit sha rules_scala_version into your Bazel project and register a scala_toolchain at the default Scala version (2.12.18)

Then in your BUILD file just add the following so the rules will be available:

load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library", "scala_binary", "scala_test")

You may wish to have these rules loaded by default using bazel's prelude. You can add the above to the file tools/build_rules/prelude_bazel in your repo (don't forget to have a, possibly empty, BUILD file there) and then it will be automatically prepended to every BUILD file in the workspace.

To run with a persistent worker (much faster), you need to add

build --strategy=Scalac=worker
build --worker_sandboxing

to your command line, or to enable by default for building/testing add it to your .bazelrc.

Coverage support

It will produce several .dat files with results for your targets.

You can also add more options to receive a combined coverage report:

bazel coverage \
  --combined_report=lcov \
  --coverage_report_generator="@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" \
  //...

This should produce a single bazel-out/_coverage/_coverage_report.dat from all coverage files that are generated.

You can extract information from your coverage reports with lcov:

# For a summary:
lcov --summary your-coverage-report.dat
# For details:
lcov --list your-coverage-report.dat

If you prefer an HTML report, then you can use genhtml provided also by the lcov package.

Coverage support has been only tested with ScalaTest.

Please check coverage.md for more details on coverage support.

Selecting Scala version

With toolchains

Rules scala supports the last two released minor versions for each of Scala 2.11, 2.12, 2.13. Previous minor versions may work but are supported only on a best effort basis.

To configure Scala version you must call scala_config(scala_version = "2.xx.xx") and configure dependencies by declaring scala_toolchain. For a quick start you can use scala_repositories() and scala_register_toolchains(), which have dependency providers configured for 2.11.12, 2.12.18 and 2.13.12 versions.

# WORKSPACE
load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config")
scala_config(scala_version = "2.13.12")

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")
scala_register_toolchains()

Note: Toolchains are a more flexible way to configure dependencies, so you should prefer that way. Please also note, that the overriden_artifacts parameter is likely to be removed in the future.

Bazel compatible versions

minimal bazel version rules_scala gitsha
5.3.1 HEAD
4.1.0 a40063ef97688f056824b22b9e49fae6efd1df0f
3.5.0 0f55e9f8cff6494bbff7cd57048d732286a520f5
2.0.0 116709091e5e1aab3346184217b565f4cb7ba4eb
1.1.0 d681a952da74fc61a49fc3167b03548f42fc5dde
0.28.1 bd0c388125e12f4f173648fc4474f73160a5c628
0.23.x ca655e5a330cbf1d66ce1d9baa63522752ec6011
0.22.x f3113fb6e9e35cb8f441d2305542026d98afc0a2
0.16.x f3113fb6e9e35cb8f441d2305542026d98afc0a2
0.15.x 3b9ab9be31ac217d3337c709cb6bfeb89c8dcbb1
0.14.x 3b9ab9be31ac217d3337c709cb6bfeb89c8dcbb1
0.13.x 3c987b6ae8a453886759b132f1572c0efca2eca2

See the configuration file for the exact versions verified with the continuous-integration builds.

Breaking changes

If you're upgrading to a version containing one of these commits, you may encounter a breaking change where there was previously undefined behavior.

  • 929b318 on 2020-01-30: Fixed a bug in the JMH benchmark build that was allowing build failures to creep through. Previously you were able to build a benchmark suite with JMH build errors. Running the benchmark suite would only run the successfully-built benchmarks.

Usage with bazel-deps

Bazel-deps allows you to generate bazel dependencies transitively for maven artifacts. Generally we don't want bazel-deps to fetch scala artifacts from maven but instead use the ones we get from calling scala_repositories. The artifacts can be overridden in the dependencies file used by bazel-deps:

replacements:
  org.scala-lang:
    scala-library:
      lang: scala/unmangled
      target: "@io_bazel_rules_scala_scala_library//:io_bazel_rules_scala_scala_library"
    scala-reflect:
      lang: scala/unmangled
      target: "@io_bazel_rules_scala_scala_reflect//:io_bazel_rules_scala_scala_reflect"
    scala-compiler:
      lang: scala/unmangled
      target: "@io_bazel_rules_scala_scala_compiler//:io_bazel_rules_scala_scala_compiler"

  org.scala-lang.modules:
    scala-parser-combinators:
      lang: scala
      target:
        "@io_bazel_rules_scala_scala_parser_combinators//:io_bazel_rules_scala_scala_parser_combinators"
    scala-xml:
      lang: scala
      target:
        "@io_bazel_rules_scala_scala_xml//:io_bazel_rules_scala_scala_xml"

Publishing to Maven repository

Go here

Dependency Tracking

Rules Scala supports multiple dependency modes including strict and unused dependency tracking. See Dependency Tracking for more info.

Advanced configurable rules

To make the ruleset more flexible and configurable, we introduce a phase architecture. By using a phase architecture, where rule implementations are defined as a list of phases that are executed sequentially, functionality can easily be added (or modified) by adding (or swapping) phases.

Phases provide 3 major benefits:

  • Consumers are able to configure the rules to their specific use cases by defining new phases within their workspace without impacting other consumers.
  • Contributors are able to implement new functionalities by creating additional default phases.
  • Phases give us more clear idea what steps are shared across rules.

See Customizable Phase for more info.

Phase extensions

Building from source

Setup bazel: We recommend using Bazelisk as your default bazel binary

Test & Build:

bash test_all.sh

You can also use:

bazel test //test/...

Note bazel test //... will not work since we have a sub-folder on the root folder which is meant to be used in a failure scenario in the integration tests. Similarly to only build you should use bazel build //src/... due to that folder.

Updates

This section contains a list of updates that might require action from the user.

Contributing

See CONTRIBUTING.md for more info.

Adopters

Here's a (non-exhaustive) list of companies that use rules_scala in production. Don't see yours? You can add it in a PR!

More Repositories

1

bazel

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

starlark

Starlark Language
Starlark
2,242
star
3

bazelisk

A user-friendly launcher for Bazel.
Go
1,899
star
4

rules_go

Go rules for Bazel
Go
1,335
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,132
star
6

rules_docker

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

buildtools

A bazel BUILD file formatter and editor
Go
975
star
8

examples

Examples for Bazel
Starlark
784
star
9

intellij

IntelliJ plugin for Bazel projects
Java
748
star
10

rules_nodejs

NodeJS toolchain for Bazel.
Starlark
719
star
11

rules_foreign_cc

Build rules for interfacing with "foreign" (non-Bazel) build systems (CMake, configure-make, GNU Make, boost, ninja, Meson)
Starlark
639
star
12

bazel-buildfarm

Bazel remote caching and execution service
Java
624
star
13

rules_rust

Rust rules for Bazel
Starlark
613
star
14

tulsi

An Xcode Project Generator For Bazel
Swift
547
star
15

rules_python

Bazel Python Rules
Starlark
504
star
16

rules_apple

Bazel rules to build apps for Apple platforms.
Starlark
477
star
17

bazel-watcher

Tools for building Bazel targets when source files change.
Go
414
star
18

sandboxfs

A virtual file system for sandboxing
Rust
367
star
19

bazel-skylib

Common useful functions and rules for Bazel
Starlark
366
star
20

rules_kotlin

Bazel rules for Kotlin
Kotlin
324
star
21

rules_jvm_external

Bazel rules to resolve, fetch and export Maven artifacts
Starlark
309
star
22

remote-apis

An API for caching and execution of actions on a remote system.
Starlark
300
star
23

rules_swift

Bazel rules to build Swift on Apple and Linux platforms
Starlark
298
star
24

rules_k8s

This repository contains rules for interacting with Kubernetes configurations / clusters.
Starlark
288
star
25

rules_typescript

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

continuous-integration

Bazel's Continuous Integration Setup
Python
249
star
27

vscode-bazel

Bazel support for Visual Studio Code
TypeScript
226
star
28

bazel-central-registry

The central registry of Bazel modules for the Bzlmod external dependency system.
Starlark
220
star
29

rules_pkg

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

bazel-toolchains

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

rules_dotnet

.NET rules for Bazel
Starlark
180
star
32

rules_android

Android rules for Bazel
Starlark
174
star
33

rules_cc

C++ Rules for Bazel
Starlark
166
star
34

rules_proto

Protocol buffer rules for Bazel
Starlark
158
star
35

rules_closure

Closure rules for Bazel
Java
151
star
36

vim-bazel

Vim support for Bazel
Vim Script
138
star
37

platforms

Constraint values for specifying platforms and toolchains
Starlark
102
star
38

proposals

Index of all Bazel proposals and design documents
102
star
39

stardoc

Stardoc: Starlark Documentation Generator
Java
101
star
40

rules_webtesting

Bazel rules to allow testing against a browser with WebDriver.
Go
94
star
41

rules_fuzzing

Bazel Starlark extensions for defining fuzz tests in Bazel projects
Starlark
79
star
42

emacs-bazel-mode

Emacs mode for Bazel
Emacs Lisp
75
star
43

rules_license

Starlark
70
star
44

apple_support

Apple support for Bazel rules
Starlark
70
star
45

rules_jsonnet

Jsonnet rules for Bazel
Starlark
64
star
46

rules_java

Java rules for Bazel
Starlark
64
star
47

setup-bazelisk

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

rules_sass

Sass rules for Bazel
Starlark
50
star
49

bazel-federation

Starlark
47
star
50

skydoc

Documentation generator for Skylark
Python
46
star
51

reclient

Go
46
star
52

remote-apis-sdks

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

migration-tooling

Migration tools for Bazel
Java
44
star
54

BUILD_file_generator

Generate BUILD files for your Java files
Java
39
star
55

codelabs

Shell
37
star
56

eclipse

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

bazel-integration-testing

Framework for integration tests that call Bazel
Java
32
star
58

rules_android_ndk

Starlark
29
star
59

rules_appengine

AppEngine rules for Bazel
Starlark
29
star
60

tools_remote

Java
28
star
61

vim-ft-bzl

Vim Script
27
star
62

bazel-bench

Benchmarking tool for bazel
Python
26
star
63

tools_android

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

homebrew-tap

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

rules_d

D rules for Bazel
Starlark
24
star
66

rules_perl

Perl rules for Bazel
Starlark
23
star
67

bzlmod

Go
20
star
68

bazel-blog

Content of the Bazel blog
HTML
19
star
69

rules_gwt

Bazel rules for GWT
Starlark
19
star
70

rules_testing

Starlark testing framework and utility libraries
Starlark
19
star
71

bazel-website

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

bazelcon

Artifacts from BazelCon
15
star
73

java_tools

Python
12
star
74

rules_groovy

Groovy rules for Bazel
Starlark
11
star
75

rules_postcss

PostCSS rules for Bazel
Starlark
10
star
76

bazel_metrics

Python
7
star
77

community

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

rules_angular

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

gmaven_rules

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

rules_platform

Starlark
3
star
81

rules_utp

Starlark
2
star
82

.allstar

1
star
83

.github

1
star