Referring angular.json in bazel workspace when supplying it in the root workspace is not an option

270 views Asked by At

I am trying to integrate Angular project in an existing Bazel build system. When I try to run angular build, I am getting the following error.

Workspace configuration file (angular.json, .angular.json, workspace.json, .workspace.json) cannot be found in '/home/build/.cache/bazel/_bazel_build/46b74a3c1f9a666f55591a5719d9f2ba/sandbox/linux-sandbox/1/execroot/root' or in parent directories.

Now my angular project is inside a subfolder in the root workspace and so angular is not able to find the angular json file. There is also no option to specify exact location of angular json using args when I run angular build using the architect package of angular devkit which could have potentially solved the problem.

One way, I can think of solving this is copying angular json file to the root of workspace before I run the build command. Is there any other option that I can try?

3

There are 3 answers

1
Birju Shah On BEST ANSWER

You can use chdir

You basically would pass path to that subfolder and the tool will change to that directory before running ng build. I also had to change the outDir by moving up the levels to the root directory for build to success.

For example, if chdir is lib/sub/angular then outDir would be ../../../$(@D)

0
Cameron On

Birju's answer is dead-on. But I felt like it deserved a bit of code.

I will also add, setting the output directory after using chdir is really important, otherwise you will be scratching your head because the build claims to succeed, but when you check your bazel output, nothing will appear (a silent failure of sorts). Note that in my example, it's outputPath but this variable really depends on the tool you are using, not Bazel itself.

foo/BUILD.bazel

load("@npm//@angular-devkit/architect-cli:index.bzl", "architect") # architect ruleset being generated by bazel itself. (I.e. `foo/`)

architect(
    name = "build",
    args = [
        "frontend:build",
        "--outputPath=../$(@D)", # This output argument needs to point to the wherever the workspace directory is. Otherwise, your bazel build will succeed but nothing will appear.
                               
    ],
    chdir = package_name(),      # package_name() gets the path of wherever the current build file is

    # any other arguments...
)
0
Tom On

In case anyone is trying to build Angular library by using architect, which doesn't allow outputPath. I followed this example.

In which, the chdir = package_name(), line is definitely needed as @Cameron @Birju said. The args $(@D) in the code need to be changed to the right location. Otherwise, it is located in sandbox and will be deleted.

In the example, everything runs well. But in my case, my GUI project is in the subdir1/subdir2 of the WORKSPACE dir and my lib is in the subdir1/subdir2/projects/mylib. I don't have the .bazelrc files as in the example. So I end up have to change the ${@D} to add lots of ../ in order to get out of the sandbox dir, such as "../../../../../../../../../execroot/bla/$(@D)"