obeattie
obeattie

Reputation: 3344

Derive Bazel label from source file path

Given a path to a source file within the workspace, how can I derive the label that Bazel uses to refer to the file. This seems to depend on what packages exist. For example, if I have this structure:

.
├── BUILD
├── WORKSPACE
└── src
    └── bar
        └── foo.go

Then the label for src/bar/foo.go is //src/bar/foo.go. However, if I have this structure:

.
├── BUILD
├── WORKSPACE
└── src
    ├── BUILD
    └── bar
        ├── BUILD
        └── foo.go

Then the label for the same file is //src/bar:foo.go.

Is there a way to get Bazel to tell me what the label that identifies a file is, or must I to derive it based on the presence/absence of BUILD files at various levels of the workspace tree?

Upvotes: 1

Views: 1970

Answers (1)

Jin
Jin

Reputation: 13463

If a valid label exists for path/to/file.txt, running bazel query path/to/file.txt will return you the absolute label.

e.g. for the first example:

$ bazel query src/bar/foo.go
//:src/bar/foo.go

and for the second example:

$ bazel query src/bar/foo.go
//src/bar:foo.go

If the file is not referenced in any rule (filegroup, exports_files, etc) in any BUILD file, it will not have a label.

Upvotes: 4

Related Questions