{"record":{"id":"fc26af26b535ba94","repo":"itsfatduck/optimizerDuck","slug":"revert-shell-error-commandfailed-stderr-if-present-else","errorCode":null,"errorMessage":"Revert.Shell.Error.CommandFailed (stderr if present, else localized message with exit code)","messagePattern":"Revert\\.Shell\\.Error\\.CommandFailed \\(stderr if present, else localized message with exit code\\)","errorType":"exception","errorClass":"StepExecutionException","httpStatus":null,"severity":"error","filePath":"optimizerDuck/Domain/Revert/Steps/ShellRevertStep.cs","lineNumber":57,"sourceCode":"                .QueryPowerShellAsync(Command, logger)\n                .ConfigureAwait(false),\n            ShellType.CMD => await shell.QueryCMDAsync(Command, logger).ConfigureAwait(false),\n            _ => new ShellResult\n            {\n                Command = Command,\n                Stdout = \"\",\n                Stderr = Loc.Instance[\"Revert.Shell.Error.UnknownShellType\"],\n                ExitCode = 1,\n                Duration = TimeSpan.Zero,\n            },\n        };\n\n        if (result.ExitCode != 0)\n        {\n            var error = !string.IsNullOrWhiteSpace(result.Stderr)\n                ? result.Stderr\n                : Loc.Instance[\"Revert.Shell.Error.CommandFailed\", result.ExitCode];\n            throw new StepExecutionException(error, result.Stderr);\n        }\n\n        return true;\n    }\n\n    /// <inheritdoc />\n    public JObject ToData()\n    {\n        return new JObject\n        {\n            [nameof(ShellType)] = ShellType.ToString(),\n            [nameof(Command)] = Command,\n        };\n    }\n\n    /// <summary>\n    ///     Deserializes a <see cref=\"ShellRevertStep\" /> from JSON data.\n    /// </summary>","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/itsfatduck/optimizerDuck/blob/36acf585ae122a09f0e29eb4acedfaf94e81c0de/optimizerDuck/Domain/Revert/Steps/ShellRevertStep.cs#L39-L75","documentation":"ShellRevertStep.ExecuteAsync throws StepExecutionException when the revert command run through ShellService (cmd.exe or PowerShell) returns a non-zero exit code. The exception message is the command's stderr if present, otherwise the localized 'Revert.Shell.Error.CommandFailed' formatted with the exit code; stderr is attached as the detail.","triggerScenarios":"Undo command executed via ShellRevertStep (ShellType CMD/PowerShell + stored Command) exits non-zero: the command references a deleted file/key, PowerShell parsing error, insufficient rights, or the environment changed since apply so the stored command no longer works.","commonSituations":"Reverting a shell tweak whose target (file, task, key) was removed by Windows update or user; stored PowerShell one-liner broken by module removal or execution policy change; antivirus blocking reg.exe/powershell.exe invocation.","solutions":["Read the exception detail (result.Stderr) — it names the real cause; fix that and re-run the revert.","Run the app elevated if the command needs admin rights.","Execute the stored command manually (from the revert JSON's step payload) to see the full error and adjust the environment.","If the change is already undone by other means, delete the revert file %LocalAppData%\\optimizerDuck\\Revert\\{id}.json.","Review AppSettings.Optimize.ShellTimeoutMs if the failure is a timeout-related non-zero exit."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// sanity-check the command target before running the revert\nif (cmd.Contains(\"reg delete\") && !RegistryService.KeyExists(path)) return; // nothing to undo","typeGuard":null,"tryCatchPattern":"try { await shellStep.ExecuteAsync(opCall); }\ncatch (StepExecutionException ex)\n{ logger.LogError(\"Shell revert failed: {Err}\", ex.Detail ?? ex.Message); }","preventionTips":["Keep revert commands simple and idempotent","Prefer registry-service steps over raw shell commands when possible","Run elevated; keep execution policy permissive for the app host","Read stderr from the exception detail, not the top-level message"],"tags":["csharp","shell","cmd","powershell","revert"],"backgroundTag":"command-not-found","analyzedSha":"36acf585ae122a09f0e29eb4acedfaf94e81c0de","analyzedAt":"2026-09-13T08:30:13.845Z","contentChangedAt":"2026-09-13T08:30:13.845Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}