A practical example of shared libraries in a monorepo

One of the most powerful aspects of working in a monorepo is the ability to share code between packages/teams/hierarchies. In this post I will try to explain a very simple real world scenario

Example Scenario

Imagine you want to develop a library to show the file sizes in megabytes which you feel might be useful to other parts of your monorepo. The library accepts the size as Integer (ex: 2048 bytes) and can return a humanized string (ex: 2 MB). To add some quality assurance, we will also write a test for the same.

How does Bazel enable sharing code?

From the above scenario we are aware that we need to develop this function as a shared library which then be imported by another package for usage. Bazel makes this extremely simple by allowing us to define the function in a library and export it other services that will need it. As explained in my earlier post linked at the bottom of this post, we can also control which other libraries can be allowed to import it for usage as well.

Let's get coding

For code organization purpose, we will have a libraries directory at the root of our workspace with a child directory called humanize_filesize which is where we will write our library code.

Let's write some very elementary Go code in humanize_filesize.go

package humanize_filesize

import "fmt"

// GetHumanizedFilesize takes size_in_bytes as an int32 pointer and returns the size in megabytes.
func GetHumanizedFilesize(size_in_bytes *int32) string {
    if size_in_bytes != nil {
        size_in_megabytes := float64(*size_in_bytes) / (1024 * 1024)
        return fmt.Sprintf("%.4f MB", size_in_megabytes)
    }
    return "0 MB"
}

This code simply takes an int32 as an input and returns a computed readable megabyte string to 4 decimal precision

This function is definitely not comprehensive and can definitely be improved, but that's not the point of this exercise.

Also assert that our logic is working as intended, we will add a very elementary test alongside our go code in a file called humanize_filesize_test.go

package humanize_filesize

import (
    "testing"
)

func TestHumanizeFilesize(t *testing.T) {
    tests := []struct {
        name          string
        size_in_bytes *int32
        expected      string
    }{
        {
            name:          "nil bytes",
            size_in_bytes: nil,
            expected:      "0 MB",
        },
        {
            name:          "2048 bytes",
            size_in_bytes: int32Ptr(2048),
            expected:      "0.0020 MB",
        },
        {
            name:          "0 bytes",
            size_in_bytes: int32Ptr(0),
            expected:      "0.0000 MB",
        },
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            result := GetHumanizedFilesize(tt.size_in_bytes)
            if result != tt.expected {
                t.Errorf("expected %s, got %s", tt.expected, result)
            }
        })
    }
}

func int32Ptr(n int32) *int32 {
    return &n
}

A very simple test with basic tests for nil, int32 and 0 as inputs

Now comes the juicy part of how to export this function so that this can be imported within other packages or services. This is where we have to define the BUILD.bazel file

load("@rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "humanize_filesize",
    srcs = ["humanize_filesize.go"],
    importpath = "basil/libraries/humanize_filesize",
    visibility = ["//visibility:public"],
)

go_test(
    name = "humanize_filesize_test",
    srcs = ["humanize_filesize_test.go"],
    embed = [":humanize_filesize"],
)

In here we are defining two main rules. One for the actual library and one for the test file that we wrote.

The go_library defines that the target humanize_filesize uses humanize_filesize.go as one of its sources which can be imported by the path specified in importpath and it is visible publicly within the workspace for other packages to import. We will learn how to control the visibility in a future post.

The go_test defines a test target which embeds the code from the output of go_library.

At this point we should be able to test the library by running our test suite as following

bazel build //... && bazel run //libraries/humanize_filesize:humanize_filesize_test

You should be able to see the test output as following indicating that all the tests have passed.

INFO: Analyzed target //libraries/humanize_filesize:humanize_filesize_test (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //libraries/humanize_filesize:humanize_filesize_test up-to-date:
  bazel-bin/libraries/humanize_filesize/humanize_filesize_test_/humanize_filesize_test
INFO: Elapsed time: 0.392s, Critical Path: 0.24s
INFO: 5 processes: 1 internal, 4 darwin-sandbox.
INFO: Build completed successfully, 5 total actions
INFO: Running command line: external/bazel_tools/tools/test/test-setup.sh libraries/humanize_filesize/humanize_filesize_test_/humanize_filesize_test
exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //libraries/humanize_filesize:humanize_filesize_test
-----------------------------------------------------------------------------
PASS

🎉 Wohoo!!! 🥳 Now we know that our library is working as intended.

Now let's use this library in a service service1 within a services directory that we will create at the root of the workspace with the following go code and BUILD.bazel file

service1.go

package main

import (
    "basil/libraries/humanize_filesize"
    "fmt"
    "math/rand"
)

func main() {
    v := rand.Int31n(1000000)
    fmt.Printf(`%d bytes = %s\n`, v, humanize_filesize.GetHumanizedFilesize(&v))
}

BUILD.bazel

load("@rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
    name = "service1_lib",
    srcs = ["service1.go"],
    importpath = "basil/services/service1",
    visibility = ["//visibility:private"],
    deps = ["//libraries/humanize_filesize"],
)

go_binary(
    name = "service1",
    embed = [":service1_lib"],
    visibility = ["//visibility:public"],
)

The go code is pretty simple which imports our library that we declared earlier and uses the GetHumanizedFilesize function from our library and passes a random integer value and prints the output.

Now when execute bazel build //services/service1 , bazel will resolve all dependencies for our target including the library that we developed and build them.

service1 can now be executed using bazel run //services/service1 since we have only one binary target defined. If you have more than one binary targets, ex: serviceX, you can execute that using bazel run //services/service1:serviceX. By default when not specifying a target, bazel will always try to find a binary target with the same name as the directory and run that.

So... there you go. We have made your first shared library that can be used by other parts of our monorepo.

All code for this example can be found at https://github.com/nixclix/basil/pull/3/commits/61c673b8757860bd5e60eb2ab6c35f3f4da78c87

If you like the content of this post feel free to share it. Also, please subscribe and leave comments on what you think about this post and if there are things that you would like to see me improving on.

0
Subscribe to my newsletter

Read articles from Nikhil Dev Chunchu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nikhil Dev Chunchu
Nikhil Dev Chunchu