Skip to content

Provider.orElse(<provider>) tests presence eagerly #13333

@ZacSweers

Description

@ZacSweers

Expected Behavior

If I have a provider used in a task, it should not test presence during configuration

Current Behavior

It does, and results in an exception

Caused by: java.lang.IllegalStateException: Cannot obtain value from provider of environment variable 'USER' at configuration time.
Use a provider returned by 'forUseAtConfigurationTime()' instead.

Context

I want to use an environmentVariable provider and want to default it if absent.

Steps to Reproduce

Drop this into a build.gradle.kts file

val userProvider = project.providers.environmentVariable("USER")
    .orElse(project.provider { "absent" })

abstract class TestTask : DefaultTask() {
  @get:Input
  abstract val userProp: Property<String>

  @TaskAction
  fun printUser() {
    println(userProp.get())
  }
}

project.tasks.register<TestTask>("testTask") {
  userProp.set(userProvider)
}

Then run ./gradlew testTask

You'll see this failure

Could not determine the dependencies of task ':testTask'.
> Cannot obtain value from provider of environment variable 'USER' at configuration time.
  Use a provider returned by 'forUseAtConfigurationTime()' instead.

Your Environment

Build scan URL:

Metadata

Metadata

Assignees

Labels

a:bugThis doesn't work as expectedin:configuration-modellazy api, domain object container

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions