{"record":{"id":"d4b71ed7efff6717","repo":"anomalyco/sst","slug":"command-exited-with-code-d","errorCode":null,"errorMessage":"command exited with code %d","messagePattern":"command exited with code (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/server/resource/run.go","lineNumber":101,"sourceCode":"\t\treturn err\n\t}\n\tstderr, err := cmd.StderrPipe()\n\tif err != nil {\n\t\treturn err\n\t}\n\treader := io.MultiReader(stdout, stderr)\n\terr = cmd.Start()\n\tif err != nil {\n\t\treturn err\n\t}\n\tscanner := bufio.NewScanner(reader)\n\tfor scanner.Scan() {\n\t\tbus.Publish(&common.StdoutEvent{Line: scanner.Text()})\n\t}\n\tslog.Info(\"waiting for command to finish\", \"cmd\", cmd.String())\n\tcmd.Wait()\n\tif cmd.ProcessState.ExitCode() > 0 {\n\t\treturn fmt.Errorf(\"command exited with code %d\", cmd.ProcessState.ExitCode())\n\t}\n\treturn nil\n}\n","sourceCodeStart":83,"sourceCodeEnd":105,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/pkg/server/resource/run.go#L83-L105","documentation":"The Run resource executes a shell command (sh -c) during Create/Update and streams its output; this error is returned when the command finishes with a non-zero exit code. It means the user-supplied command itself failed — SST surfaces the exit code so the deployment/build step is marked failed. stdout/stderr were already published as StdoutEvent lines before this error.","triggerScenarios":"cmd.Wait() completes and cmd.ProcessState.ExitCode() > 0 for the command string in input.Command, run in input.Cwd with input.Env plus the inherited environment.","commonSituations":"Build commands failing on missing dependencies (npm/bun install not run), test failures, syntax errors in scripts, missing binaries on PATH, wrong cwd, env vars missing, command killed by signal (exit codes >128), or sh -c syntax errors.","solutions":["Read the published stdout/stderr lines above the error for the actual command failure","Run the same command manually in input.Cwd to reproduce","Install missing dependencies or fix the script the command invokes","Set required env vars via the Run resource's env input","Verify the cwd exists and required binaries are on PATH"],"exampleFix":"// before: fails when node_modules absent\nnew sst.Run(\"Build\", { command: \"npm run build\", cwd: \"site\" })\n// after\nnew sst.Run(\"Build\", { command: \"npm install && npm run build\", cwd: \"site\" })","handlingStrategy":"try-catch","validationCode":"// before declaring the Run resource\nconst { status } = spawnSync(\"sh\", [\"-c\", command], { cwd, env: { ...process.env, ...env } });\nif (status !== 0) throw new Error(`command will fail with exit code ${status}`);","typeGuard":null,"tryCatchPattern":"try {\n  new sst.Run(\"Build\", { command, cwd, env });\n} catch (e) {\n  const m = /command exited with code (\\d+)/.exec(e.message);\n  if (m) console.error(`build failed, exit ${m[1]} — see stdout/stderr above`);\n  throw e;\n}","preventionTips":["Run the command manually in the target cwd before adding it to the app","Chain dependency installation into the command (npm install && ...)","Pass required secrets/env via the Run env input instead of relying on CI defaults","Check that binaries invoked exist in the deploy environment's PATH"],"tags":["process","shell","exit-code","build"],"backgroundTag":"command-exited-nonzero","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}