Skip to content

Inconsistent Java deprecation warnings #25927

Open
@fzakaria

Description

@fzakaria

Description of the bug:

This is a bug report for something I brought up on slack.

I have provided a minimal reproduction code: https://github.com/fzakaria/bazel-xlint-repro

We have a library that contains a @Deprecation annotation.

@Deprecated
public class DeprecatedLib {

    public void oldMethod() {
        System.out.println("This is an old, deprecated method.");
    }

    public static String oldUtility(String input) {
        return "Deprecated: " + input;
    }
}

We have turned on -Xlint:all and -Werror on our Java toolchain.

When we write a test that uses the deprecated class, the Java compiler complains.

import org.junit.Test;

public class DeprecatedLibTest {

    @Test
    public void testDoSomething() {
        DeprecatedLib lib = new DeprecatedLib();
        lib.oldMethod(); // This will show a warning
    }
}
> bazel build //:DeprecatedLibTest --action_env=1=3
INFO: Invocation ID: a25ed412-763d-4c72-96be-b986a465f139
INFO: Analyzed target //:DeprecatedLibTest (0 packages loaded, 0 targets configured).
ERROR: /Users/fzakaria/code/playground/bazel/xlint-repro/BUILD.bazel:18:10: Building DeprecatedLibTest.jar (1 source file) failed: (Exit 1): java failed: error executing Javac command (from target //:DeprecatedLibTest) external/rules_java~~toolchains~remotejdk21_macos_aarch64/bin/java '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' ... (remaining 19 arguments skipped)
DeprecatedLibTest.java:7: error: [deprecation] DeprecatedLib in unnamed package has been deprecated
        DeprecatedLib lib = new DeprecatedLib();
        ^
DeprecatedLibTest.java:7: error: [deprecation] DeprecatedLib in unnamed package has been deprecated
        DeprecatedLib lib = new DeprecatedLib();
                                ^

However if we add some changes to the class, such as a static import, the warning seems to no longer be present.

import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * By *fail* i mean this target builds correctly even though it uses a deprecated library.
 * 
 * You can see the alternative class in DeprecatedLibTest.java which fails to build
 * although it uses the same code with the sole exception of not using jupiter static assertions.
 */
public class DeprecatedLibFailTest {

    @Test
    public void testDoSomething() {
        DeprecatedLib lib = new DeprecatedLib();
        lib.oldMethod(); // This will show a warning
    }
}
> bazel build //:DeprecatedLibFailTest --action_env=1=5
INFO: Invocation ID: 64af9b38-0189-4906-9d04-0bd3a1b4420b
WARNING: Build option --action_env has changed, discarding analysis cache (this can be expensive, see https://bazel.build/advanced/performance/iteration-speed).
INFO: Analyzed target //:DeprecatedLibFailTest (56 packages loaded, 1616 targets configured).
INFO: Found 1 target...
Target //:DeprecatedLibFailTest up-to-date:
  bazel-bin/DeprecatedLibFailTest
  bazel-bin/DeprecatedLibFailTest.jar
INFO: Elapsed time: 0.860s, Critical Path: 0.58s
INFO: 7 processes: 2 internal, 3 darwin-sandbox, 2 worker.
INFO: Build completed successfully, 7 total action

I am very stumped as to why the import is causing the deprecation notice to be triggered or not.

Which category does this issue belong to?

Java Rules

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

To see the "failure mode" (it passing)

> bazel build //:DeprecatedLibFailTest

To see the "correct mode" (it failing)

> bazel build //:DeprecatedLibTest

Which operating system are you running Bazel on?

MacOS

What is the output of bazel info release?

release 7.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2We'll consider working on this in future. (Assignee optional)team-Rules-JavaIssues for Java rulestype: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions