{"record":{"id":"d06a510b535b75d3","repo":"pulumi/pulumi","slug":"could-not-import-stack-failed-to-marshal-stack-st","errorCode":null,"errorMessage":"could not import stack, failed to marshal stack state: %w","messagePattern":"could not import stack, failed to marshal stack state: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdk/go/auto/local_workspace.go","lineNumber":827,"sourceCode":"\t\t\tfmt.Errorf(\"failed to export stack, unable to unmarshall stack state: %w\", err), stdout, stderr, errCode,\n\t\t)\n\t}\n\n\treturn state, nil\n}\n\n// ImportStack imports the specified deployment state into a pre-existing stack.\n// This can be combined with ExportStack to edit a stack's state (such as recovery from failed deployments).\nfunc (l *LocalWorkspace) ImportStack(ctx context.Context, stackName string, state apitype.UntypedDeployment) error {\n\tf, err := os.CreateTemp(os.TempDir(), \"\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"could not import stack. failed to allocate temp file: %w\", err)\n\t}\n\tdefer func() { contract.IgnoreError(os.Remove(f.Name())) }()\n\n\tbytes, err := json.Marshal(state)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"could not import stack, failed to marshal stack state: %w\", err)\n\t}\n\n\t_, err = f.Write(bytes)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"could not import stack. failed to write out stack intermediate: %w\", err)\n\t}\n\n\tstdout, stderr, errCode, err := l.runPulumiCmdSync(ctx, \"stack\", \"import\", \"--file\", f.Name(), \"--stack\", stackName)\n\tif err != nil {\n\t\treturn newAutoError(fmt.Errorf(\"could not import stack: %w\", err), stdout, stderr, errCode)\n\t}\n\n\treturn nil\n}\n\n// StackOutputs gets the current set of Stack outputs from the last Stack.Up().\nfunc (l *LocalWorkspace) StackOutputs(ctx context.Context, stackName string) (OutputMap, error) {\n\t// standard outputs","sourceCodeStart":809,"sourceCodeEnd":845,"githubUrl":"https://github.com/pulumi/pulumi/blob/793f7b2e160db4321fb7fb6b0607461e01cb251e/sdk/go/auto/local_workspace.go#L809-L845","documentation":"ImportStack marshals the provided apitype.UntypedDeployment to JSON before writing it to the temp file. This error is returned if json.Marshal fails, which for UntypedDeployment practically means the struct contains values that cannot be serialized.","triggerScenarios":"Calling ImportStack with a state value that was hand-constructed or round-tripped through something producing non-marshalable content (e.g. invalid UTF-8 in deployment bytes) rather than coming from ExportStack.","commonSituations":"Custom tooling editing exported state and corrupting it; loading state from a source that produces invalid JSON data; unmarshal-then-marshal round trips losing valid types.","solutions":["Obtain state via ExportStack instead of hand-building apitype.UntypedDeployment","Validate the Deployment JSON yourself with json.Valid / a test Marshal before calling ImportStack","Check for invalid UTF-8 or control characters if the state was produced by another tool","Log the failing field by marshaling components separately to isolate the bad value"],"exampleFix":"// before\nerr := ws.ImportStack(ctx, \"dev\", handEditedState)\n// after - validate first\nif !json.Valid(handEditedState.Deployment) {\n  return errors.New(\"deployment bytes are not valid JSON\")\n}\nerr := ws.ImportStack(ctx, \"dev\", handEditedState)","handlingStrategy":"validation","validationCode":"raw, err := json.Marshal(state)\nif err != nil { return fmt.Errorf(\"state not marshalable: %w\", err) }\nif !json.Valid(state.Deployment) { return errors.New(\"deployment payload is not valid JSON\") }","typeGuard":"func validDeployment(state apitype.UntypedDeployment) bool {\n  return json.Valid(state.Deployment) && state.Version != 0\n}","tryCatchPattern":"if err := ws.ImportStack(ctx, stackName, state); err != nil {\n  if strings.Contains(err.Error(), \"failed to marshal stack state\") {\n    return fmt.Errorf(\"state produced outside ExportStack is corrupt: %w\", err)\n  }\n  return err\n}","preventionTips":["Only feed ImportStack state obtained from ExportStack","Run json.Valid on deployment bytes before import","Avoid manual edits to exported state or validate after edits","Marshal state once in a test to catch non-serializable fields early"],"tags":["go","automation-api","json-marshal","state"],"backgroundTag":"state-marshal-failed","analyzedSha":"793f7b2e160db4321fb7fb6b0607461e01cb251e","analyzedAt":"2026-08-31T09:36:43.099Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}