在除 /tmp 之外的其他路径中不允许使用 bazel 生成 jacoco.exec

Generation of jacoco.exec using bazel not permitted in other paths except /tmp

在我的 BUILD.bazel 中,我的 java_test 看起来像这样:

java_test(
    name = "SomeServiceTest",
    srcs = [
      "src/test/java/com/service/SomeServiceTest.java",
    ],
    test_class = "com.service.SomeServiceTest",
    deps = [
        "SomeService",
        "@junit_junit//jar",
        "@commons_logging_commons_logging//jar",
        "@org_hamcrest_hamcrest_core//jar",
        "@com_fasterxml_jackson_core_jackson_annotations//jar",
        "@javax_servlet_javax_servlet_api//jar",
        "@org_springframework_spring_aop//jar",
        "@org_springframework_spring_beans//jar",
        "@org_springframework_spring_context//jar",
        "@org_springframework_spring_test//jar",
        "@org_springframework_spring_web//jar",
        "@org_mockito_mockito_core//jar",
        "@net_bytebuddy_byte_buddy//jar",
    ],

    size = "medium",
    jvm_flags = ["-javaagent:$$workspacepath/jacocoagent-runtime.jar=destfile=$$workspacepath/jacoco.exec"]
)

我想使 jacocoagent-runtime.jar 的路径和生成 jacoco.exec 的路径成为动态的,因此,jvm_flags 设置。我在执行下面的 bazel 测试时定义了 $$workspacepath:

bazel test --test_output=all --action_env=workspacepath=/Users/Someone/Desktop some-service:all_tests

现在,我收到以下错误:

java.io.FileNotFoundException: /Users/Someone/Desktop/jacoco.exec (Operation not permitted)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at org.jacoco.agent.rt.internal_290345e.output.FileOutput.openFile(FileOutput.java:67)
at org.jacoco.agent.rt.internal_290345e.output.FileOutput.writeExecutionData(FileOutput.java:53)
at org.jacoco.agent.rt.internal_290345e.Agent.shutdown(Agent.java:137)
at org.jacoco.agent.rt.internal_290345e.Agent.run(Agent.java:54)

如果我将工作区路径更改为 /tmp,它可以正常工作。 /tmp 以外的其他路径有什么问题?

我同意@Godin -- sounds like the input path is not in the sandbox. Does --spawn_strategy=standalone[1]帮助?

如果这确实是问题,那么要使用沙盒修复构建,您需要将 .jar 文件作为 java_test 操作的输入并从 jvm_flags 中正确引用其路径。

为此:

  1. 在您的工作区中创建一个新包并将 jacoco jar 复制到那里,或者将 new_local_repository 规则添加到您的 WORKSPACE 文件并引用 jar 的目录并指定build_file_contents 属性为 exports_files(["jacoco-runtime.jar"])

  2. 现在您可以通过标签(例如 @jacoco//:jacoco-runtime.jar)引用 Jacoco,您需要将其添加到 java_test 规则的 data 属性

  3. 最后您需要更改 java_test 规则的 jvm_flags 属性以使用 $(location <label>) 引用 jar,例如$(location @jacoco//:jacoco-runtime.jar)


[1] https://docs.bazel.build/versions/master/user-manual.html#flag--spawn_strategy