From 207101547f610cd7e9db4b924b91ab6a870a7418 Mon Sep 17 00:00:00 2001 From: Matthew Gharrity Date: Wed, 23 Sep 2026 14:07:43 -0700 Subject: [PATCH] Starlarkify collect_native_deps_dirs() This code previously called JavaStarlarkCommon.collectNativeLibsDirs() from Bazel, but we can implement this in Starlark instead. As a side effect, this change avoids a bug in Bazel's implementation of collectNativeLibsDirs(). If a library provides both an interface library (.lib) and a dynamic library (.dll), Bazel would end up using neither, and the library would never end up on java.library.path. This primarily affects Windows in practice. I would have liked to add a regression test, but there is a related rules_cc bug that makes it tricky: https://github.com/bazelbuild/rules_cc/pull/899. I plan to add a Windows test later once all the fixes are in. Note, I had to set use_launcher=False in a couple tests to make them pass in environments where a launcher is enabled, because launchers can pull in extra native deps, resulting in additional entries being listed on java.library.path (which is OK but affects test expectations). Previously the implicit launcher deps were ignored incidentally because they had associated interface libraries. Note for Googlers: this fix is in service of b/149162146#comment19. --- java/private/java_common_internal.bzl | 7 ++++++- .../common/rules/common_launcher_java_binary_tests.bzl | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/java/private/java_common_internal.bzl b/java/private/java_common_internal.bzl index 30d9533c..b9275aad 100644 --- a/java/private/java_common_internal.bzl +++ b/java/private/java_common_internal.bzl @@ -516,7 +516,12 @@ def collect_native_deps_dirs(libraries): Returns: ([String]) A set of root-relative paths as a list """ - return get_internal_java_common().collect_native_deps_dirs(libraries) + dirs = {} + for library in libraries.to_list(): + dylib = library.dynamic_library + if dylib: + dirs[paths.dirname(dylib.short_path)] = None + return dirs.keys() def get_runtime_classpath_for_archive(jars, excluded_jars): """Filters a classpath to remove certain entries diff --git a/test/java/common/rules/common_launcher_java_binary_tests.bzl b/test/java/common/rules/common_launcher_java_binary_tests.bzl index ec403a0a..b6f41e26 100644 --- a/test/java/common/rules/common_launcher_java_binary_tests.bzl +++ b/test/java/common/rules/common_launcher_java_binary_tests.bzl @@ -232,12 +232,16 @@ def _test_java_binary_native_library_path_includes_transitive_deps(name): name = name + "/app", srcs = ["DoesNotMatter.java"], deps = [name + "/jni.so"], + # Launchers can contribute extra native deps, affecting test expectations. + use_launcher = False, ) util.helper_target( java_binary, name = name + "/runtime_app", srcs = ["AlsoDoesNotMatter.java"], runtime_deps = [name + "/jni.so"], + # Launchers can contribute extra native deps, affecting test expectations. + use_launcher = False, ) util.helper_target( cc_binary, @@ -286,6 +290,8 @@ def _test_java_binary_native_library_path_separator(name): name + "/lib1", name + "/lib2", ], + # Launchers can contribute extra native deps, affecting test expectations. + use_launcher = False, ) # Define two native libs (in different directories so java.library.path has multiple entries).