anomalyco/sst · error

command exited with code %d

Error message

command exited with code %d

What it means

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.

Source

Thrown at pkg/server/resource/run.go:101

		return err
	}
	stderr, err := cmd.StderrPipe()
	if err != nil {
		return err
	}
	reader := io.MultiReader(stdout, stderr)
	err = cmd.Start()
	if err != nil {
		return err
	}
	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		bus.Publish(&common.StdoutEvent{Line: scanner.Text()})
	}
	slog.Info("waiting for command to finish", "cmd", cmd.String())
	cmd.Wait()
	if cmd.ProcessState.ExitCode() > 0 {
		return fmt.Errorf("command exited with code %d", cmd.ProcessState.ExitCode())
	}
	return nil
}

View on GitHub (pinned to a0bd20f762)

Solutions

  1. Read the published stdout/stderr lines above the error for the actual command failure
  2. Run the same command manually in input.Cwd to reproduce
  3. Install missing dependencies or fix the script the command invokes
  4. Set required env vars via the Run resource's env input
  5. Verify the cwd exists and required binaries are on PATH

Example fix

// before: fails when node_modules absent
new sst.Run("Build", { command: "npm run build", cwd: "site" })
// after
new sst.Run("Build", { command: "npm install && npm run build", cwd: "site" })
Defensive patterns

Strategy: try-catch

Validate before calling

// before declaring the Run resource
const { status } = spawnSync("sh", ["-c", command], { cwd, env: { ...process.env, ...env } });
if (status !== 0) throw new Error(`command will fail with exit code ${status}`);

Try / catch

try {
  new sst.Run("Build", { command, cwd, env });
} catch (e) {
  const m = /command exited with code (\d+)/.exec(e.message);
  if (m) console.error(`build failed, exit ${m[1]} — see stdout/stderr above`);
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of anomalyco/sst@a0bd20f762 (2026-08-30). Data as JSON: /api/errors/d4b71ed7efff6717. Report an issue: GitHub.