Skip to content

feat(core): add the file operations Node fs needs to Environment files - #64378

Draft
rekram1-node wants to merge 2 commits into
devfrom
env-file-ops
Draft

rekram1-node wants to merge 2 commits into
devfrom
env-file-ops

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Issue for this PR

None.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Environment.Files, the file layer every tool goes through, gains the operations Node's fs needs that it can't build from read/write/list today, and stat reports the rest of POSIX stat. This is groundwork for Code Mode's fs (#61186) to cover the whole API. Nothing calls the new operations yet, so behavior doesn't change.

 interface FilesImpl {
   read, write, stat, list, remove, move, mkdir
+  append(path, bytes)                       // add to the end, create when missing
+  writeAt(path, bytes, offset)              // positional write, no truncation (pwrite)
+  truncate(path, length)
+  chmod(path, mode)
+  chown(path, uid, gid, follow)             // follow: false is lchown
+  utimes(path, atimeMs, mtimeMs, follow)    // follow: false is lutimes
+  symlink(target, path)  readlink(path)  realpath(path)  link(existing, path)
+  statfs(path)
 }

 FileInfo { type, size, mtimeMs
+  dev?, ino?, mode?, nlink?, uid?, gid?, rdev?, blksize?, blocks?, atimeMs?, ctimeMs?, birthtimeMs?
 }

The new FileInfo fields are optional, so nothing that builds or reads one has to change.

Each implementation gets them:

  • local (node:fs): the matching fs call. writeAt opens with O_RDWR | O_CREAT so bytes outside the range stay.
  • GNU exec (remote environments): one script each, with the existing exit-code protocol for missing paths and wrong kinds: cat >>, dd oflag=seek_bytes conv=notrunc, truncate -s, chmod, chown [-h], touch -c [-h] -a/-m -d @t, ln [-s], readlink, realpath -e, stat -f. stat asks for every field in one call. Times from GNU stat are whole seconds, as mtimeMs already was.
  • memory (tests): keeps mode, owner, times and inode per node. Its symlink helper, which tests already used, is now the interface method. A hard link is a copy there, since it keeps no link counts.
  • workspace connections pass each operation through.

How did you verify your code works?

  • test/lib/environment-conformance.ts runs the same checks against all three implementations. New checks cover the stat fields, append, positional writes past the end (zero-filled), truncation both ways, chmod and utimes read back through stat, chown, symlink/readlink/realpath/link with their NotFound and WrongKind cases, and statfs. The existing "stat matches the info read returns" check now leaves out the access time, which reading can move.
  • test/environment.test.ts: 32 pass on macOS (the GNU suite is Linux-only), and all 47 pass on Linux with GNU coreutils 9.4, including the GNU exec suite.
  • Workspace, read, file access, session environment, location filesystem, execute and file-write tests pass (107). bun typecheck in core, app, cli, server, sdk and plugin, and bun run check from the root.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…es, links, realpath and statfs to Environment files, and full stat metadata

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound, with one problem in the GNU exec driver. On Linux, test/environment.test.ts passes 47/47 (including the GNU exec suite), the workspace tests pass, and typecheck:tsc passes in core.

Public API: unchanged. FileInfo's new optional fields, FilesystemInfo and the new FilesImpl operations are internal to @opencode/core, and nothing outside core implements FilesImpl.

One concern: in the GNU exec driver, symlink and link call ln without -T. When the destination is an existing directory (or a symlink to one), ln creates the link inside it and succeeds, while the local driver (Node) fails with EEXIST. Details are in the inline comments.

[String(atimeMs / 1_000), String(mtimeMs / 1_000)],
),
symlink: (target, path) =>
run(path, `ln -s -- "$2" "$1"`, [target]).pipe(Effect.flatMap((result) => complete(path, result))),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When path is an existing directory, or a symlink to one, ln -s -- target dir creates dir/<basename of target> and exits 0. The local driver (fs.symlink) fails with EEXIST instead. I checked with coreutils 9.7: in a directory with d/, ln -s -- f d created d/f -> f. ln -sT -- "$2" "$1" (--no-target-directory) makes it fail the same way Node does.

Same problem for hard links: when path is an existing directory, ln -- "$1" "$2" links into it instead of failing like fs.link. ln -T fixes it here too.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The earlier point is addressed: the GNU exec driver now uses ln -sT / ln -T, the memory driver also refuses an existing destination, and the conformance check covers both calls failing onto an existing directory. test/environment.test.ts passes 47/47 on Linux, including the GNU exec suite, and typecheck:tsc passes in core. Public API unchanged. No new concerns.

@rekram1-node
rekram1-node marked this pull request as draft November 17, 2026 06:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant