{"record":{"id":"e7bffc848f9a1e85","repo":"go-delve/delve","slug":"first-argument-of-append-file-was-not-a-string","errorCode":null,"errorMessage":"first argument of append_file was not a string","messagePattern":"first argument of append_file was not a string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/terminal/starbind/starlark.go","lineNumber":127,"sourceCode":"\t\tpath, ok := args[0].(starlark.String)\n\t\tif !ok {\n\t\t\treturn nil, decorateError(thread, errors.New(\"argument of read_file was not a string\"))\n\t\t}\n\t\tbuf, err := os.ReadFile(string(path))\n\t\tif err != nil {\n\t\t\treturn nil, decorateError(thread, err)\n\t\t}\n\t\treturn starlark.String(string(buf)), nil\n\t})\n\tbuiltindoc(readFileBuiltinName, \"(Path)\", \"reads a file.\")\n\n\tenv.env[appendFileBuiltinName] = starlark.NewBuiltin(appendFileBuiltinName, func(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {\n\t\tif len(args) != 2 {\n\t\t\treturn nil, decorateError(thread, errors.New(\"wrong number of arguments\"))\n\t\t}\n\t\tpath, ok := args[0].(starlark.String)\n\t\tif !ok {\n\t\t\treturn nil, decorateError(thread, errors.New(\"first argument of append_file was not a string\"))\n\t\t}\n\t\tf, err := os.OpenFile(string(path), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o640)\n\t\tif err != nil {\n\t\t\treturn nil, decorateError(thread, err)\n\t\t}\n\t\tdefer f.Close()\n\t\t_, err = f.Write(toBytes(args[1]))\n\t\treturn starlark.None, decorateError(thread, err)\n\t})\n\tbuiltindoc(appendFileBuiltinName, \"(Path, Text)\", \"append text to the specified file.\")\n\n\tenv.env[writeFileBuiltinName] = starlark.NewBuiltin(writeFileBuiltinName, func(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {\n\t\tif len(args) != 2 {\n\t\t\treturn nil, decorateError(thread, errors.New(\"wrong number of arguments\"))\n\t\t}\n\t\tpath, ok := args[0].(starlark.String)\n\t\tif !ok {\n\t\t\treturn nil, decorateError(thread, errors.New(\"first argument of write_file was not a string\"))","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/terminal/starbind/starlark.go#L109-L145","documentation":"append_file's first argument must be a starlark.String containing the file path. If args[0] is any other Starlark type (int, list, None, etc.), the builtin returns this error before calling os.OpenFile. The message explicitly says 'first argument' because the second argument may legitimately be non-string (it is converted with toBytes).","triggerScenarios":"Calling append_file with a non-string first argument, e.g. append_file(123, \"data\"), append_file(None, \"data\"), or passing a Path-like/list value produced elsewhere in the script.","commonSituations":"Scripts that build paths via string operations that accidentally yield a list or None (e.g. a failed split or lookup), or users passing a numeric constant instead of a quoted path.","solutions":["Quote or stringify the path: append_file(str(path_value), text)","Check the variable actually holds a string before the call: print(path)","Build paths with string concatenation instead of containers: append_file(\"/tmp/\" + name, text)"],"exampleFix":"// before\nparts = [\"/tmp\", \"out.log\"]\nappend_file(parts, \"line\\n\")\n\n// after\npath = \"/tmp/out.log\"\nappend_file(path, \"line\\n\")","handlingStrategy":"type-guard","validationCode":"if type(path) != \"string\":\n    fail(\"append_file path must be a string: \" + str(path))\nappend_file(path, text)","typeGuard":"def as_path(v):\n    return v if type(v) == \"string\" else str(v)\n\n# usage\nappend_file(as_path(raw_path), text)","tryCatchPattern":null,"preventionTips":["Verify path variables hold strings, not lists of components","Join path components with '+' or a join helper before calling","Guard against None from failed lookups before using a value as a path"],"tags":["starlark","scripting","type-error"],"backgroundTag":"wrong-argument-type","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}