Skip to content

Commit

Permalink
Support enabling enable_check_dependencies at the toolchain level (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bencodes authored Oct 24, 2023
1 parent b2ba326 commit 2e0e4f0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
7 changes: 6 additions & 1 deletion rules/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def _run_android_lint(
disable_checks,
enable_checks,
autofix,
regenerate):
regenerate,
android_lint_enable_check_dependencies):
"""Constructs the Android Lint actions
Args:
Expand All @@ -55,6 +56,7 @@ def _run_android_lint(
enable_checks: List of additional checks to enable
autofix: Whether to autofix (This is a no-op feature right now)
regenerate: Whether to regenerate the baseline files
android_lint_enable_check_dependencies: Enables dependency checking during analysis
"""
inputs = []
outputs = [output]
Expand Down Expand Up @@ -102,6 +104,8 @@ def _run_android_lint(
for dep in classpath_deps:
args.add("--classpath", dep)
inputs.extend(classpath_deps)
if android_lint_enable_check_dependencies:
args.add("--enable-check-dependencies")

# Declare the output file
output = ctx.actions.declare_file("{}.xml".format(ctx.label.name))
Expand Down Expand Up @@ -206,6 +210,7 @@ def process_android_lint_issues(ctx, regenerate):
enable_checks = ctx.attr.enable_checks,
autofix = ctx.attr.autofix,
regenerate = regenerate,
android_lint_enable_check_dependencies = _utils.get_android_lint_toolchain(ctx).android_lint_enable_check_dependencies,
)

return struct(
Expand Down
5 changes: 5 additions & 0 deletions src/cli/AndroidLintActionArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ internal class AndroidLintActionArgs(
help = "",
)

val enableCheckDependencies: Boolean by parser.flagging(
names = arrayOf("--enable-check-dependencies"),
help = "",
).default { false }

companion object {

internal fun parseArgs(args: List<String>): AndroidLintActionArgs {
Expand Down
8 changes: 3 additions & 5 deletions src/cli/AndroidLintRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,17 @@ internal class AndroidLintRunner {
// TODO(bencodes) Use reflection to open this so that the lint version
// can be dynamically set by the toolchain.
val main = Main()
disableDependenciesCheck(main)
disableDependenciesCheck(main, actionArgs.enableCheckDependencies)
return main.run(args.toTypedArray())
}

// A reflection hack for forcing lint to not run against transitive dependencies
// TODO(bencodes) Allow this to be toggled from the toolchain
private fun disableDependenciesCheck(lintMain: Main) {
private fun disableDependenciesCheck(lintMain: Main, enableCheckDependencies: Boolean) {
val mainClass = Class.forName("com.android.tools.lint.Main")
val lintCliFlagsField = mainClass.getDeclaredField("flags")
lintCliFlagsField.isAccessible = true

val lintCliFlags = lintCliFlagsField.get(lintMain) as LintCliFlags
lintCliFlags.isCheckDependencies = false
lintCliFlags.isCheckDependencies = enableCheckDependencies
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/src/cli/AndroidLintActionArgsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class AndroidLintActionArgsTest {
"1.7",
"--kotlin-language-level",
"1.8",
"--enable-check-dependencies",
),
)

Expand All @@ -66,5 +67,6 @@ class AndroidLintActionArgsTest {
assertThat(parseArgs.compileSdkVersion).isEqualTo("1.6")
assertThat(parseArgs.javaLanguageLevel).isEqualTo("1.7")
assertThat(parseArgs.kotlinLanguageLevel).isEqualTo("1.8")
assertThat(parseArgs.enableCheckDependencies).isTrue()
}
}
7 changes: 7 additions & 0 deletions toolchains/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
"""Android Lint Toolchain."""

_ATTRS = dict(
android_lint_enable_check_dependencies = attr.bool(
default = False,
doc = """Enables the dependency analysis features within lint. Warning: This feature is extremely
expensive and will slow down Lint. It's recommended that you leave this feature disabled unless it's
explicitly required.
""",
),
compile_sdk_version = attr.string(
default = "34",
doc = "The Android SDK version to compile against.",
Expand Down

0 comments on commit 2e0e4f0

Please sign in to comment.