{"record":{"id":"d57240c224cd3149","repo":"direnv/direnv","slug":"panic-err","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/dotenv/parse.go","lineNumber":126,"sourceCode":"\t\tkey := match[1]\n\t\tvalue := match[2]\n\n\t\tparseValue(key, value, dotenv)\n\t}\n\n\t// If we end with an unclosed multi-line value, return an error\n\tif inMultiline {\n\t\treturn nil, fmt.Errorf(\"unclosed quoted value in .env file\")\n\t}\n\n\treturn dotenv, nil\n}\n\n// MustParse works the same as Parse but panics on error\nfunc MustParse(data string) map[string]string {\n\tenv, err := Parse(data)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn env\n}\n\nfunc parseValue(key string, value string, dotenv map[string]string) {\n\tif len(value) <= 1 {\n\t\tdotenv[key] = value\n\t\treturn\n\t}\n\n\tsingleQuoted := false\n\n\tif value[0:1] == \"'\" && value[len(value)-1:] == \"'\" {\n\t\t// single-quoted string, do not expand\n\t\tsingleQuoted = true\n\t\tvalue = value[1 : len(value)-1]\n\t} else if value[0:1] == `\"` && value[len(value)-1:] == `\"` {\n\t\tvalue = value[1 : len(value)-1]","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/direnv/direnv/blob/b00e451f547f39be7ab836d969054114a465a0f9/pkg/dotenv/parse.go#L108-L144","documentation":"MustParse is the panic-on-error variant of dotenv.Parse; when the input contains any parse failure (invalid line, unclosed quote, etc.) it panics with the wrapped error instead of returning it. The error is never a distinct type — it is whatever Parse returned.","triggerScenarios":"Any call to MustParse with malformed .env content, directly in tests (TestFailingMustParse) or via test helpers that assert valid fixtures.","commonSituations":"Unit tests feeding intentionally invalid data to verify panic behavior; developers using MustParse in non-test code with user-supplied or hand-edited content; fixtures drifted out of valid syntax.","solutions":["Use Parse and handle the error instead of MustParse when input is not statically valid","Validate/fix the input string before calling MustParse","In tests, use recover() if intentionally asserting the panic"],"exampleFix":"// before\nenv := dotenv.MustParse(userInput)\n// after\nenv, err := dotenv.Parse(userInput)\nif err != nil {\n    return fmt.Errorf(\"bad .env: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"func safeMustParse(s string) (m map[string]string) {\n    defer func() { if r := recover(); r != nil { m = nil } }()\n    return dotenv.MustParse(s)\n}","preventionTips":["Prefer Parse over MustParse for any non-constant input","Only call MustParse on literals or test fixtures known to be valid","Wrap MustParse with recover() in library code","Keep test fixtures linted so MustParse never panics unexpectedly"],"tags":["dotenv","panic","mustparse","parse"],"backgroundTag":"mustparse-panic","analyzedSha":"b00e451f547f39be7ab836d969054114a465a0f9","analyzedAt":"2026-09-05T21:39:49.983Z","contentChangedAt":"2026-09-05T21:39:49.983Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}