diff --git a/.bazelrc b/.bazelrc index 59cc1f9b3..65e26eb6f 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/BUILD b/BUILD index 8dba02828..600a97f43 100644 --- a/BUILD +++ b/BUILD @@ -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", ], ) @@ -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", ], ) diff --git a/MODULE.bazel b/MODULE.bazel index e2e667300..c6cc9ff72 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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") @@ -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) diff --git a/docs/kotlin.md b/docs/kotlin.md index bbce34eba..2d4a650b2 100755 --- a/docs/kotlin.md +++ b/docs/kotlin.md @@ -70,7 +70,7 @@ It is appropriate for building workspace utilities. `java_binary` should be pref | 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 | `""` | | plugins | - | List of labels | optional | `[]` | | resource_jars | Set of archives containing Java resources. If specified, the contents of these jars are merged into the output jar. | List of labels | optional | `[]` | -| 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 | `""` | +| 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. | Label | optional | `None` | | 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. | List of labels | optional | `[]` | @@ -166,7 +166,7 @@ This rule compiles and links Kotlin and Java sources into a .jar file. | neverlink | If true only use this library for compilation and not at runtime. | Boolean | optional | `False` | | plugins | - | List of labels | optional | `[]` | | resource_jars | Set of archives containing Java resources. If specified, the contents of these jars are merged into the output jar. | List of labels | optional | `[]` | -| 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 | `""` | +| 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. | Label | optional | `None` | | 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. | List of labels | optional | `[]` | @@ -209,7 +209,7 @@ Setup a simple kotlin_test. | 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 | `""` | | plugins | - | List of labels | optional | `[]` | | resource_jars | Set of archives containing Java resources. If specified, the contents of these jars are merged into the output jar. | List of labels | optional | `[]` | -| 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 | `""` | +| 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. | Label | optional | `None` | | 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. | List of labels | optional | `[]` | | test_class | The Java class to be loaded by the test runner. | String | optional | `""` | diff --git a/examples/nested_module_resources/BUILD.bazel b/examples/nested_module_resources/BUILD.bazel new file mode 100644 index 000000000..bc93da9ea --- /dev/null +++ b/examples/nested_module_resources/BUILD.bazel @@ -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"], +) diff --git a/examples/nested_module_resources/MODULE.bazel b/examples/nested_module_resources/MODULE.bazel new file mode 100644 index 000000000..86e6389ea --- /dev/null +++ b/examples/nested_module_resources/MODULE.bazel @@ -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", +) diff --git a/examples/nested_module_resources/Main.kt b/examples/nested_module_resources/Main.kt new file mode 100644 index 000000000..384ac1122 --- /dev/null +++ b/examples/nested_module_resources/Main.kt @@ -0,0 +1,3 @@ +fun main() { + printMessage() +} diff --git a/examples/nested_module_resources/README.md b/examples/nested_module_resources/README.md new file mode 100644 index 000000000..e5e090b36 --- /dev/null +++ b/examples/nested_module_resources/README.md @@ -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` diff --git a/examples/nested_module_resources/nested/BUILD.bazel b/examples/nested_module_resources/nested/BUILD.bazel new file mode 100644 index 000000000..322df555c --- /dev/null +++ b/examples/nested_module_resources/nested/BUILD.bazel @@ -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"], +) diff --git a/examples/nested_module_resources/nested/MODULE.bazel b/examples/nested_module_resources/nested/MODULE.bazel new file mode 100644 index 000000000..29489fccf --- /dev/null +++ b/examples/nested_module_resources/nested/MODULE.bazel @@ -0,0 +1,7 @@ +module(name = "nested") + +bazel_dep(name = "rules_kotlin", version = "1.9.5") +local_path_override( + module_name = "rules_kotlin", + path = "../../..", +) diff --git a/examples/nested_module_resources/nested/Printer.kt b/examples/nested_module_resources/nested/Printer.kt new file mode 100644 index 000000000..086dbcf64 --- /dev/null +++ b/examples/nested_module_resources/nested/Printer.kt @@ -0,0 +1,8 @@ +fun printMessage() { + val resourceName = "resource.txt" + val resourceUrl = Thread.currentThread().contextClassLoader.getResource(resourceName) + val resourceText = resourceUrl?.readText() + + println("Hello $resourceText") +} + diff --git a/examples/nested_module_resources/nested/resourcez/resource.txt b/examples/nested_module_resources/nested/resourcez/resource.txt new file mode 100644 index 000000000..04fea0642 --- /dev/null +++ b/examples/nested_module_resources/nested/resourcez/resource.txt @@ -0,0 +1 @@ +world \ No newline at end of file diff --git a/kotlin/internal/jvm/compile.bzl b/kotlin/internal/jvm/compile.bzl index 87bea81da..ca3e64589 100644 --- a/kotlin/internal/jvm/compile.bzl +++ b/kotlin/internal/jvm/compile.bzl @@ -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 @@ -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 + 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( diff --git a/kotlin/internal/jvm/jvm.bzl b/kotlin/internal/jvm/jvm.bzl index 095deb995..2f20f879d 100644 --- a/kotlin/internal/jvm/jvm.bzl +++ b/kotlin/internal/jvm/jvm.bzl @@ -227,10 +227,12 @@ _common_attr = utils.add_dicts( the output jar.""", default = [], ), - "resource_strip_prefix": attr.string( - doc = """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.""", - default = "", + "resource_strip_prefix": attr.label( + doc = """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.""", + allow_single_file = True, + # providers = ["FileProvider"], ), "resources": attr.label_list( doc = """A list of files that should be include in a Java jar.""", diff --git a/src/test/starlark/BUILD.bazel b/src/test/starlark/BUILD.bazel index aea229cc0..d0c13fa8e 100644 --- a/src/test/starlark/BUILD.bazel +++ b/src/test/starlark/BUILD.bazel @@ -1,3 +1,6 @@ +load("//src/test/starlark/internal/jvm:resource_strip_prefix_test.bzl", "strip_resource_prefix_test_suite") load(":convert_test.bzl", "convert_test_suite") convert_test_suite(name = "convert_tests") + +strip_resource_prefix_test_suite(name = "resource_strip_prefix_tests") diff --git a/src/test/starlark/internal/jvm/resource_strip_prefix_test.bzl b/src/test/starlark/internal/jvm/resource_strip_prefix_test.bzl new file mode 100644 index 000000000..a5fe15b67 --- /dev/null +++ b/src/test/starlark/internal/jvm/resource_strip_prefix_test.bzl @@ -0,0 +1,156 @@ +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("//kotlin:jvm.bzl", "kt_jvm_library") + +def _strip_resource_prefix_test_impl(ctx): + env = analysistest.begin(ctx) + + actions = analysistest.target_actions(env) + + # Find the only FileWrite action (it's the one responsible for writing the arguments to the resource zipper) + file_write_actions = [ + action + for action in actions + if action.mnemonic == "FileWrite" + ] + asserts.equals(env, expected = 1, actual = len(file_write_actions)) + + arguments = file_write_actions[0].content + + print("chuj " + arguments) + + pkg = ctx.attr.pkg + + # The only line should be of the form: + # data.txt=//resourcez/data.txt + lines = arguments.splitlines() + asserts.equals(env, expected = 1, actual = len(lines)) + line_parts = lines[0].split("=", 1) + asserts.equals(env, expected = 2, actual = len(line_parts)) + source_path = line_parts[1] + expected_suffix = pkg + "/" + ctx.attr.resource_strip_prefix + "/" + ctx.attr.resource_path + asserts.true( + env, + source_path.endswith(expected_suffix), + msg = "source path " + source_path + " does not have expected suffix " + expected_suffix, + ) + + destination_path = line_parts[0] + + # The destination path should have the resource_strip_prefix removed + asserts.equals(env, expected = ctx.attr.resource_path, actual = destination_path, msg = "resource_strip_prefix was not applied correctly") + + return analysistest.end(env) + +strip_resource_prefix_test = analysistest.make( + _strip_resource_prefix_test_impl, + attrs = { + "pkg": attr.string(), + "resource_path": attr.string(), + "resource_strip_prefix": attr.string(), + }, +) + +# Macro to setup the test. +def _strip_resource_prefix_contents(): + write_file( + name = "file", + out = "resourcez/resource.txt", + tags = ["manual"], + ) + + write_file( + name = "source", + out = "Source.kt", + tags = ["manual"], + ) + + kt_jvm_library( + name = "dynamically_created_file", + srcs = ["source"], + resource_strip_prefix = "resourcez", + resources = ["file"], # note: file created dynamically above + tags = ["manual"], + ) + + kt_jvm_library( + name = "static_file", + srcs = ["source"], + resource_strip_prefix = "test_resources", + resources = ["test_resources/resource.txt"], # static file in the package + tags = ["manual"], + ) + + kt_jvm_library( + name = "standard_package", + srcs = ["source"], + resources = ["src/main/resources/resource.txt"], # no explicit strip prefix + tags = ["manual"], + ) + + package_name = native.package_name().split("/")[-1] + native.filegroup( + name = package_name, + srcs = ["test_resources/actual_file.txt"], + ) + + kt_jvm_library( + name = "same_as_package_name", + srcs = ["source"], + resources = [package_name], # filegroup defined above + resource_strip_prefix = "test_resources", + tags = ["manual"], + ) + + strip_resource_prefix_test( + name = "dynamically_created_file_test", + target_under_test = ":dynamically_created_file", + tags = ["manual"], + pkg = native.package_name(), + resource_strip_prefix = "resourcez", + resource_path = "resource.txt", + ) + + strip_resource_prefix_test( + name = "static_file_test", + target_under_test = ":static_file", + tags = ["manual"], + pkg = native.package_name(), + resource_strip_prefix = "test_resources", + resource_path = "resource.txt", + ) + + strip_resource_prefix_test( + name = "standard_package_test", + target_under_test = ":standard_package", + tags = ["manual"], + pkg = native.package_name(), + resource_strip_prefix = "src/main/resources", + resource_path = "resource.txt", + ) + + strip_resource_prefix_test( + name = "same_as_package_name_test", + target_under_test = ":same_as_package_name", + tags = ["manual"], + pkg = native.package_name(), + resource_strip_prefix = "test_resources", + resource_path = "actual_file.txt", + ) + +# Entry point from the BUILD file; macro for running each test case's macro and +# declaring a test suite that wraps them together. +def strip_resource_prefix_test_suite(name): + # Call all test functions and wrap their targets in a suite. + _strip_resource_prefix_contents() + + native.test_suite( + name = name, + tests = [ + ":dynamically_created_file_test", + ":static_file_test", + ":standard_package_test", + ":same_as_package_name_test", + ], + tags = ["manual"], + ) diff --git a/src/test/starlark/test_resources/actual_file.txt b/src/test/starlark/test_resources/actual_file.txt new file mode 100644 index 000000000..e69de29bb