Skip to content

Test runnables don't match simple (non-dotted) package names #241

@jalzur

Description

@jalzur

What happened?

Problem

Test runnables (the ▶ play buttons next to @Test methods and test classes) don't appear for Java files in simple (non-dotted) packages like package org;. They only work for dotted package names like package com.example;.

Root Cause

The runnables.scm test queries require (scoped_identifier) as the child of package_declaration:

; Run test function (marker annotation, e.g. @Test)
((package_declaration
  (scoped_identifier) @java_package_name)   ; <-- this is the problem
  (class_declaration
    ...
    (#match? @annotation_name "Test$")))) @_
  (#set! tag java-test-method))

In the tree-sitter-java grammar, package_declaration uses _name, which is inlined and resolves to either identifier (for simple names like org) or scoped_identifier (for dotted names like com.example):

  • package org;(package_declaration (identifier))
  • package com.example;(package_declaration (scoped_identifier (identifier) (identifier)))

The query only matches the latter, so any test in a simple package gets no runnable.

Note that the main method query already handles this correctly by making the package_declaration optional with ?:

((package_declaration
  (scoped_identifier) @java_package_name)?   ; <-- optional, works with or without package
  (class_declaration
    ...

Fix

All test runnable patterns that match (package_declaration (scoped_identifier) ...) should also match (package_declaration (identifier) ...):

(package_declaration
  [
    (scoped_identifier) @java_package_name
    (identifier) @java_package_name
  ])

This affects the following patterns:

  • java-test-method
  • java-test-method-nested
  • java-test-class
  • java-test-class-nested

Alternatively, making the package_declaration optional (like the main method queries do with ?) would also fix the issue and additionally handle classes in the default package.

Reproduction

  1. Create a Java file with a simple package: package org;
  2. Add a @Test annotated method
  3. Observe that no ▶ launcher appears in Zed
  4. Change the package to package com.example; and observe the launcher appears

Environment

  • Zed Java extension version: 6.8.16
  • JDTLS: 1.58.0
  • Java: 25

What did you expect to happen?

To have a test runnable (arrow) launcher in the unit tests without caring of the package of the class.

Environment

Zed: 1.1.6
Platform: macOS Tahoe 26.4.1.4
Zed Java extension version: 6.8.16
JDTLS: 1.58.0
Java: 25

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions