{"record":{"id":"8c3e30d5eb572816","repo":"go-delve/delve","slug":"first-argument-of-write-file-was-not-a-string","errorCode":null,"errorMessage":"first argument of write_file was not a string","messagePattern":"first argument of write_file was not a string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/terminal/starbind/starlark.go","lineNumber":145,"sourceCode":"\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\"))\n\t\t}\n\t\terr := os.WriteFile(string(path), toBytes(args[1]), 0o640)\n\t\treturn starlark.None, decorateError(thread, err)\n\t})\n\tbuiltindoc(writeFileBuiltinName, \"(Path, Text)\", \"writes text to the specified file.\")\n\n\tenv.env[curScopeBuiltinName] = starlark.NewBuiltin(curScopeBuiltinName, func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {\n\t\treturn env.interfaceToStarlarkValue(env.ctx.Scope()), nil\n\t})\n\tbuiltindoc(curScopeBuiltinName, \"()\", \"returns the current scope.\")\n\n\tenv.env[defaultLoadConfigBuiltinName] = starlark.NewBuiltin(defaultLoadConfigBuiltinName, func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {\n\t\treturn env.interfaceToStarlarkValue(env.ctx.LoadConfig()), nil\n\t})\n\tbuiltindoc(defaultLoadConfigBuiltinName, \"()\", \"returns the default load configuration.\")\n\n\tenv.env[targetObjectName] = starlarkTargetObject{env: env}\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/terminal/starbind/starlark.go#L127-L163","documentation":"write_file's first argument must be a starlark.String path; any other Starlark type for args[0] produces this error. The check happens before os.WriteFile so a malformed call never touches the filesystem. The second argument may be any value because toBytes converts it.","triggerScenarios":"Calling write_file with a non-string first argument, e.g. write_file(None, \"data\"), write_file(42, \"data\"), or passing a starlark.List/tuple as the destination.","commonSituations":"Scripts where the path variable was overwritten by a computation returning None (e.g. a failed builtin call), or where a list of path components was passed instead of a joined string.","solutions":["Ensure the first argument is a string: write_file(\"/tmp/output.txt\", data)","Wrap dynamic values with str(): write_file(str(path), data)","Verify with print(path) that the variable holds the expected string before calling"],"exampleFix":"// before\nlogfile = find_logfile()  # returns None on failure\nwrite_file(logfile, \"x\")\n\n// after\nlogfile = find_logfile()\nif logfile == None:\n    logfile = \"/tmp/default.log\"\nwrite_file(logfile, \"x\")","handlingStrategy":"type-guard","validationCode":"if path == None:\n    path = \"/tmp/default.log\"\nif type(path) != \"string\":\n    path = str(path)\nwrite_file(path, data)","typeGuard":"def ensure_path(v, default):\n    if v == None or type(v) != \"string\":\n        return default\n    return v\n\n# usage\nwrite_file(ensure_path(computed_path, \"/tmp/out.txt\"), data)","tryCatchPattern":null,"preventionTips":["Check upstream calls for None returns before reusing their results as paths","Default fallback paths make scripts resilient to failed computations","Validate types early in long scripts where values flow through many variables"],"tags":["starlark","scripting","type-error"],"backgroundTag":"wrong-argument-type","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}