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?
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.
Related
I am using Bazel with Golang, but the question is no Go-specific. I have a common go directory structure:
cmd/
├── mycommand/
│ ├── BUILD.bazel
│ ├── main.go
│ └── somefolder
│ └── other.go
├── othercommand/
│ ├── BUILD.bazel
│ └── main.go
pkg/
└── mypackage/
├── BUILD.bazel
└── init.go
BUILD.bazel
WORKSPACE
... and I'd like to reference targets under the cmd folder. I have a bazel query that will give me the list of those targets:
bazel query 'kind("go_binary", deps(//cmd/...))'
//cmd/mycommand:mycommand
//cmd/othercommand:othercommand
The question: How can I include this query in a BUILD.bazel file, something like the following:
pkg_tar(
name = "release",
srcs = kind("go_binary", deps(//cmd/...)),
mode = "0644",
)
...which gives
ERROR: /some/path/BUILD.bazel:10:12: name 'kind' is not defined
ERROR: /some/path/BUILD.bazel:10:30: name 'deps' is not defined
Build targets need to be statically referenced in BUILD files, so embedding queries as inputs to rule attributes does not work.
However, there are a couple of ways to dynamically generate targets to be used statically in the BUILD files:
1) Run a tool that generates a BUILD file before running Bazel. rules_go's Gazelle is a good example.
2) Write a repository rule that invokes non-hermetic tools to dynamically generate targets that your BUILD files can depend on.
Note that you may come across the genquery rule, which does let you perform a query on targets, but the rule outputs to a file during Bazel's execution phase, and not a Starlark list that can ingested into other rules' attributes during the analysis phase, which happens before the execution phase.
I have a directory structure that looks like this:
some-root/
└── my-stuff/
├── BUILD
├── foo/
│ └── BUILD
├── bar/
│ └── BUILD
└── baz/
└── BUILD
I'd like to have a target like //some-root/my-stuff:update which runs all of //some-root/my-stuff/foo:update, //some-root/my-stuff/bar:update, //some-root/my-stuff/baz:update.
I can do this by listing each target as a dependency. However, if I have many of these and I want to be able to add more it becomes a pain (it's easy to add a bunch of subdirectories and miss adding one to the parent BUILD file).
Is there a way to do a wildcard labels or otherwise discover labels from file paths? I'm able to do bazel test //some-root/my-stuff/... to run all tests under a path, but I can't seem to use that pattern inside of a BUILD file and what I'd want is more like bazel run //some-root/my-stuff/...:update which doesn't work either.
You can get all labels with the name update from the command line:
bazel query "attr(name, '^update$', //...)"
and take the output of query and manually edit your dependencies.
But unfortunately you can not put this into a genquery rule (which would generate the list of targets to depend on), because
queries containing wildcard target specifications (e.g. //pkg:* or //pkg:all) are not allowed
I'm trying to make bazel build a jar and an so file for a flutter project but every time I type 'bazel build' into the command prompt I keep getting
ERROR: The 'build' command is only supported from within a workspace (below a directory having a WORKSPACE file).
See documentation at https://docs.bazel.build/versions/master/build-ref.html#workspace
I've read some documentation it seems like the solution is to create a blank file called 'WORKSPACE' but I don't understand where this file is supposed to be stored. here's a link to the documentation I read https://docs.bazel.build/versions/2.0.0/tutorial/java.html
thanks in advance!
WORKSPACE file goes to the root of your workspace (source). It's the top directory for all your build packages and start of there absolute path you'd refer too with //. For instance if you had a tree like this:
.
├── BUILD
├── a_source_file
├── package1
│ ├── BUILD
│ └── other_source
└── package2
├── BUILD
└── another_source
You would construct your workspace where all your packages converge (root they share) as:
.
├── BUILD
├── WORKSPACE
├── a_source_file
├── package1
│ ├── BUILD
│ └── other_source
└── package2
├── BUILD
└── another_source
And your build targets could then be for instance: //:a_build_target or //package2:another_target.
Problem: I have a one single repository where I have to walk through the repo to find a specific Jenkinsfile to run the pipeline. Note that I want to define the path to this Jenkinsfile explicity so I thought about having a jenkinsfilePath.yml in root directory of the repo, read the yaml, change directory and run Jenkinfile from the path. The folder structure is as follows:
testingSingleRepo
├── Jenkinsfile
├── feature_flagging
│ ├── Jenkinsfile
│ ├── __init__.py
│ ├── src
│ └── tests
└── jenkinsfilePath.yml
I am having issue running Jenkinsfile inside feature_flagging from the root Jenkinfile in testingSingleRepo. I was successful in changing directory to the folder feature_flagging by using dir. After googling a lot with similar questions, I came across the function build but I could not make that work. Any suggestions/solutions?
Calling a Jenkinsfile from a main pipeline, we can use load
load 'feature_flagging/Jenkinsfile'
So after looking around, I have decided to go with another approach. I have decided that I will have a master Jenkinfile in the root where I will have a generic pipeline setup. It will read the yaml file, change directory and execute shell scripts inside Jenkins/ folder accordingly. The folder will consist of generic scripts that reflects to the root Jenkins pipeline such as setup.sh,test.sh, deploy.sh etc. The folder structure will look something like below:
testingSingleRepo
├── Jenkinsfile
├── feature_flagging
│ ├── Jenkins/
│ ├── __init__.py
│ ├── src
│ └── tests
└── jenkinsfilePath.yml
Our monolith repo looks something like this:
├── bin
└── ...
└── source
├── foo
├── BUILD
└── ...
└── bar
├── BUILD
└── ...
└── ...
We have thousands of BUILD files producing thousands of targets into the bin directory.
Now that we have all these compiled binaries in the bin directory, it would be nice to have a method for deriving which BUILD file put it there.
I don't want to hand-write a tool that recursively parses all of the BUILD file's build rules and prints the answer. Is this something that can be achieved with bazel query?
Most of our build rules are pretty typical:
cc_binary(
name="foo",
...
)
py_binary(
name="bar.py",
...
)
The end goal would be to run something like:
where-source ./bin/foo
./source/foo/BUILD created ./bin/foo
If you trim off the bin/ (I assume you're using --symlink_prefix?), you can use bazel query:
# Where did bin/foo/bar/baz get defined?
$ bazel query foo/bar/baz
//foo/bar:baz
Or, to get detailed info about the location:
$ bazel query --output=location foo/bar/baz
/home/kchodorow/gitroot/proj/source/foo/bar/BUILD:3:1: go_binary rule //foo/bar:baz
Or, to get the definition of the target itself:
$ bazel query --output=build foo/bar/baz
go_binary(
name = "baz",
srcs = ["//foo/bar/baz.go"],
)
See What build rule contains file src/main/java/com/example/cache/LRUCache.java as a source? and the rest of that page for more examples of bazel query.