Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support experimental Detekt type resolution #118

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion detekt/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def _impl(ctx):

java_arguments = ctx.actions.args()

for jvm_flag in ctx.toolchains["@rules_detekt//detekt:toolchain_type"].jvm_flags:
detekt_toolchain = ctx.toolchains["@rules_detekt//detekt:toolchain_type"]
for jvm_flag in detekt_toolchain.jvm_flags:
# The Bazel-generated execution script requires "=" between argument names and values.
java_arguments.add("--jvm_flag={}".format(jvm_flag))

Expand Down Expand Up @@ -62,6 +63,16 @@ def _impl(ctx):
if ctx.attr.parallel:
detekt_arguments.add("--parallel")

if detekt_toolchain.experimental_type_resolution == True and "detekt_type_resolution_incompatible" not in ctx.attr.tags:
# Collect the transitive classpath compile jars to pass to Detekt for classpath information
# compile_jars should contain mostly ijar/header jars that are faster to load onto the classpath
classpath = depset([], transitive = [dep[JavaInfo].transitive_compile_time_jars for dep in ctx.attr.deps])
action_inputs.extend(classpath.to_list())
detekt_arguments.add_joined("--classpath", classpath, join_with = ",")

detekt_arguments.add("--language-version", detekt_toolchain.language_version)
detekt_arguments.add("--jvm-target", detekt_toolchain.jvm_target)

action_inputs.extend(ctx.files.plugins)
detekt_arguments.add_joined("--plugins", ctx.files.plugins, join_with = ",")

Expand Down Expand Up @@ -93,6 +104,11 @@ detekt = rule(
allow_empty = False,
doc = "Kotlin source code files.",
),
"deps": attr.label_list(
default = [],
doc = "Dependencies to provide to Detekt for classpath type resolution.",
providers = [JavaInfo],
),
"cfgs": attr.label_list(
allow_files = [".yml"],
default = [],
Expand Down
41 changes: 41 additions & 0 deletions detekt/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Toolchain declaration.
def _impl(ctx):
toolchain = platform_common.ToolchainInfo(
jvm_flags = ctx.attr.jvm_flags,
experimental_type_resolution = ctx.attr.experimental_type_resolution,
language_version = ctx.attr.language_version,
jvm_target = ctx.attr.jvm_target,
)

return [toolchain]
Expand All @@ -17,5 +20,43 @@ detekt_toolchain = rule(
doc = "JVM flags used for Detekt execution.",
allow_empty = False,
),
"experimental_type_resolution": attr.bool(
doc = """Flag for toggling experimental type resolution which is expected to be stable in
the Detekt 2.x [see](https://detekt.github.io/detekt/type-resolution.html)
""",
default = True,
),
"language_version": attr.string(
doc = """Target version of the generated JVM bytecode that was generated during compilation and
is now being used for type resolution [see](https://detekt.github.io/detekt/cli.html)
""",
default = "1.5",
values = [
"1.0",
"1.1",
"1.2",
"1.3",
"1.4",
"1.5",
"1.6",
"1.7",
],
),
"jvm_target": attr.string(
doc = "Compatibility mode for Kotlin language version [see](https://detekt.github.io/detekt/cli.html)",
default = "1.8",
values = [
"1.6",
"1.8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
],
),
},
)
18 changes: 18 additions & 0 deletions tests/analysis/tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def _action_full_contents_test_impl(ctx):
"--disable-default-rulesets",
"--fail-fast",
"--parallel",
"--classpath",
",".join([
"{{output_dir}}/external/rules_detekt_dependencies/v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.5.21/header_kotlin-stdlib-1.5.21.jar",
"{{output_dir}}/external/rules_detekt_dependencies/v1/https/repo1.maven.org/maven2/org/jetbrains/annotations/13.0/header_annotations-13.0.jar",
"{{output_dir}}/external/rules_detekt_dependencies/v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.5.21/header_kotlin-stdlib-common-1.5.21.jar",
]),
"--language-version",
"1.5",
"--jvm-target",
"1.8",
])

expected_inputs = _expand_paths(env.ctx, [
Expand All @@ -54,6 +64,9 @@ def _action_full_contents_test_impl(ctx):
"{{source_dir}}/config B.yml",
"{{source_dir}}/config C.yml",
"{{source_dir}}/baseline.xml",
"{{output_dir}}/external/rules_detekt_dependencies/v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.5.21/header_kotlin-stdlib-1.5.21.jar",
"{{output_dir}}/external/rules_detekt_dependencies/v1/https/repo1.maven.org/maven2/org/jetbrains/annotations/13.0/header_annotations-13.0.jar",
"{{output_dir}}/external/rules_detekt_dependencies/v1/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.5.21/header_kotlin-stdlib-common-1.5.21.jar",
"bazel-out/host/internal/_middlemen/detekt_Swrapper_Sbin-runfiles",
"bazel-out/host/bin/detekt/wrapper/bin.jar",
"bazel-out/host/bin/detekt/wrapper/bin",
Expand Down Expand Up @@ -89,6 +102,7 @@ def _test_action_full_contents():
parallel = True,
# The "plugins" option is skipped here since the path includes a declared Detekt version
# and we do not want to change the test every time the Detekt artifact is updated.
deps = ["@rules_detekt_dependencies//:org_jetbrains_kotlin_kotlin_stdlib"],
)

action_full_contents_test(
Expand All @@ -112,6 +126,10 @@ def _action_blank_contents_test_impl(ctx):
"{{source_dir}}/path A.kt,{{source_dir}}/path B.kt,{{source_dir}}/path C.kt",
"--report",
"txt:{{output_dir}}/{{source_dir}}/test_target_blank_detekt_report.txt",
"--language-version",
"1.5",
"--jvm-target",
"1.8",
])

expected_inputs = _expand_paths(env.ctx, [
Expand Down