bazelbuild / intellij

IntelliJ plugin for Bazel projects
https://github.com/bazelbuild/intellij/blob/master/docs/index.md
Apache License 2.0
763 stars 306 forks source link

Incorrect test target selected when running or debugging tests with Bazel for IntelliJ plugin versions 2023.10.08.0.1 and newer #7017

Open paulxu86 opened 2 days ago

paulxu86 commented 2 days ago

Description of the bug:

When running or debugging tests in IntelliJ IDEA using newer versions of the Bazel for IntelliJ plugin (versions 2024.10.08.0.1-api-version-242 and later), the IDE often selects the incorrect Bazel target for test execution. I have noticed that this issue specifically occurs when a test class extends a base test class. While it may happen in other scenarios, this appears to be a common case.

Which category does this issue belong to?

Intellij

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

  1. Set up the environment:

  2. Open a Bazel project in IntelliJ IDEA.

  3. Find a test class that extends another base test class:

public class TestA extends OtherBaseTestB {
    // Test implementation
}
  1. Attempt to run or debug the test: Click the green run/debug icon (▶) next to the test class in the editor's left gutter.

  2. Observe the behaviour:

    • The IDE selects the wrong Bazel target for the test execution.
    • Tests may fail or produce unexpected results due to the incorrect target being used.

Which Intellij IDE are you using? Please provide the specific version.

IntelliJ IDEA Ultimate Edition [2024.2] and [2024.3]

What programming languages and tools are you using? Please provide specific versions.

Programming Language: Java; Test Frameworks: JUnit; Bazel Version: 7.2.1

What Bazel plugin version are you using?

2024.10.08.0.1-api-version-242, 2024.11.07.0.1-api-version-243

Have you found anything relevant by searching the web?

GitHub Issues: Searched but did not find existing issues related to this problem. Email Threads: Checked the intellij-bazel-plugin Google Group as well but found no relevant discussions.

Any other information, logs, or outputs that you want to share?

paulxu86 commented 2 days ago

I wanted to provide some additional details that might help diagnose the issue.

In our project, we use list expansion in our <MODULE_NAME>/BUILD.bazel files to dynamically execute the test targets. I suspect this might be contributing to the problem with incorrect test target selection in the newer plugin versions.

Here's a simplified and sanitized snippet of our BUILD.bazel file for TestA:

load("@rules_java//java:defs.bzl", "java_junit5_test")

package(default_visibility = ["//visibility:public"])

java_library(
    name = "my_project_lib",
    srcs = glob(["src/main/java/**/*.java"]),
    deps = [
        # Dependencies
    ],
)

TEST_FILES = glob(
    ["src/test/java/**/*.java"],
)

# test targets using list expansion
[java_junit5_test(
    name = "test_" + f[:-5].replace("/", "_").replace("src_test_java_", ""),
    srcs = [f],
    test_class = f[:-5].replace("/", ".").replace("src.test.java.", ""),
    deps = [
        ":my_project_lib",
        "//thirdparty:junit5-jupiter",
        # Other dependencies
    ],
    # Other attributes
) for f in TEST_FILES]

As I mentioned in the description above, running a test class (e.g., TestA), that extends OtherBaseTestB, the plugin selects the wrong Bazel target. The plugin incorrectly selects the target AnotherBaseTestC, which is incorrect. If we stick with using the plugin with the newer version, we need to manually edit the Run/Debug Configuration window to specify the correct target for TestA.

I hope this additional information helps in identifying the root cause of the issue.

tpasternak commented 21 hours ago

Well, looks like related to this change https://github.com/bazelbuild/intellij/pull/4473

@agluszak can you please look at it?