go-delve/delve · error

first argument of write_file was not a string

Error message

first argument of write_file was not a string

What it means

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.

Source

Thrown at pkg/terminal/starbind/starlark.go:145

			return nil, decorateError(thread, errors.New("first argument of append_file was not a string"))
		}
		f, err := os.OpenFile(string(path), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o640)
		if err != nil {
			return nil, decorateError(thread, err)
		}
		defer f.Close()
		_, err = f.Write(toBytes(args[1]))
		return starlark.None, decorateError(thread, err)
	})
	builtindoc(appendFileBuiltinName, "(Path, Text)", "append text to the specified file.")

	env.env[writeFileBuiltinName] = starlark.NewBuiltin(writeFileBuiltinName, func(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
		if len(args) != 2 {
			return nil, decorateError(thread, errors.New("wrong number of arguments"))
		}
		path, ok := args[0].(starlark.String)
		if !ok {
			return nil, decorateError(thread, errors.New("first argument of write_file was not a string"))
		}
		err := os.WriteFile(string(path), toBytes(args[1]), 0o640)
		return starlark.None, decorateError(thread, err)
	})
	builtindoc(writeFileBuiltinName, "(Path, Text)", "writes text to the specified file.")

	env.env[curScopeBuiltinName] = starlark.NewBuiltin(curScopeBuiltinName, func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
		return env.interfaceToStarlarkValue(env.ctx.Scope()), nil
	})
	builtindoc(curScopeBuiltinName, "()", "returns the current scope.")

	env.env[defaultLoadConfigBuiltinName] = starlark.NewBuiltin(defaultLoadConfigBuiltinName, func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
		return env.interfaceToStarlarkValue(env.ctx.LoadConfig()), nil
	})
	builtindoc(defaultLoadConfigBuiltinName, "()", "returns the default load configuration.")

	env.env[targetObjectName] = starlarkTargetObject{env: env}

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Ensure the first argument is a string: write_file("/tmp/output.txt", data)
  2. Wrap dynamic values with str(): write_file(str(path), data)
  3. Verify with print(path) that the variable holds the expected string before calling

Example fix

// before
logfile = find_logfile()  # returns None on failure
write_file(logfile, "x")

// after
logfile = find_logfile()
if logfile == None:
    logfile = "/tmp/default.log"
write_file(logfile, "x")
Defensive patterns

Strategy: type-guard

Validate before calling

if path == None:
    path = "/tmp/default.log"
if type(path) != "string":
    path = str(path)
write_file(path, data)

Type guard

def ensure_path(v, default):
    if v == None or type(v) != "string":
        return default
    return v

# usage
write_file(ensure_path(computed_path, "/tmp/out.txt"), data)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/8c3e30d5eb572816. Report an issue: GitHub.