{"record":{"id":"1b3a04c32975f28a","repo":"JosefNemec/Playnite","slug":"cannot-execute-script-no-runtime-given","errorCode":null,"errorMessage":"Cannot execute script, no runtime given.","messagePattern":"Cannot execute script, no runtime given\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"source/Playnite/GamesEditor.cs","lineNumber":1635,"sourceCode":"        public bool ExecuteScriptAction(\r\n            IPowerShellRuntime runtime,\r\n            string script,\r\n            Game game,\r\n            bool execute,\r\n            bool global,\r\n            GameScriptType type,\r\n            Dictionary<string, object> vars = null)\r\n        {\r\n            if (!execute || script.IsNullOrWhiteSpace())\r\n            {\r\n                return true;\r\n            }\r\n\r\n            try\r\n            {\r\n                if (runtime == null)\r\n                {\r\n                    throw new Exception(\"Cannot execute script, no runtime given.\");\r\n                }\r\n\r\n                if (!PowerShellRuntime.IsInstalled)\r\n                {\r\n                    throw new Exception(resources.GetString(\"LOCErrorPowerShellNotInstalled\"));\r\n                }\r\n\r\n                var scriptVars = new Dictionary<string, object>\r\n                {\r\n                    {  \"PlayniteApi\", Application.PlayniteApiGlobal },\r\n                    {  \"Game\", game.GetCopy() }\r\n                };\r\n\r\n                vars?.ForEach(a => scriptVars.AddOrUpdate(a.Key, a.Value));\r\n                var expandedScript = game.ExpandVariables(script);\r\n                var dir = game.ExpandVariables(game.InstallDirectory, true);\r\n                if (!dir.IsNullOrEmpty() && Directory.Exists(dir))\r\n                {\r","sourceCodeStart":1617,"sourceCodeEnd":1653,"githubUrl":"https://github.com/JosefNemec/Playnite/blob/5911f4e964e628aa7a69c2030ab35101afa63067/source/Playnite/GamesEditor.cs#L1617-L1653","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nif (runtime == null) { throw new Exception(\"Cannot execute script, no runtime given.\"); }\n\n// after — fail fast at call site with clear contract\nif (script.IsNullOrWhiteSpace()) return true;\nif (runtime == null) throw new ArgumentNullException(nameof(runtime), \"A PowerShellRuntime is required to execute game scripts.\");","handlingStrategy":"validation","validationCode":"// Only invoke ExecuteScript when a runtime is available.\nif (!script.IsNullOrWhiteSpace() && execute) {\n    if (runtime == null) throw new ArgumentNullException(nameof(runtime));\n    ExecuteScript(game, type, script, runtime, vars);\n}","typeGuard":"static bool CanExecuteScript(string script, bool execute, PowerShellRuntime runtime) =>\n    (!script.IsNullOrWhiteSpace() && execute) ? runtime != null : true;","tryCatchPattern":null,"preventionTips":["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."],"tags":["scripting","powershell","null-argument","validation"],"backgroundTag":null,"analyzedSha":"5911f4e964e628aa7a69c2030ab35101afa63067","analyzedAt":"2026-08-13T17:38:25.713Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}