How to find the workspace path for an external workspace in Skylark?

2,315 views
Skip to first unread message

Alex Eagle

unread,
Jul 11, 2018, 5:52:17 PM7/11/18
to bazel-...@googlegroups.com
My WORKSPACE file declares a local_repository

local_repository(
  name = "foo",
  path = "bar")

in some logic in my rules, it's necessary to teach a program how to resolve execroot paths (like bazel-out/host/bin/external/foo/some/path) to the original location (bar/some/path)

Does anyone know a trick, given a Label or a workspace name, to find out how the workspace was declared and if it has a local path, what that path is?

Thanks!
-Alex

László Csomor

unread,
Jul 12, 2018, 3:25:55 AM7/12/18
to Alex Eagle, bazel-discuss
Hi Alex,

I haven't found any way to obtain this info. Bazel can tell variations of the path via "foo" (see below), but not via "bar". (See https://docs.bazel.build/versions/master/skylark/lib/File.html)


Given the directory structure:

/tmp/x/repo/
    sub1/
        foo.bzl
        BUILD
    WORKSPACE
/tmp/x/bar/
    sub2/
        BUILD
    WORKSPACE
  # empty



And the sources:
  $ cat /tmp/x/repo/WORKSPACE
local_repository(
    name = "foo",
    path = "/tmp/x/bar",
)


  $ cat /tmp/x/repo/sub1/foo.bzl
def _impl(ctx):
  src = ctx.file.src
  print("basename=(%s)" % src.basename)
  print("dirname=(%s)" % src.dirname)
  print("extension=(%s)" % src.extension)
  print("is_source=(%s)" % src.is_source)
  print("owner=(%s)" % src.owner)
  print("path=(%s)" % src.path)
  print("root=(%s)" % src.root)
  print("root.path=(%s)" % src.root.path)
  print("short_path=(%s)" % src.short_path)
  ctx.actions.do_nothing(mnemonic="foo")

foo = rule(
    implementation = _impl,
    attrs = {"src": attr.label(allow_files=True, single_file=True)},
)


  $ cat /tmp/x/repo/sub1/BUILD
load(":foo.bzl", "foo")
foo(name = "foo", src = "@foo//sub2")


  $ cat /tmp/x/bar/sub2/BUILD
genrule(
    name = "sub2",
    outs = ["sub2.txt"],
    cmd = "echo $@ > $@",
    visibility = ["//visibility:public"],
)

The output is:

  $ bazel build //sub1:foo |& grep DEBUG
DEBUG: /tmp/x/repo/sub1/foo.bzl:3:3: basename=(sub2.txt)
DEBUG: /tmp/x/repo/sub1/foo.bzl:4:3: dirname=(bazel-out/k8-fastbuild/genfiles/external/foo/sub2)
DEBUG: /tmp/x/repo/sub1/foo.bzl:5:3: extension=(txt)
DEBUG: /tmp/x/repo/sub1/foo.bzl:6:3: is_source=(False)
DEBUG: /tmp/x/repo/sub1/foo.bzl:7:3: owner=(@foo//sub2:sub2)
DEBUG: /tmp/x/repo/sub1/foo.bzl:8:3: path=(bazel-out/k8-fastbuild/genfiles/external/foo/sub2/sub2.txt)
DEBUG: /tmp/x/repo/sub1/foo.bzl:9:3: root=(<derived root>)
DEBUG: /tmp/x/repo/sub1/foo.bzl:10:3: root.path=(bazel-out/k8-fastbuild/genfiles)
DEBUG: /tmp/x/repo/sub1/foo.bzl:11:3: short_path=(../foo/sub2/sub2.txt)

--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAP4t0yiRFp62Lkzb7%3DhHYQEBkviYXQ1kpCfLqotmssre40Gifw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages