gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

WriteFile sometimes fails with error ERROR The "paths[0]" argument must be of type string. Received undefined

Open jacob314 opened this issue 1 year ago • 5 comments

I am not able to reproduce on my machine but the stack trace is hopefully enough to debug.

 ██████╗ ███████╗███╗   ███╗██╗███╗   ██╗██╗
██╔════╝ ██╔════╝████╗ ████║██║████╗  ██║██║
██║  ███╗█████╗  ██╔████╔██║██║██╔██╗ ██║██║
██║   ██║██╔══╝  ██║╚██╔╝██║██║██║╚██╗██║██║
╚██████╔╝███████╗██║ ╚═╝ ██║██║██║ ╚████║██║
 ╚═════╝ ╚══════╝╚═╝     ╚═╝╚═╝╚═╝  ╚═══╝╚═╝


Tips for getting started:
1. /help for more information.
2. Ask coding questions, edit code or run commands.
3. Be specific for the best results.


> @packages/cli add tests to the currently empty LoadingIndicator.test.tsx file. use rerender in the tests.

 ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  ReadManyFiles Will attempt to read and concatenate files using patterns: `packages/cli/**` (within target directory: `/Users/jacobr/git/4/gemini-cli`). Ex… │
 │    ReadManyFiles Result (Target Dir: /Users/jacobr/git/4/gemini-cli)                                                                                           │
 │                                                                                                                                                                │
 │    Successfully read and concatenated content from 95 file(s).                                                                                                 │
 │                                                                                                                                                                │
 │    Processed Files (first 10 shown):                                                                                                                           │
 │     - packages/cli/Dockerfile.sandbox                                                                                                                          │
 │     - packages/cli/README.md                                                                                                                                   │
 │     - packages/cli/index.ts                                                                                                                                    │
 │     - packages/cli/junit.xml                                                                                                                                   │
 │     - packages/cli/package.json                                                                                                                                │
 │     - packages/cli/src/config/config.test.ts                                                                                                                   │
 │     - packages/cli/src/config/config.ts                                                                                                                        │
 │     - packages/cli/src/config/settings.ts                                                                                                                      │
 │     - packages/cli/src/gemini.ts                                                                                                                               │
 │     - packages/cli/src/ui/App.tsx                                                                                                                              │
 │     - ...and 85 more.                                                                                                                                          │
 ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

✦ Okay Captain Fun, I will add tests to packages/cli/src/ui/components/LoadingIndicator.test.tsx.


  First, I'll read the component's source code to ensure the tests are comprehensive.

 ╭────────────────────────────────────────────────╮
 │ ✔  ReadFile packages/.../LoadingIndicator.tsx  │
 ╰────────────────────────────────────────────────╯

✦ Now I'll write the tests.


  ERROR  The "paths[0]" argument must be of type string. Received undefined

 node:path:1181:7

 - Object.resolve (node:path:1181:7)
 - makeRelative (packages/server/dist/src/utils/paths.js:92:35)
 - WriteFileTool.getDescription (packages/server/dist/src/tools/write-file.js:84:26)
 -  (packages/cli/dist/src/ui/hooks/useToolScheduler.js:333:31)
 -     at Array.map (<anonymous>)
 - mapToDisplay (packages/cli/dist/src/ui/hooks/useToolScheduler.js:282:31)
 -  (packages/cli/dist/src/ui/hooks/useGeminiStream.js:46:64)
 - updateMemo (node_modules/react-reconciler/cjs/react-reconciler.development.js:7600:19)
 - Object.useMemo (node_modules/react-reconciler/cjs/react-reconciler.development.js:8240:16)
 - useMemo (node_modules/react/cjs/react.development.js:1650:21)
 - ````

jacob314 avatar May 24 '25 00:05 jacob314

This looks like a problem with the file_path parameter for the write_file tool would be undefined if gemini generates a tool call request for write_file but omits the file_path argument, or provides it as undefined.

  1. The error occurs in WriteFileTool.getDescription(). This method is used to generate a human-readable description of what the tool will do (e.g., "Write to /path/to/your/file.txt").
  2. WriteFileTool.getDescription() is called by a function in useToolScheduler.js (specifically, within an Array.map inside mapToDisplay). This hook is likely responsible for managing and displaying upcoming or in-progress tool calls in the CLI's UI.

So, the sequence is:

  • The LLM decides to use the write_file tool.
  • It should provide parameters like { "file_path": "/path/to/file.txt", "content": "..." }.
  • If the LLM mistakenly sends parameters like { "content": "..." } (missing file_path), then params.file_path would be undefined.
  • The useToolScheduler receives these parameters and tries to create a display representation for the tool call.
  • It calls WriteFileTool.getDescription(params).
  • Inside getDescription, makeRelative(params.file_path, ...) is called. Since params.file_path is undefined, makeRelative passes undefined to path.resolve(), leading to the crash.

A fix would handle the symptom by making makeRelative (and thus WriteFileTool.getDescription) more resilient to an undefined file_path. However, the root cause is likely the LLM generating an incomplete tool request.

The useToolScheduler hook, specifically in the mapToDisplay function, calls t.tool.getDescription(t.request.args).

Here's the relevant part of mapToDisplay for a successful, cancelled, awaiting_approval, executing, validating, or scheduled tool call:

   // ...
         case 'success': // Also applies to 'cancelled', 'awaiting_approval', etc.
           return {
             callId: t.request.callId,
             name: t.tool.displayName,
             description: t.tool.getDescription(t.request.args), // <--- This is the call site
             resultDisplay: t.response.resultDisplay,
             status: mapStatus(t.status),
             confirmationDetails: undefined,
             renderOutputAsMarkdown: t.tool.isOutputMarkdown,
           };
   // ...

The t.request.args are the arguments provided by the LLM for the tool call. If the LLM omits file_path from t.request.args for a write_file call, then t.tool.getDescription() (which is WriteFileTool.getDescription()) will receive args where args.file_path is undefined.

So, the chain of events is:

  1. LLM generates a FunctionCall for write_file but omits file_path in the args.
  2. The schedule function in useToolScheduler receives this ToolCallRequestInfo (as r).
  3. It creates a ToolCall object (as t) where t.request.args is the (incomplete) arguments from the LLM.
  4. Later, mapToDisplay is called with this ToolCall object t.
  5. mapToDisplay calls t.tool.getDescription(t.request.args).
  6. WriteFileTool.getDescription(args) calls makeRelative(args.file_path, ...).
  7. Since args.file_path is undefined, makeRelative passes undefined to path.resolve(), causing the error.

scottdensmore avatar May 31 '25 04:05 scottdensmore

A fix might be to update makeRelative in path.ts with

if (typeof targetPath !== 'string') {
    return '.';
  }

This might fix the crash but not put the file in the right place???

scottdensmore avatar May 31 '25 04:05 scottdensmore

A fix might be to update makeRelative in path.ts with

if (typeof targetPath !== 'string') {
    return '.';
  }

This might fix the crash but not put the file in the right place???

Ultimately this sounds reasonable + making our getDescriptions more resilient is important as well

NTaylorMullen avatar May 31 '25 05:05 NTaylorMullen

@scottdensmore if you'd like to tackle this I don't believe Seth has started it

NTaylorMullen avatar May 31 '25 05:05 NTaylorMullen

Fixed with https://github.com/google-gemini/gemini-cli/pull/708

anj-s avatar Jun 03 '25 22:06 anj-s