Repository: JetBrains/compose-multiplatform
File: gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/wixToolset.kt (Kotlin)

Code before:
internal const val DOWNLOAD_WIX_TOOLSET_TASK_NAME = "downloadWix"
internal const val UNZIP_WIX_TOOLSET_TASK_NAME = "unzipWix"
internal const val WIX_PATH_ENV_VAR = "WIX_PATH"
internal const val DOWNLOAD_WIX_PROPERTY = "compose.desktop.application.downloadWix"

internal fun JvmApplicationContext.configureWix() {
    check(currentOS == OS.Windows) { "Should not be called for non-Windows OS: $currentOS" }

    val wixPath = System.getenv()[WIX_PATH_ENV_VAR]
    if (wixPath != null) {
        val wixDir = File(wixPath)
        check(wixDir.isDirectory) { "$WIX_PATH_ENV_VAR value is not a valid directory: $wixDir" }
        project.eachWindowsPackageTask {
            wixToolsetDir.set(wixDir)
        }
        return
    }

    if (project.findProperty(DOWNLOAD_WIX_PROPERTY) == "false") return

    val root = project.rootProject
    val wixDir = project.gradle.gradleUserHomeDir.resolve("compose-jb")
    val fileName = "wix311"
    val zipFile = wixDir.resolve("$fileName.zip")
    val unzipDir = root.layout.buildDirectory.dir(fileName)
    val download = root.tasks.maybeCreate(DOWNLOAD_WIX_TOOLSET_TASK_NAME, Download::class.java).apply {
        onlyIf { !zipFile.isFile }
        src("https://github.com/wixtoolset/wix3/releases/download/wix3112rtm/wix311-binaries.zip")
        dest(zipFile)
    }
    val unzip = root.tasks.maybeCreate(UNZIP_WIX_TOOLSET_TASK_NAME, Copy::class.java).apply {
        dependsOn(download)
        from(project.zipTree(zipFile))
        destinationDir = unzipDir.ioFile
    }
    project.eachWindowsPackageTask {
        dependsOn(unzip)
        wixToolsetDir.set(unzipDir)
    }
}

private fun Project.eachWindowsPackageTask(fn: AbstractJPackageTask.() -> Unit) {
    tasks.withType(AbstractJPackageTask::class.java).configureEach { packageTask ->
        if (packageTask.targetFormat.isCompatibleWith(OS.Windows)) {
            packageTask.fn()
        }
    }
}

Review comment (refactor):
The `maybeCreate` suppose to return the task if it exists ([the source code](https://github.com/gradle/gradle/blob/8ec670c5eeddcdaba6ee840360cb4eeda788558e/subprojects/core/src/main/java/org/gradle/api/internal/AbstractNamedDomainObjectContainer.java#L60) calls `findByName`).

Does it definitely fixes the issue?

Code after:
internal const val DOWNLOAD_WIX_TOOLSET_TASK_NAME = "downloadWix"
internal const val UNZIP_WIX_TOOLSET_TASK_NAME = "unzipWix"
internal const val WIX_PATH_ENV_VAR = "WIX_PATH"
internal const val DOWNLOAD_WIX_PROPERTY = "compose.desktop.application.downloadWix"

internal fun JvmApplicationContext.configureWix() {
    check(currentOS == OS.Windows) { "Should not be called for non-Windows OS: $currentOS" }

    val wixPath = System.getenv()[WIX_PATH_ENV_VAR]
    if (wixPath != null) {
        val wixDir = File(wixPath)
        check(wixDir.isDirectory) { "$WIX_PATH_ENV_VAR value is not a valid directory: $wixDir" }
        project.eachWindowsPackageTask {
            wixToolsetDir.set(wixDir)
        }
        return
    }

    if (project.findProperty(DOWNLOAD_WIX_PROPERTY) == "false") return

    val root = project.rootProject
    val wixDir = project.gradle.gradleUserHomeDir.resolve("compose-jb")
    val fileName = "wix311"
    val zipFile = wixDir.resolve("$fileName.zip")
    val unzipDir = root.layout.buildDirectory.dir(fileName)
    val download = root.tasks.findByName(DOWNLOAD_WIX_TOOLSET_TASK_NAME) ?: root.tasks.maybeCreate(
        DOWNLOAD_WIX_TOOLSET_TASK_NAME,
        Download::class.java
    ).apply {
        onlyIf { !zipFile.isFile }
        src("https://github.com/wixtoolset/wix3/releases/download/wix3112rtm/wix311-binaries.zip")
        dest(zipFile)
    }
    val unzip = root.tasks.findByName(UNZIP_WIX_TOOLSET_TASK_NAME) ?: root.tasks.maybeCreate(
        UNZIP_WIX_TOOLSET_TASK_NAME,
        Copy::class.java
    ).apply {
        dependsOn(download)
        from(project.zipTree(zipFile))
        destinationDir = unzipDir.ioFile
    }
    project.eachWindowsPackageTask {
        dependsOn(unzip)
        wixToolsetDir.set(unzipDir)
    }
}

private fun Project.eachWindowsPackageTask(fn: AbstractJPackageTask.() -> Unit) {
    tasks.withType(AbstractJPackageTask::class.java).configureEach { packageTask ->
        if (packageTask.targetFormat.isCompatibleWith(OS.Windows)) {
            packageTask.fn()