Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ common --enable_runfiles

# To update these lines, execute
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
build --deleted_packages=.bazelbsp,.bazelbsp/aspects,examples/android,examples/android/app,examples/android/bzl,examples/android/libAndroid,examples/android/libJava,examples/android/libKtAndroid,examples/android/libKtAndroid/src/test/java/examples/android/lib,examples/android/third_party,examples/anvil,examples/anvil/app,examples/anvil/app/src/androidTest/java/com/squareup/anvil/sample,examples/anvil/app/src/main/java/com/squareup/anvil/sample,examples/anvil/app/src/test/java/com/squareup/anvil/sample,examples/anvil/library,examples/anvil/library/src/main/java/com/squareup/anvil/sample,examples/anvil/repro/src/main/java/com/repro/lib,examples/anvil/scopes,examples/anvil/scopes/src/main/java/com/squareup/scopes,examples/anvil/third_party,examples/associates,examples/associates/projects/core/api,examples/associates/projects/core/api/src/test/kotlin/core/api,examples/associates/projects/core/impl,examples/associates/projects/core/impl/src/test/kotlin/core/impl,examples/dagger,examples/dagger/third_party,examples/deps,examples/deps/bzl,examples/deps/libAndroid1,examples/deps/libAndroid2,examples/deps/libAndroid3,examples/deps/libAndroid4,examples/deps/libJava1,examples/deps/libJava2,examples/deps/libJava3,examples/deps/libJava4,examples/deps/libKt1,examples/deps/libKt2,examples/deps/libKt3,examples/deps/libKt4,examples/deps/libKtAndroid1,examples/deps/libKtAndroid2,examples/deps/libKtAndroid3,examples/deps/libKtAndroid4,examples/jetpack_compose,examples/jetpack_compose/app,examples/jetpack_compose/compose-ui,examples/ksp,examples/ksp/third_party,examples/multiplex,examples/multiplex/src,examples/plugin,examples/plugin/src/allopen,examples/plugin/src/allopennoarg,examples/plugin/src/noarg,examples/plugin/src/parcelize,examples/plugin/src/sam_with_receiver,examples/plugin/src/serialization,examples/trivial,examples/trivial/app,src/main/starlark/release_archive
query --deleted_packages=.bazelbsp,.bazelbsp/aspects,examples/android,examples/android/app,examples/android/bzl,examples/android/libAndroid,examples/android/libJava,examples/android/libKtAndroid,examples/android/libKtAndroid/src/test/java/examples/android/lib,examples/android/third_party,examples/anvil,examples/anvil/app,examples/anvil/app/src/androidTest/java/com/squareup/anvil/sample,examples/anvil/app/src/main/java/com/squareup/anvil/sample,examples/anvil/app/src/test/java/com/squareup/anvil/sample,examples/anvil/library,examples/anvil/library/src/main/java/com/squareup/anvil/sample,examples/anvil/repro/src/main/java/com/repro/lib,examples/anvil/scopes,examples/anvil/scopes/src/main/java/com/squareup/scopes,examples/anvil/third_party,examples/associates,examples/associates/projects/core/api,examples/associates/projects/core/api/src/test/kotlin/core/api,examples/associates/projects/core/impl,examples/associates/projects/core/impl/src/test/kotlin/core/impl,examples/dagger,examples/dagger/third_party,examples/deps,examples/deps/bzl,examples/deps/libAndroid1,examples/deps/libAndroid2,examples/deps/libAndroid3,examples/deps/libAndroid4,examples/deps/libJava1,examples/deps/libJava2,examples/deps/libJava3,examples/deps/libJava4,examples/deps/libKt1,examples/deps/libKt2,examples/deps/libKt3,examples/deps/libKt4,examples/deps/libKtAndroid1,examples/deps/libKtAndroid2,examples/deps/libKtAndroid3,examples/deps/libKtAndroid4,examples/jetpack_compose,examples/jetpack_compose/app,examples/jetpack_compose/compose-ui,examples/ksp,examples/ksp/third_party,examples/multiplex,examples/multiplex/src,examples/plugin,examples/plugin/src/allopen,examples/plugin/src/allopennoarg,examples/plugin/src/noarg,examples/plugin/src/parcelize,examples/plugin/src/sam_with_receiver,examples/plugin/src/serialization,examples/trivial,examples/trivial/app,src/main/starlark/release_archive
build --deleted_packages=.bazelbsp,.bazelbsp/aspects,examples/android,examples/android/app,examples/android/bzl,examples/android/libAndroid,examples/android/libJava,examples/android/libKtAndroid,examples/android/libKtAndroid/src/test/java/examples/android/lib,examples/android/third_party,examples/anvil,examples/anvil/app,examples/anvil/app/src/androidTest/java/com/squareup/anvil/sample,examples/anvil/app/src/main/java/com/squareup/anvil/sample,examples/anvil/app/src/test/java/com/squareup/anvil/sample,examples/anvil/library,examples/anvil/library/src/main/java/com/squareup/anvil/sample,examples/anvil/repro/src/main/java/com/repro/lib,examples/anvil/scopes,examples/anvil/scopes/src/main/java/com/squareup/scopes,examples/anvil/third_party,examples/associates,examples/associates/projects/core/api,examples/associates/projects/core/api/src/test/kotlin/core/api,examples/associates/projects/core/impl,examples/associates/projects/core/impl/src/test/kotlin/core/impl,examples/dagger,examples/dagger/third_party,examples/deps,examples/deps/bzl,examples/deps/libAndroid1,examples/deps/libAndroid2,examples/deps/libAndroid3,examples/deps/libAndroid4,examples/deps/libJava1,examples/deps/libJava2,examples/deps/libJava3,examples/deps/libJava4,examples/deps/libKt1,examples/deps/libKt2,examples/deps/libKt3,examples/deps/libKt4,examples/deps/libKtAndroid1,examples/deps/libKtAndroid2,examples/deps/libKtAndroid3,examples/deps/libKtAndroid4,examples/jetpack_compose,examples/jetpack_compose/app,examples/jetpack_compose/compose-ui,examples/ksp,examples/ksp/third_party,examples/multiplex,examples/multiplex/src,examples/nested_module_resources,examples/nested_module_resources/nested,examples/plugin,examples/plugin/src/allopen,examples/plugin/src/allopennoarg,examples/plugin/src/noarg,examples/plugin/src/parcelize,examples/plugin/src/sam_with_receiver,examples/plugin/src/serialization,examples/trivial,examples/trivial/app,src/main/starlark/release_archive
query --deleted_packages=.bazelbsp,.bazelbsp/aspects,examples/android,examples/android/app,examples/android/bzl,examples/android/libAndroid,examples/android/libJava,examples/android/libKtAndroid,examples/android/libKtAndroid/src/test/java/examples/android/lib,examples/android/third_party,examples/anvil,examples/anvil/app,examples/anvil/app/src/androidTest/java/com/squareup/anvil/sample,examples/anvil/app/src/main/java/com/squareup/anvil/sample,examples/anvil/app/src/test/java/com/squareup/anvil/sample,examples/anvil/library,examples/anvil/library/src/main/java/com/squareup/anvil/sample,examples/anvil/repro/src/main/java/com/repro/lib,examples/anvil/scopes,examples/anvil/scopes/src/main/java/com/squareup/scopes,examples/anvil/third_party,examples/associates,examples/associates/projects/core/api,examples/associates/projects/core/api/src/test/kotlin/core/api,examples/associates/projects/core/impl,examples/associates/projects/core/impl/src/test/kotlin/core/impl,examples/dagger,examples/dagger/third_party,examples/deps,examples/deps/bzl,examples/deps/libAndroid1,examples/deps/libAndroid2,examples/deps/libAndroid3,examples/deps/libAndroid4,examples/deps/libJava1,examples/deps/libJava2,examples/deps/libJava3,examples/deps/libJava4,examples/deps/libKt1,examples/deps/libKt2,examples/deps/libKt3,examples/deps/libKt4,examples/deps/libKtAndroid1,examples/deps/libKtAndroid2,examples/deps/libKtAndroid3,examples/deps/libKtAndroid4,examples/jetpack_compose,examples/jetpack_compose/app,examples/jetpack_compose/compose-ui,examples/ksp,examples/ksp/third_party,examples/multiplex,examples/multiplex/src,examples/nested_module_resources,examples/nested_module_resources/nested,examples/plugin,examples/plugin/src/allopen,examples/plugin/src/allopennoarg,examples/plugin/src/noarg,examples/plugin/src/parcelize,examples/plugin/src/sam_with_receiver,examples/plugin/src/serialization,examples/trivial,examples/trivial/app,src/main/starlark/release_archive

# User-specific .bazelrc
try-import %workspace%/user.bazelrc
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ test_suite(
"//src/test/kotlin/io/bazel/kotlin/builder:builder_tests",
"//src/test/kotlin/io/bazel/worker:worker_tests",
"//src/test/starlark:convert_tests",
"//src/test/starlark:resource_strip_prefix_tests",
],
)

Expand All @@ -52,6 +53,7 @@ test_suite(
"//src/test/kotlin/io/bazel/kotlin:local_assertion_tests",
"//src/test/kotlin/io/bazel/worker:local_worker_tests",
"//src/test/starlark:convert_tests",
"//src/test/starlark:resource_strip_prefix_tests",
],
)

Expand Down
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bazel_dep(name = "rules_proto", version = "6.0.2", repo_name = "rules_proto")
bazel_dep(name = "abseil-py", version = "2.1.0", repo_name = "py_absl")
bazel_dep(name = "rules_cc", version = "0.0.16")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "bazel_skylib", version = "1.8.2")
bazel_dep(name = "rules_java", version = "8.9.0")
bazel_dep(name = "rules_python", version = "0.23.1")
bazel_dep(name = "rules_android", version = "0.6.4")
Expand Down Expand Up @@ -109,7 +109,7 @@ use_repo(maven, "kotlin_rules_maven", "unpinned_kotlin_rules_maven")
bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "stardoc", version = "0.8.0", repo_name = "io_bazel_stardoc")

