Skip to content
Edit this page

003: Hybrid Task Runner and Execution

Context

Windows builds on Linux rarely involve a single executable call. Projects combine Wine compilation (dotnet build, python.exe, ISCC.exe), Linux host commands (cleaning directories, patching downloaded tools, installing mods), active-process safety checks before modifying locked game files, and multi-target Windows builds.

Benchmark

General host task runners (just, Taskfile, GNU Make) handle host shell pipelines cleanly, yet require custom wrapper scripts for Wine PATH merging, batch escaping, Z:\ drive translation, headless display suppression, and wineserver persistence. CoopCourier previously combined scripts/neorune.nu and a Makefile to orchestrate Proton .NET builds and mod installation. That older setup suffered from batch-file collisions whenever two commands reused the same static .bat path.

Interaction flow

Execution proceeds through DAG ordering, guard inspection, runtime preparation, and per-step invocation:

  1. Parse CLI arguments for winesmith run <task> [KEY=VALUE ...], winesmith exec [flags] -- <cmd...>, or winesmith shell.
  2. Resolve the active target profile ([windows], --target <name>, or --all-targets) and merge positional KEY=VALUE variables.
  3. Topologically sort the depends graph (reporting cyclic chains cleanly) and skip runner download and prefix creation when the resolved graph contains only host steps.
  4. Before running each task, scan Linux /proc/<pid>/cmdline and /proc/<pid>/comm for any forbid_running regex match, aborting with exit code 4 if found.
  5. Render MiniJinja templates ({{ var }}, {{ winpath(path) }}, {{ prefix_drive_c }}, {% if ... %}) and write a unique batch wrapper C:\winesmith\tmp\wine-<pid>-<seq>.cmd containing merged PATH and [env]. Execute Wine with the selected display adapter (none, xvfb, or host), keep wineserver -p<secs> alive if configured, and delete the wrapper file on drop.
  6. Run host steps via /bin/sh -eu -c in the rendered task working directory with WINESMITH_PROJECT_ROOT, WINESMITH_PREFIX_DIR, WINESMITH_PREFIX_DRIVE_C, and optional WINESMITH_TARGET exported.

Visual design

The sequence diagram below traces winesmith run across Wine compilation, process guard checks, and Linux host steps:

sequenceDiagram
    actor User
    participant CLI as winesmith
    participant Guard as Process Guard
    participant Wine as Runner (Wine / Proton)
    participant Host as Linux Host Shell

    User->>CLI: winesmith run install config=Release
    CLI->>CLI: Sort depends DAG and render MiniJinja + winpath()
    CLI->>Wine: Execute build step via unique C:\winesmith\tmp\wine-PID-1.cmd
    Wine-->>CLI: Exit code 0
    CLI->>Guard: Scan /proc for forbid_running regexes
    Guard-->>CLI: No conflicting process
    CLI->>Host: Execute install host step via /bin/sh -eu -c
    Host-->>CLI: Exit code 0
    CLI-->>User: Exit code 0

Implementation

Safe path translation rules

Two translation rules keep mixed Linux paths and Windows slash switches intact:

  • winpath("relative/or/abs/path") normalizes the path relative to project_root. When the path sits inside a configured [mounts] directory (such as w = "."), it emits W:\...; otherwise it falls back to Z:\....
  • Inside winesmith exec, slash-prefixed flags (/p:Configuration=Release, /c, /VERYSILENT, /DVersion=1.0) pass through untouched unless the token names an existing Linux file or directory. Tokens starting explicitly with ./, ../, or ~/ also trigger translation.

Validated parity manifest (CoopCourier/winesmith.toml)

This manifest replaced CoopCourier's Makefile and scripts/neorune.nu while keeping local Steam game paths in gitignored winesmith.local.toml:

[project]
name = "CoopCourier"

[windows]
version = "win10"
arch = "win64"
display = "none"

[runner]
kind = "proton"
version = "Proton 9.0"

[prefix]
mode = "shared"
name = "neorune-net10"

[mounts]
w = "."

[vars]
config = "Debug"
profile = false
mod_name = "CoopCourier"
project = "source/CoopCourier.csproj"
out = "Release"
game_dir = ""
game_compat_data = ""

[symlinks]
"users/steamuser/AppData/Local/Dungeons2/Saved/SaveGames" = "{% if game_compat_data %}{{ game_compat_data }}/pfx/drive_c/users/steamuser/AppData/Local/Dungeons2/Saved/SaveGames{% endif %}"

[toolchains.dotnet]
version = "10.0.403"
shared_nuget_cache = true
templates = ["NeoRune.Templates"]
tools = ["NeoRune.Tool"]
post_install = [
  { host = "python3 scripts/patch-neorune.py '{{ prefix_drive_c }}/users/steamuser/.dotnet/tools/.store/neorune.tool/1.0.0/neorune.tool/1.0.0/tools/net10.0/any/neorune.dll'" }
]

[tasks.build]
description = "Build CoopCourier mod with .NET 10 inside Proton"
run = "dotnet build {{ winpath(project) }} -c {{ config }} -p:NeoRuneInstall=false -p:AllowMissingPrunePackageData=true{% if profile %} -p:NeoRuneProfile=true{% endif %}"

[tasks.test]
description = "Run NeoRune test suite for CoopCourier"
run = "neorune test --project {{ winpath(project) }}"

[tasks.install]
description = "Install compiled .pak files into Minecraft Dungeons II ~mods"
forbid_running = ["(?i)Dungeons.*\\.exe"]
host = """
if [ -z '{{ game_dir }}' ]; then
  echo 'Set `game_dir` in winesmith.local.toml or pass `game_dir=/path/to/game`.' >&2
  exit 2
fi
mkdir -p '{{ game_dir }}/Dungeons/Content/Paks/~mods/{{ mod_name }}'
cp -r source/bin/NeoRune/Pak/. '{{ game_dir }}/Dungeons/Content/Paks/~mods/{{ mod_name }}'
"""

[tasks.clean]
description = "Remove local build outputs (bin and obj)"
host = "rm -rf source/bin source/obj"

Edge cases and constraints

Ad-hoc runs and signal interruption rely on strict cleanup invariants:

  • Zero-config invocations (winesmith exec --win win7 --arch win32 --toolchain python@3.8.10 -- python --version) build an in-memory target spec and reuse the spec-hashed shared prefix without requiring winesmith.toml.
  • WrapperGuard deletes each temporary wine-<pid>-<seq>.cmd script in Drop, including after non-zero child exits or SIGINT/SIGTERM forwarding.
  • Across --all-targets, targets execute sequentially and abort immediately with the failing target's exact exit code.