Skip to content

Use @DslMarker or other mechanism to restrict DSL scopes #23005

Description

@octylFractal

Expected Behavior

Certain blocks should not be available in nested scopes, for example the top-level dependencies should not be available in Test Suites blocks to prevent confusion with its dependencies.

Current Behavior / Context

In Kotlin DSL, it is easy to do:

testing {
    suites {
        named("test") { // Should be named<JvmTestSuite>("test")
            dependencies {
                // Oops, this is the top-level one!
            }
        }
    }
}

This should be a warning or error.

Path to Implementation

Kotlin's @DslMarker seems promising, though we would need to be careful to not overly restrict scopes. It might be possible for us to ask the Kotlin team for more features in this vein to allow us to allow certain functions to be kept.

One issue with @DslMarker that is immediately obvious is that there is no way to access the top-level receiver without saving it in a variable explicitly (val topLevelThis = this before a block). It would be nice if we could define e.g. this@project / this@script / this@build implicitly.

Also, it appears we already have @GradleDsl for marking this, though it isn't widely used.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions