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
- Construct and pass a PowerShellRuntime when calling ExecuteScript for any script-bearing GameScriptType.
- Skip the call (or no-op) when there is no script to run rather than passing null runtime.
- 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
- Always construct a PowerShellRuntime at the entry point that may run scripts.
- Pass runtime explicitly; avoid optional/null runtime parameters on script-bearing paths.
- Static-analysis all ExecuteScript call sites for null runtime.
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
- Cannot load script file, uknown format.
- No profile provided.
- PowerShell 3.0 or newer is not installed.
- PowerShell 5.1 is not installed.
- Interactive session is already running
AI-assisted analysis of JosefNemec/Playnite@5911f4e964 (2026-08-13).
Data as JSON: /api/errors/1b3a04c32975f28a.
Report an issue: GitHub.