bazel_dep(name = "rules_testing", version = "0.5.0", dev_dependency = True)
bazel_dep(name = "rules_testing", version = "0.9.0", dev_dependency = True)
bazel_dep(name = "rules_bazel_integration_test", version = "0.34.0", dev_dependency = True)

bazel_binaries = use_extension("@rules_bazel_integration_test//:extensions.bzl", "bazel_binaries", dev_dependency = True)
Expand Down
6 changes: 3 additions & 3 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ It is appropriate for building workspace utilities. `java_binary` should be pref
| <a id="kt_jvm_binary-module_name"></a>module_name | The name of the module, if not provided the module name is derived from the label. --e.g., `//some/package/path:label_name` is translated to `some_package_path-label_name`. | String | optional | `""` |
| <a id="kt_jvm_binary-plugins"></a>plugins | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_jvm_binary-resource_jars"></a>resource_jars | Set of archives containing Java resources. If specified, the contents of these jars are merged into the output jar. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_jvm_binary-resource_strip_prefix"></a>resource_strip_prefix | The path prefix to strip from Java resources, files residing under common prefix such as `src/main/resources` or `src/test/resources` or `kotlin` will have stripping applied by convention. | String | optional | `""` |
| <a id="kt_jvm_binary-resource_strip_prefix"></a>resource_strip_prefix | The path prefix to strip from Java resources. Should be a label pointing to a directory. Files residing under common prefix such as `src/main/resources` or `src/test/resources` or `kotlin` will have stripping applied by convention if this is not specified. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="kt_jvm_binary-runtime_deps"></a>runtime_deps | Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


Expand Down Expand Up @@ -166,7 +166,7 @@ This rule compiles and links Kotlin and Java sources into a .jar file.
| <a id="kt_jvm_library-neverlink"></a>neverlink | If true only use this library for compilation and not at runtime. | Boolean | optional | `False` |
| <a id="kt_jvm_library-plugins"></a>plugins | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_jvm_library-resource_jars"></a>resource_jars | Set of archives containing Java resources. If specified, the contents of these jars are merged into the output jar. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_jvm_library-resource_strip_prefix"></a>resource_strip_prefix | The path prefix to strip from Java resources, files residing under common prefix such as `src/main/resources` or `src/test/resources` or `kotlin` will have stripping applied by convention. | String | optional | `""` |
| <a id="kt_jvm_library-resource_strip_prefix"></a>resource_strip_prefix | The path prefix to strip from Java resources. Should be a label pointing to a directory. Files residing under common prefix such as `src/main/resources` or `src/test/resources` or `kotlin` will have stripping applied by convention if this is not specified. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="kt_jvm_library-runtime_deps"></a>runtime_deps | Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


Expand Down Expand Up @@ -209,7 +209,7 @@ Setup a simple kotlin_test.
| <a id="kt_jvm_test-module_name"></a>module_name | The name of the module, if not provided the module name is derived from the label. --e.g., `//some/package/path:label_name` is translated to `some_package_path-label_name`. | String | optional | `""` |
| <a id="kt_jvm_test-plugins"></a>plugins | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_jvm_test-resource_jars"></a>resource_jars | Set of archives containing Java resources. If specified, the contents of these jars are merged into the output jar. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_jvm_test-resource_strip_prefix"></a>resource_strip_prefix | The path prefix to strip from Java resources, files residing under common prefix such as `src/main/resources` or `src/test/resources` or `kotlin` will have stripping applied by convention. | String | optional | `""` |
| <a id="kt_jvm_test-resource_strip_prefix"></a>resource_strip_prefix | The path prefix to strip from Java resources. Should be a label pointing to a directory. Files residing under common prefix such as `src/main/resources` or `src/test/resources` or `kotlin` will have stripping applied by convention if this is not specified. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="kt_jvm_test-runtime_deps"></a>runtime_deps | Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_jvm_test-test_class"></a>test_class | The Java class to be loaded by the test runner. | String | optional | `""` |

Expand Down
8 changes: 8 additions & 0 deletions examples/nested_module_resources/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_binary")

kt_jvm_binary(
name = "main",
srcs = glob(["*.kt"]),
main_class = "MainKt",
deps = ["@nested//:printer"],
)
13 changes: 13 additions & 0 deletions examples/nested_module_resources/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module(name = "nested_module_resources")

