{"record":{"id":"56a92e32f769c1aa","repo":"anomalyco/sst","slug":"panic-err-56a92e","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"sdk/golang/resource/resource.go","lineNumber":99,"sourceCode":"\t\treturn nil, ErrNotFound\n\t}\n\tnext, ok := casted[path[0]]\n\tif !ok {\n\t\treturn nil, ErrNotFound\n\t}\n\treturn get(next, path[1:]...)\n}\n\nfunc loadFromEnv() {\n\tfor _, item := range os.Environ() {\n\t\tpair := strings.SplitN(item, \"=\", 2)\n\t\tkey := pair[0]\n\t\tvalue := pair[1]\n\t\tif strings.HasPrefix(key, \"SST_RESOURCE_\") {\n\t\t\tvar result map[string]interface{}\n\t\t\terr := json.Unmarshal([]byte(value), &result)\n\t\t\tif err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\t\t\tresources[strings.TrimPrefix(key, \"SST_RESOURCE_\")] = result\n\t\t}\n\t}\n\n\t// Load consolidated resources JSON (used on Windows to avoid uppercasing)\n\tif consolidated := os.Getenv(\"SST_RESOURCES_JSON\"); consolidated != \"\" {\n\t\tvar parsed map[string]interface{}\n\t\tif err := json.Unmarshal([]byte(consolidated), &parsed); err == nil {\n\t\t\tfor k, v := range parsed {\n\t\t\t\tresources[k] = v\n\t\t\t}\n\t\t}\n\t}\n}\n","sourceCodeStart":81,"sourceCodeEnd":115,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/sdk/golang/resource/resource.go#L81-L115","documentation":"The SST Go SDK loads linked-resource metadata from SST_RESOURCE_* environment variables at package init (via loadFromEnv). Each variable's value must be a JSON object; if json.Unmarshal fails, the SDK panics — because init() cannot return errors, malformed resource data is treated as fatal at process start.","triggerScenarios":"Any program importing sdk/golang/resource where some environment variable prefixed with SST_RESOURCE_ (e.g. SST_RESOURCE_MY_BUCKET) contains a value that is not a valid JSON object — truncated JSON, single quotes instead of double quotes, plain strings/numbers, or a JSON array.","commonSituations":"Manually exporting a resource var in a shell where quoting mangles the JSON (`export SST_RESOURCE_X={\"arn\":...}` — the shell strips the braces); copying an env var through CI systems that escape quotes; defining a link in sst.config.ts whose value is not a plain JSON object; Windows env-var size mangling.","solutions":["Re-run your function through `sst dev` or a fresh `sst deploy` so SST regenerates the SST_RESOURCE_* variables from real resource outputs instead of hand-editing them","Validate the value is a JSON object: `echo \"$SST_RESOURCE_MY_BUCKET\" | jq -e 'type == \"object\"'` before starting the app","Fix shell quoting — single-quote the JSON: `export SST_RESOURCE_MY_BUCKET='{\"BucketName\":\"...\"}'`","Check the linked resource's properties in sst.config.ts; only JSON-serializable plain objects can be linked","Wrap the SDK import path: if you cannot fix the env, unset offending SST_RESOURCE_* vars (the SDK skips them), though linked-resource Lookups will fail"],"exampleFix":"// before\nexport SST_RESOURCE_MY_BUCKET={\"BucketName\": \"my-bucket\"}\n// shell strips braces -> invalid JSON -> panic\n// after\nexport SST_RESOURCE_MY_BUCKET='{\"BucketName\": \"my-bucket\"}'","handlingStrategy":"validation","validationCode":"import \"encoding/json\"\nimport \"os\"\nimport \"strings\"\n\nfunc validateSSTResourceEnv() error {\n    for _, item := range os.Environ() {\n        pair := strings.SplitN(item, \"=\", 2)\n        if strings.HasPrefix(pair[0], \"SST_RESOURCE_\") {\n            var m map[string]any\n            if err := json.Unmarshal([]byte(pair[1]), &m); err != nil {\n                return fmt.Errorf(\"%s is not a JSON object: %w\", pair[0], err)\n            }\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// SDK panics inside init(); you cannot catch it in-process — run the validator in a pre-launch check and fail fast with a clear message:\nif err := validateSSTResourceEnv(); err != nil {\n    log.Fatalf(\"aborting before SST SDK init: %v\", err)\n}","preventionTips":["Never hand-write SST_RESOURCE_* values; let `sst dev`/`sst deploy` inject them","Quote JSON in single quotes when exporting env vars in shells","Validate link payloads in sst.config.ts are plain JSON-serializable objects","Add a CI smoke test that imports the SDK with the same env vars the runtime uses"],"tags":["go","panic","json","environment","init-time"],"backgroundTag":"invalid-json-in-env-var","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}