Cherry-pick https://github.com/bazelbuild/bazel/commit/f0ad0fe0e1fc663412a400c4e6d7960f220dd647 into 6.3.1 @iancha1992