JosefNemec/Playnite · error · Exception

Cannot execute script, no runtime given.

Error message

Cannot execute script, no runtime given.

What it means

Thrown by ExecuteScript (GamesEditor) when execute is true and a script is present but the supplied PowerShell runtime is null. The method needs a live runtime to invoke scripts; a null runtime means the caller did not initialize scripting for this context.

Source

Thrown at source/Playnite/GamesEditor.cs:1635

        public bool ExecuteScriptAction(
            IPowerShellRuntime runtime,
            string script,
            Game game,
            bool execute,
            bool global,
            GameScriptType type,
            Dictionary<string, object> vars = null)
        {
            if (!execute || script.IsNullOrWhiteSpace())
            {
                return true;
            }

            try
            {
                if (runtime == null)
                {
                    throw new Exception("Cannot execute script, no runtime given.");
                }

                if (!PowerShellRuntime.IsInstalled)
                {
                    throw new Exception(resources.GetString("LOCErrorPowerShellNotInstalled"));
                }

                var scriptVars = new Dictionary<string, object>
                {
                    {  "PlayniteApi", Application.PlayniteApiGlobal },
                    {  "Game", game.GetCopy() }
                };

                vars?.ForEach(a => scriptVars.AddOrUpdate(a.Key, a.Value));
                var expandedScript = game.ExpandVariables(script);
                var dir = game.ExpandVariables(game.InstallDirectory, true);
                if (!dir.IsNullOrEmpty() && Directory.Exists(dir))
                {

View on GitHub (pinned to 5911f4e964)

Solutions

  1. Construct and pass a PowerShellRuntime when calling ExecuteScript for any script-bearing GameScriptType.
  2. Skip the call (or no-op) when there is no script to run rather than passing null runtime.
  3. Add a unit test asserting runtime is non-null on all ExecuteScript call sites.

Example fix

// before
if (runtime == null) { throw new Exception("Cannot execute script, no runtime given."); }

// after — fail fast at call site with clear contract
if (script.IsNullOrWhiteSpace()) return true;
if (runtime == null) throw new ArgumentNullException(nameof(runtime), "A PowerShellRuntime is required to execute game scripts.");
Defensive patterns

Strategy: validation

Validate before calling

// Only invoke ExecuteScript when a runtime is available.
if (!script.IsNullOrWhiteSpace() && execute) {
    if (runtime == null) throw new ArgumentNullException(nameof(runtime));
    ExecuteScript(game, type, script, runtime, vars);
}

Type guard

static bool CanExecuteScript(string script, bool execute, PowerShellRuntime runtime) =>
    (!script.IsNullOrWhiteSpace() && execute) ? runtime != null : true;

Prevention

When it happens

Trigger: Calling ExecuteScript with a non-empty script and execute=true while passing runtime=null. The guard precedes any script compilation, so it fails fast before touching PowerShell.

Common situations: A code path runs game install/uninstall scripts without constructing a PowerShellRuntime; a refactor changed runtime ownership and a branch forgot to pass it; scripts invoked during shutdown after runtime disposal.

Related errors


AI-assisted analysis of JosefNemec/Playnite@5911f4e964 (2026-08-13). Data as JSON: /api/errors/1b3a04c32975f28a. Report an issue: GitHub.