bazel_dep(name = "rules_kotlin", version = "1.9.5")
local_path_override(
module_name = "rules_kotlin",
path = "../..",
)

bazel_dep(name = "nested", version = "0")
local_path_override(
module_name = "nested",
path = "nested",
)
3 changes: 3 additions & 0 deletions examples/nested_module_resources/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main() {
printMessage()
}
47 changes: 47 additions & 0 deletions examples/nested_module_resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Nested Module Resources Example

## What This Tests

This example tests **resource path resolution across Bazel module boundaries** when using `resource_strip_prefix`.

## Problem Statement

When a target in one Bazel module depends on a library in an external Bazel module that has resources with a custom `resource_strip_prefix`, the resource paths need to be correctly resolved.

### Module Structure

```
nested_module_resources/ # Main module
├── MODULE.bazel # bazel_dep(name = "nested")
├── Main.kt # Calls @nested//:printer
└── nested/ # SEPARATE BAZEL MODULE
├── MODULE.bazel # module(name = "nested")
├── Printer.kt
├── BUILD.bazel # resource_strip_prefix = "resourcez"
└── resourcez/
└── resource.txt
```

**Key Point:** `nested/` is a separate Bazel module with its own `MODULE.bazel`, not just a subdirectory.

## The Bug (Before Fix)

Without the fix in PR #1390, resource paths from external modules included the module path prefix, causing the strip prefix check to fail:

### With nested/ in .bazelignore:
```
Error in fail: Resource file ../nested+/resourcez/resource.txt is not under
the specified prefix to strip resourcez
```
- Resource path: `../nested+/resourcez/resource.txt`
- Strip prefix: `resourcez`
- Check fails: `../nested+/resourcez/resource.txt` doesn't start with `resourcez`

### Without .bazelignore:
```
Error in fail: Resource file nested/resourcez/resource.txt is not under
the specified prefix to strip resourcez
```
- Resource path: `nested/resourcez/resource.txt`
- Strip prefix: `resourcez`
- Check fails: `nested/resourcez/resource.txt` doesn't start with `resourcez`
9 changes: 9 additions & 0 deletions examples/nested_module_resources/nested/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

kt_jvm_library(
name = "printer",
srcs = glob(["*.kt"]),
resource_strip_prefix = "resourcez",
resources = glob(["resourcez/**"]),
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions examples/nested_module_resources/nested/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module(name = "nested")

bazel_dep(name = "rules_kotlin", version = "1.9.5")
local_path_override(
module_name = "rules_kotlin",
path = "../../..",
)
8 changes: 8 additions & 0 deletions examples/nested_module_resources/nested/Printer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fun printMessage() {
val resourceName = "resource.txt"
val resourceUrl = Thread.currentThread().contextClassLoader.getResource(resourceName)
val resourceText = resourceUrl?.readText()

println("Hello $resourceText")
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
world
27 changes: 25 additions & 2 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ _CONVENTIONAL_RESOURCE_PATHS = [

def _adjust_resources_path_by_strip_prefix(path, resource_strip_prefix):
if not path.startswith(resource_strip_prefix):
fail("Resource file %s is not under the specified prefix to strip" % path)
fail("Resource file %s is not under the specified prefix to strip %s" % (path, resource_strip_prefix))

clean_path = path[len(resource_strip_prefix):]
return clean_path
Expand Down Expand Up @@ -274,8 +274,31 @@ def _fold_jars_action(ctx, rule_kind, toolchains, output_jar, input_jars, action

def _resourcejar_args_action(ctx, extra_resources = {}):
res_cmd = []

# Get the strip prefix from the File object if provided
strip_prefix = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ctx.file.resource_strip_prefix:
file = ctx.file.resource_strip_prefix
file_path = file.path

# Assume that strip_prefix has the same root as the resources
if ctx.files.resources and file.root.path != ctx.files.resources[0].root.path:
# Strip prefix root mismatch
file_path = file_path[len(file.root.path):]

# if dirname starts with ctx.label.package, we need to remove ctx.label.package, because it means that
# we've hit the edge case when there is a target with the same name as the package
if file.dirname.startswith(ctx.label.package + "/"):
strip_prefix = file_path[len(ctx.label.package) + 1:]
else:
strip_prefix = file_path

if ctx.files.resources and file.root.path != ctx.files.resources[0].root.path:
# Add back the root path to align with resources paths
strip_prefix = ctx.files.resources[0].root.path + "/" + strip_prefix

for f in ctx.files.resources:
target_path = _adjust_resources_path(f.short_path, ctx.attr.resource_strip_prefix)
target_path = _adjust_resources_path(f.path, strip_prefix)
if target_path[0] == "/":
target_path = target_path[1:]
line = "{target_path}={f_path}\n".format(
Expand Down
Loading