Search code examples
erlangbazel

Erlang Hello World using Bazel as a build system


I want to build an Erlang hello world example using rules_erlang on Ubuntu 22.04.

My setup looks like this:

BUILD.bazel

load("@rules_erlang//:erlang_app.bzl", "erlang_app", "test_erlang_app")
load("@rules_erlang//:xref.bzl", "xref")
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
load("@rules_erlang//:ct.bzl", "ct_suite", "assert_suites")

APP_NAME = "hello_world"
APP_VERSION = "0.1.0"

erlang_app(
    app_name = APP_NAME,
    app_version = APP_VERSION,
)

src/hello_world.erl

-module(hello_world).
-compile(export_all).


hello() ->
    io:format("hello world~n").

WORKSPACE.bazel

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

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

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
    name = "rules_erlang",
    sha256 = "5e59800ecc786d5375951028c959c6e6275c94eff2a52f5d53ccb1ad8b2ea20a",
    strip_prefix = "rules_erlang-3.8.4",
    urls = ["https://github.com/rabbitmq/rules_erlang/archive/refs/tags/3.8.4.zip"],
)

load(
    "@rules_erlang//:rules_erlang.bzl",
    "erlang_config",
    "rules_erlang_dependencies",
)

erlang_config()

rules_erlang_dependencies()

load("@erlang_config//:defaults.bzl", "register_defaults")

register_defaults()

The code can also found here.

When I execute bazel build //... I get

ERROR: /home/vertexwahn/.cache/bazel/_bazel_vertexwahn/b5f945f94177a8ffa6ac0f7108dfc1cd/external/erlang_config/external/BUILD.bazel:12:16: Validating otp at /usr failed: (Exit 1): bash failed: error executing command /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging Erlang version mismatch (Expected UNKNOWN, found 24.2.1)

Any hints to get this working are welcome!


Solution

  • To run it, you can declare a shell rule in your BUILD.bazel, for instance:

    load("@rules_erlang//:shell.bzl", "shell")
    shell(
        name = "repl",
        deps = [":erlang_app"],
        extra_erl_args = ["-eval", "'hello_world:hello()'"],
    )
    

    and then you could bazel run :repl.

    Or, you could use an escript rule if you change hello_world.erl so that it exports main/1 instead of hello:

    load("@rules_erlang//:escript.bzl", "escript_archive")
    escript_archive(
        name = "hello_world",
        app = ":erlang_app",
    )
    

    hello_world.erl:

    -module(hello_world).
    -export([main/1]).
    
    
    main(_) ->
        io:format("hello world~n").
    

    and run with bazel run :hello_world