When to prefix a BUILD file (*.BUILD) in Bazel - bazel

In its C++ unit testing tutorial, Bazel suggests adding a root level gtest.BUILD file to the workspace root in order to properly integrate Google Test into the test project.
https://docs.bazel.build/versions/master/cpp-use-cases.html
Why would one create a new BUILD file and add gtest prefix to it rather than adding a new build rule to an existing BUILD file in the workspace? Is it just a minor style preference?

Because if you added a BUILD file somewhere in the workspace (e.g. under //third_party/gtest/BUILD) then that file would create a package there.
Then, if you had targets declared in that BUILD file, would their files exist under //third_party/gtest, or would they exist in the zip file that the http_archive downloads? If the former, then there's no need for a http_archive because the files are already in the source tree; if the latter, then the BUILD file references non-existent files in its own package. Both scenarios are flawed.
Better to call gtest's BUILD-file-to-be something that doesn't create a package, but that's descriptive of its purpose.
The build_file attribute of http_archive can reference any file, there's no requirement of the name. The name gtest.BUILD is mostly stylistic, yes, but it also avoids creating a package where it shouldn't. You could say it's an "inactive" BUILD file that will be "active" when Bazel downloads the http_archive, extracts it somewhere, and creates in that directory a symlink called BUILD which points to gtest.BUILD.
Another advantage of having such "inactive" BUILD files is that you can have multiple of them within one package, for multiple http_archives.

Related

How can I stop bazel from creating an entire copy of my project's source files?

I have a simple Java project in a directory called java-fun. Bazel is installed and runs correctly. But whenever it runs, it generates a directory called bazel-java-fun that contains copies of all the src/.../*.java files from my root directory java-fun.
Now my IDE displays a conflicting class name error: Duplicate class found in the file '/Users/traviscramer/java-fun/bazel-java-fun/src/main/Main.java' for every single Java class.
Is there a way to configure bazel so that it doesn't create this bazel-<project name> directory in my workspace?
Create a .bazelrc in your WORKSPACE.
Add to .bazelrc:
build --symlink_prefix=/ # Out of source build

Can I ignore some folder (containing bazel configuration) while building the project recursively?

For some reasons, practical or not, rxjs npm package stores BAZEL.build configuration in the package, so when I'm trying to build my project (which has node_modules folder) bazel tries automatically to build something that it's not supposed to build at all.
My question would be - what is canonical way of ignoring some specific folder while building bazel project recursively?
The only way to achieve what I'm looking for that I know of is to point to it explicitly in the command line
bazel build //... --deleted_packages=node_modules/rxjs/src (see user manual)
But I don't want to type this every time.
Bazel recently added a feature for ignoring folders (similar to gitignore).
Simply add node_modules to the .bazelignore file in the root of your project.
Yes, this is expressible as a bazel target pattern:
bazel build -- //... -//node_modules/rxjs/src/...
Full documentation is available at https://docs.bazel.build/versions/master/user-manual.html#target-patterns

How can I tell Which bazel aspect outputs are still relevant

As part of our efforts to create a bazel-maven transition interop tool (that creates maven sized jars from more granular sized bazel jars),
we have written an aspect that runs on bazel build of the entire bazel repo and writes important information to txt files outputs (e.g.: jar file paths, compile deps targets and runtime deps targets, etc.)
We ran across an issue where the repo's code was changed such that some of the txt file were not written anymore. But the old txt file from previous runs (before the code change) remained!
Is there a way to know that these txt files are no longer relevant?
You should be able to run with --build_event_json_file=file.json and try to locate generated artifacts. For example we use it on ci.bazel.io to locate actual test.xml file that were generated: https://github.com/bazelbuild/continuous-integration/blob/09975cbb487a84a62ca1e43aa43e7c6fe078f058/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy#L218
The definition of the protocol can be found in build_event_stream.proto

Bazel ignore subdirectory on full build

In my repository I have some files with the name "build" (automatically generated and/or imported, spread around elsewhere from where I have my bazel build files). These seem to be interpreted by Bazel as its BUILD files, and fail the full build I try to run with bazel build //...
Is there some way I can tell Bazel in a settings configuration file to ignore certain directories altogether? Or perhaps specify the build file names as something other than BUILD, like BUILD.bazel?
Or are my options:
To banish the name build from the entire repository.
To add a gigantic --deleted_packages=<...> to every run of build.
To not use full builds, instead specifying explicit targets.
I think this is a duplicate of the two questions you linked, but to expand on what you asked about in your comment:
You don't have to rename them BUILD.bazel, my suggestion is to add an empty BUILD.bazel to those directories. So you'd end up with:
my-project/
BUILD
src/
build/
stuff-bazel-shouldn't-mess-with
BUILD.bazel # Empty
Then Bazel will check for targets in BUILD.bazel, see that there are none, and won't try to parse the build/ directory.
And there is a distressing lack of documentation about BUILD vs. BUILD.bazel, at least that I could find.

Deployment of Jar using Ant and OJDeploy for packaging ADF

I'm trying to Deploy a Jar File from an ANT script (with OjDeploy), which compiles successfully. But when I run the main project (Which has the Jar dependency), I get a huge incident, when I analysed the incident log, it says
"Caused by: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist"
I analysed both the jar files (One generated by the ANT script and the other generated from Jdev). I couldn't find any difference b/w Manifest of those two jars. The other folders are also the same, In other words, both are almost the same, or I'm not able to figure out the difference between the two jar files generated.
I know, I'm missing something, config of build.xml or something, but not able to figure out the exact problem.
Thanks,
Shiva Shankar
My guess is that the problem is not in the JAR file but rather in the project that consumes it.
Seems like the DB connection in that project is different than the one in your JAR project - which causes one of the tables that is needed in the DB not be found.
I figured out a way to solve the problem. Not sure what was the cause of this issue, but a couple of changes made both to JAR/EAR build scripts set the things right
While building JAR files:
Uncheck "Make this the project build file" while creating the ANT build file, while the "Include Packaging Tasks (uses OjDeploy)" must be checked.
Remove "nocompile" parameter from tag in the OjDeploy.
While Building EAR files:
Create EAR in the view controller project.
Go to the View Controller Project Properties-> Project Source Path -> Resources, remove the newly created build file dependency, which specifies that the build file is for the whole project and not for the individual project.
Remove "Project" parameter from tag in the OjDeploy. (Ensures to build for the whole application)
Remove "nocompile" parameter from tag in the OjDeploy.
These are the exhaustive set of changes, which I made to ensure the ANT files are building correctly, if you get any other errors after this EAR is deployed, then its definitely due to code, EO/VO or DB related.
Thanks for all the help!!
Shiva Shankar

Resources