Skip to content

Env vars are not bound when CoreCliktCommand is used #615

Description

@JakeWharton

CliktCommand works fine. CoreSuspendingCliktCommand also broken.

Works:

fun main(vararg args: String) {
	ExampleCommand().main(args)
}

private const val envVarName = "THIS_IS_A_TEST"

private class ExampleCommand : CliktCommand() {
	private val value by option(envvar = envVarName)

	override fun run() {
		println("env: " + System.getenv(envVarName))
		println("property: $value")
	}
}
❯ gw -q installDist && THIS_IS_A_TEST=hello ./build/install/clikt-env-vars/bin/clikt-env-vars
env: hello
property: hello

Does not work:

fun main(vararg args: String) {
	ExampleCommand().main(args)
}

private const val envVarName = "THIS_IS_A_TEST"

private class ExampleCommand : CoreCliktCommand() {
	private val value by option(envvar = envVarName)

	override fun run() {
		println("env: " + System.getenv(envVarName))
		println("property: $value")
	}
}
❯ gw -q installDist && THIS_IS_A_TEST=hello ./build/install/clikt-env-vars/bin/clikt-env-vars
env: hello
property: null

Activity

  1. JakeWharton commented on Sep 5, 2025

    @JakeWharton
    ContributorAuthor

    Okay so this is documented: https://ajalt.github.io/clikt/advanced/#core-module

    However, it seems like Mordant really should not be required for most of these things. Reading env vars in a multiplatform context is a few lines. Reading files is more lines, but not a ridiculous amount of lines. In that list, the only thing that screams Mordant to me is "Text wrapping, formatting, markdown, or color support".

    Can we work to move the others into having core support?

  2. JakeWharton commented on Apr 17, 2026

    @JakeWharton
    ContributorAuthor

    @ajalt Are you open to this? I'm happy to work on it. I just got hit by it once again.

  3. ajalt commented on Apr 17, 2026

    @ajalt
    Owner

    Right now CliktCore doesn't have any target specific code, so it can easily compile to every target. I'm a little hesitant to add this for that reason.

  4. JakeWharton commented on Apr 17, 2026

    @JakeWharton
    ContributorAuthor

    What about throwing an exception if you specify an envvar binding in a CoreCliktCommand without a envvar provider specified?

  5. ajalt commented on Apr 20, 2026

    @ajalt
    Owner

    That's a good idea. Have you ran into this with the automatic envvar name assignment, or is it enough to throw if you explicitly set an envvar name?

  6. JakeWharton commented on Apr 20, 2026

    @JakeWharton
    ContributorAuthor

    Oh, I didn't know there was an automatic one. It's only bit me when I set them explicitly for use in a Docker container.

  7. oharaandrew314 commented on Sep 24, 2026

    @oharaandrew314

    For JVM, I managed to work around this with a small inclusion to my main command. I chose not to use the clikt jvm module because I'm optimizing for minimal footprint; mordant and JNA were unnecessary.

    class SystemCli: CoreCliktCommand("system") {
        init {
            context {
                readEnvvar = System::getenv // jvm-only
            }
        }
    
        override fun run() = Unit
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions