Get WORKSPACE root in Bazel BUILD

Viewed 6629

I'm using rules_protobuf to build Python language bindings for my helloworld.proto file. My helloworld.proto imports wrappers.proto.

syntax = "proto3";

package main;

import "google/protobuf/wrappers.proto";

My BUILD file

load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_compile")

# Wrapper around proto_compile.
# https://github.com/pubref/rules_protobuf/blob/master/protobuf/internal/proto_compile.bzl

py_proto_compile(
    name = "py",
    with_grpc = True,
    protos = ["helloworld.proto"],
    imports = ["/usr/local/home/username/myproject/include"]
)

The wrappers.proto file is located in the directory

/usr/local/home/username/myproject/include

The Bazel rule py_proto_compile is defined by rules_protobuf and documented in the README.md. imports is defined as:

  • Name: imports
  • Type: string_list`
  • Description: Optional paths to be passed as -I arguments to the protoc tool.
  • Default: []

My BUILD rules works, however I've hard-coded the location of of wrappers.proto with:

imports = ["/usr/local/home/username/myproject/include"]

Bazel doesn't appear to have any predefined Make variables that reference my WORKSPACE root. Ideally, I'd like to do something like this:

imports = ["$WORKSPACE_ROOT"/include"]

1 Answers
Related