Skip to content

Commit

Permalink
Work around actions/toolkit#1925 better and more completely
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Feb 24, 2025
1 parent 00f1a06 commit 75ccefa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package net.kautler

import net.kautler.dao.action.GitHubAction
import net.kautler.util.npm
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.accessors.dm.LibrariesForKotlinWrappers
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec
Expand Down Expand Up @@ -156,6 +155,12 @@ dependencies {
val syncDistribution by tasks.registering(Sync::class) {
from(setupWslDistributionFiles)
into(layout.buildDirectory.dir("distributions"))
// work-around for https://github.com/actions/toolkit/issues/1925
filesMatching("index.mjs") {
filter {
it.replace("stats = yield exports.stat", "stats = yield exports.lstat")
}
}
}

tasks.assemble {
Expand Down
58 changes: 4 additions & 54 deletions src/jsMain/kotlin/net/kautler/github/action/setup_wsl/SetupWsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import actions.exec.ExecOptions
import actions.exec.exec
import actions.io.mkdirP
import actions.io.mv
import actions.io.rmRF
import actions.io.which
import actions.tool.cache.cacheDir
import actions.tool.cache.downloadTool
Expand All @@ -58,7 +57,6 @@ import node.buffer.BufferEncoding
import node.fs.exists
import node.fs.mkdtemp
import node.fs.readdir
import node.fs.stat
import node.fs.writeFile
import node.os.tmpdir
import node.path.path
Expand All @@ -71,12 +69,6 @@ import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import actions.tool.cache.extractZip as toolCacheExtractZip

private const val BAD_WSL_EXE_PATH =
"C:\\Users\\runneradmin\\AppData\\Local\\Microsoft\\WindowsApps\\wsl.exe"

private const val BAD_WSLCONFIG_EXE_PATH =
"C:\\Users\\runneradmin\\AppData\\Local\\Microsoft\\WindowsApps\\wslconfig.exe"

suspend fun wslOutput(vararg args: String): String {
val stdoutBuilder = StringBuilder()
val stdoutBuilderUtf16Le = StringBuilder()
Expand Down Expand Up @@ -387,31 +379,12 @@ suspend fun verifyWindowsEnvironment() {
}

suspend fun installWsl() {
// part of work-around for https://github.com/actions/toolkit/issues/1925
val deleteWslExe =
runCatching { stat(BAD_WSL_EXE_PATH).isFile() }
.getOrDefault(false)
.not()
val deleteWslConfigExe =
runCatching { stat(BAD_WSLCONFIG_EXE_PATH).isFile() }
.getOrDefault(false)
.not()

exec(
commandLine = "pwsh",
args = arrayOf("-Command", """Start-Process wsl "--install --no-distribution""""),
options = ExecOptions(ignoreReturnCode = true)
)

waitForWslStatusNotContaining("is not installed", 5.minutes) {
// part of work-around for https://github.com/actions/toolkit/issues/1925
if (deleteWslExe) {
rmRF(BAD_WSL_EXE_PATH)
}
if (deleteWslConfigExe) {
rmRF(BAD_WSLCONFIG_EXE_PATH)
}
}
waitForWslStatusNotContaining("is not installed", 5.minutes)
}

suspend fun installDistribution() {
Expand All @@ -420,31 +393,12 @@ suspend fun installDistribution() {
)

if (wslVersion() != 1u) {
// part of work-around for https://github.com/actions/toolkit/issues/1925
val deleteWslExe =
runCatching { stat(BAD_WSL_EXE_PATH).isFile() }
.getOrDefault(false)
.not()
val deleteWslConfigExe =
runCatching { stat(BAD_WSLCONFIG_EXE_PATH).isFile() }
.getOrDefault(false)
.not()

retry(10) {
executeWslCommand(
wslArguments = arrayOf("--update")
)
}

// part of work-around for https://github.com/actions/toolkit/issues/1925
waitForWslStatusNotContaining("WSL is finishing an upgrade...") {
if (deleteWslExe) {
rmRF(BAD_WSL_EXE_PATH)
}
if (deleteWslConfigExe) {
rmRF(BAD_WSLCONFIG_EXE_PATH)
}
}
waitForWslStatusNotContaining("WSL is finishing an upgrade...")
}

exec(
Expand All @@ -456,17 +410,13 @@ suspend fun installDistribution() {

suspend fun waitForWslStatusNotContaining(
text: String,
duration: Duration = 30.seconds,
preAction: suspend () -> Unit = {}
duration: Duration = 30.seconds
) {
(2..duration.inWholeSeconds)
.asFlow()
.onEach { delay(1.seconds) }
.onStart { emit(1) }
.map {
preAction()
wslOutput("--status")
}
.map { wslOutput("--status") }
.firstOrNull { !it.contains(text) }
}

Expand Down

0 comments on commit 75ccefa

Please sign in to comment.