Skip to content

Edit tool fails to add trailing newline when creating new files (oldString === "") #21297

@dosmanak

Description

@dosmanak

When using the Edit tool to create new files (with oldString === ""), the tool writes the newString content exactly as provided without ensuring the file ends with a newline character.

Problem Details

In packages/opencode/src/tool/edit.ts (

export const EditTool = Tool.define("edit", {
), the new file creation path (lines ~55-65) handles the case where params.oldString === "":

if (params.oldString === "") {
  const existed = await Filesystem.exists(filePath)
  contentNew = params.newString
  // ...
  await Filesystem.write(filePath, params.newString)  // ← No EOF newline handling

Unlike the existing file modification path which preserves line ending behavior through detectLineEnding() and convertToLineEnding(), the new file creation path performs no EOF newline validation or enforcement.

Expected Behavior

When creating new files via the Edit tool, the content should automatically have a trailing newline added if one is not present, ensuring consistency with the project's coding standards.

Current Behavior

New files are created with exactly the content provided in newString, regardless of whether it ends with a newline.

Proposed Solution

Add EOF newline enforcement in the new file creation path, similar to how existing file edits handle line endings:

if (params.oldString === "") {
  // ... existing code ...
  let contentToWrite = params.newString
  if (!contentToWrite.endsWith('\n')) {
    contentToWrite += '\n'
  }
  await Filesystem.write(filePath, contentToWrite)

OpenCode version

1.1.56

Steps to reproduce

Let the edit tool create a new text file

Screenshot and/or share link

No response

Operating System

GNU/Linux Archlinux

Terminal

kitty

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcoreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions