-->

Bazel- How to recursively glob deleted_packages to

2019-07-21 06:37发布

问题:

I have a mutli-module project which I'm migrating from Maven to Bazel. During this migration people will need to be able to work on both build systems.

After an mvn clean install Maven copies some of the BUILD files into the target folder.
When I later try to run bazel build //... it thinks the BUILD files under the various target folders are valid packages and fails due to some mismatch.

I've seen deleted_packages but AFAICT it requires I specify the list of folders to "delete" while I can't do that for 200+ modules.
I'm looking for the ability to say bazel build //... --deleted_packages=**/target.
Is this supported? (my experimentation says it's not but I might be wrong). If it's not supported is there an existing hack for it?

回答1:

Can you use your shell to find the list of packages to ignore?

deleted=$(find . -name target -type d)
bazel build //... --deleted_packages="$deleted"


回答2:

@Laurent's answer gave me the lead but Bazel didn't accept relative paths and required I add both classes and test-classes folders under target to delete the package so I decided to answer with the complete solution:

#!/bin/bash
#find all the target folders under the current working dir
target_folders=$(find . -name target -type d)
#find the repo root (currently assuming it's git based)
repo_root=$(git rev-parse --show-toplevel)
repo_root_length=${#repo_root}
#the current bazel package prefix is the PWD minus the repo root and adding a slash
current_bazel_package="/${PWD:repo_root_length}"
deleted_packages=""

for target in $target_folders
    do 
        #cannonicalize the package path
        full_package_path="$current_bazel_package${target:1}"
        classes_full="${full_package_path}/classes"
        test_classes_full="${full_package_path}/test-classes"
        deleted_packages="$deleted_packages,$classes_full,$test_classes_full"
done

#remove the leading comma and call bazel-real with the other args
bazel-real "$@" --deleted_packages=${deleted_packages:1}

This script was checked in under tools/bazel which is why it calls bazel-real at the end.



回答3:

I'm sorry I don't think this is supported. Some brainstorming:

  • Is it an option to point maven outputs somewhere else?
  • Is is an option not to use //... but explicit target(s)?
  • Maybe just remove the bad BUILD files before running bazel?


标签: bazel