Skip to content

Commit 7c57e0c

Browse files
authored
Add support for strict associate deps (#1260)
* Add support for strict associate deps * FIx flag usage
1 parent 4e3b18e commit 7c57e0c

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

kotlin/internal/jvm/associates.bzl

+20-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,28 @@ load(
2525
_utils = "utils",
2626
)
2727

28-
def _get_associates(ctx, associates):
28+
def _collect_associates(ctx, toolchains, associate):
29+
"""Collects the associate jars from the provided dependency and returns
30+
them as a depset.
31+
32+
There are two outcomes for this marco:
33+
1. When `experimental_strict_associate_dependencies` is enabled and the tag override has not been provided, only the
34+
direct java_output compile jars will be collected for each associate target.
35+
2. When `experimental_strict_associate_dependencies` is disabled, the complete transitive set of compile jars will
36+
be collected for each assoicate target.
37+
"""
38+
jars_depset = None
39+
if (toolchains.kt.experimental_strict_associate_dependencies and
40+
"kt_experimental_strict_associate_dependencies_incompatible" not in ctx.attr.tags):
41+
jars_depset = depset(direct = [a.compile_jar for a in associate[JavaInfo].java_outputs])
42+
else:
43+
jars_depset = depset(transitive = [associate[JavaInfo].compile_jars])
44+
return jars_depset
45+
46+
def _get_associates(ctx, toolchains, associates):
2947
"""Creates a struct of associates meta data"""
3048
if not associates:
3149
return struct(
32-
targets = [],
3350
module_name = _utils.derive_module_name(ctx),
3451
jars = depset(),
3552
)
@@ -39,7 +56,7 @@ def _get_associates(ctx, associates):
3956
jars = []
4057
module_names = []
4158
for a in associates:
42-
jars.append(depset(transitive = [a[JavaInfo].compile_jars, a[_KtJvmInfo].module_jars]))
59+
jars.append(_collect_associates(ctx = ctx, toolchains = toolchains, associate = a))
4360
module_names.append(a[_KtJvmInfo].module_name)
4461
module_names = list(_sets.copy_of(module_names))
4562

kotlin/internal/jvm/compile.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _jvm_deps(ctx, toolchains, associate_deps, deps, exports = [], runtime_deps
115115
)
116116
dep_infos = [_java_info(d) for d in associate_deps + deps] + [toolchains.kt.jvm_stdlibs]
117117

118-
associates = _associate_utils.get_associates(ctx, associates = associate_deps)
118+
associates = _associate_utils.get_associates(ctx, toolchains = toolchains, associates = associate_deps)
119119

120120
# Reduced classpath, exclude transitive deps from compilation
121121
if (toolchains.kt.experimental_prune_transitive_deps and

kotlin/internal/toolchains.bzl

+9
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def _kotlin_toolchain_impl(ctx):
9797
empty_jdeps = ctx.file._empty_jdeps,
9898
jacocorunner = ctx.attr.jacocorunner,
9999
experimental_prune_transitive_deps = ctx.attr._experimental_prune_transitive_deps[BuildSettingInfo].value,
100+
experimental_strict_associate_dependencies = ctx.attr._experimental_strict_associate_dependencies[BuildSettingInfo].value,
100101
)
101102

102103
return [
@@ -275,6 +276,14 @@ _kt_toolchain = rule(
275276
kt_experimental_prune_transitive_deps_incompatible tag allows to exclude specific targets""",
276277
default = Label("//kotlin/settings:experimental_prune_transitive_deps"),
277278
),
279+
"_experimental_strict_associate_dependencies": attr.label(
280+
doc = """
281+
If enabled, only the direct compile jars will be collected for each listed associate target
282+
instead of the compelte transitive set of jars. This helps prevent Kotlin internals from leaking beyond
283+
their intended exposure by only exposing the direct java outputs. Using
284+
kt_experimental_prune_transitive_deps_incompatible tag allows to exclude specific targets""",
285+
default = Label("//kotlin/settings:experimental_strict_associate_dependencies"),
286+
),
278287
"_jvm_emit_jdeps": attr.label(default = "//kotlin/settings:jvm_emit_jdeps"),
279288
},
280289
implementation = _kotlin_toolchain_impl,

kotlin/settings/BUILD.bazel

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ release_archive(
2525
# Flag that controls the emission of jdeps files during kotlin jvm compilation.
2626
bool_flag(
2727
name = "jvm_emit_jdeps",
28-
build_setting_default = True,
28+
build_setting_default = True, # Upstream default behavior
2929
visibility = ["//visibility:public"],
3030
)
3131

32-
# Kotlin strict deps can be enabled by setting the following value on the command line
3332
# --@rules_kotlin//kotlin/settings:experimental_prune_transitive_deps=True
3433
bool_flag(
3534
name = "experimental_prune_transitive_deps",
3635
build_setting_default = False,
3736
visibility = ["//visibility:public"],
3837
)
38+
39+
# --@rules_kotlin//kotlin/settings:experimental_strict_associate_dependencies=True
40+
bool_flag(
41+
name = "experimental_strict_associate_dependencies",
42+
build_setting_default = False,
43+
visibility = ["//visibility:public"],
44+
)

kotlin/settings/BUILD.release.bazel

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ bool_flag(
2020
visibility = ["//visibility:public"],
2121
)
2222

23-
# Kotlin strict deps can be enabled by setting the following value on the command line
2423
# --@rules_kotlin//kotlin/settings:experimental_prune_transitive_deps=True
2524
bool_flag(
2625
name = "experimental_prune_transitive_deps",
2726
build_setting_default = False,
2827
visibility = ["//visibility:public"],
2928
)
29+
30+
# --@rules_kotlin//kotlin/settings:experimental_strict_associate_dependencies=True
31+
bool_flag(
32+
name = "experimental_strict_associate_dependencies",
33+
build_setting_default = False,
34+
visibility = ["//visibility:public"],
35+
)

0 commit comments

Comments
 (0)