Lint's test infrastructure doesn't fully resolve Gradle project dependencies by default. The project(":foo:bar") call requires actual module resolution, which the test harness doesn't provide out of the box.
Solutions Option 1: Use allowCompilationErrors()
Since you're checking syntax, not actual resolution, you can allow compilation errors:
kotlin
@Test
fun `legacy project path syntax detects error`() {
lint()
.files(
gradle(
"""
apply plugin: 'com.android.library'
dependencies {
implementation project(":foo:bar")
}
"""
).indented()
)
.issues(GradleProjectPathDetector.LEGACY_PROJECT_PATH_SYNTAX)
.allowCompilationErrors() // Add this
.run()
.expect("")
.expectFixDiffs("")
}
Option 2: Mock the Project Structure
If you need actual project resolution, create a multi-module setup:
kotlin
@Test
fun `legacy project path syntax detects error`() {
lint()
.files(
gradle(
"""
apply plugin: 'com.android.library'
dependencies {
implementation project(":foo:bar")
}
"""
).indented(),
// Add settings.gradle
gradle(
"""
include ':foo:bar'
"""
).name("settings.gradle"),
// Add the subproject's build file
gradle(
"""
apply plugin: 'com.android.library'
"""
).path("foo/bar/build.gradle")
)
.issues(GradleProjectPathDetector.LEGACY_PROJECT_PATH_SYNTAX)
.run()
.expect("")
.expectFixDiffs("")
}
Option 3: Use PSI-based Detection (Recommended for Syntax Checks)
Since you're detecting syntax patterns rather than resolved dependencies, you likely don't need full Gradle resolution. Your detector should work on the raw AST/PSI:
kotlin
class GradleProjectPathDetector : Detector(), GradleScanner {
override fun checkMethodCall(
context: GradleContext,
statement: Call,
parent: UElement,
resolved: PsiMethod
) {
if (statement.methodName == "project" &&
statement.valueArguments.size == 1) {
// Report the issue
context.report(
LEGACY_PROJECT_PATH_SYNTAX,
statement,
context.getLocation(statement),
"Use type-safe project accessor instead of project(\":path\")"
)
}
}
}
With this approach, Option 1 (allowCompilationErrors()) should be sufficient.
Option 4: Check the Actual Error Message
Run your test as-is and see what error you get. Sometimes the test infrastructure provides helpful hints about what's missing. Remove the empty .expect("") temporarily to see what lint actually reports.
Most Likely Solution
For a syntax-based detector, use Option 1 with allowCompilationErrors(). The Gradle test infrastructure doesn't need to resolve dependencies for you to detect the pattern project(":foo:bar") in the source.
If that doesn't work, share the error message you're getting and I can provide more specific guidance!