Skip to content

fix: prevent EROFS crash when launched outside a git repo#44

Merged
BYK merged 1 commit intomainfrom
fix/erofs-root-path-crash
Mar 20, 2026
Merged

fix: prevent EROFS crash when launched outside a git repo#44
BYK merged 1 commit intomainfrom
fix/erofs-root-path-crash

Conversation

@BYK
Copy link
Owner

@BYK BYK commented Mar 20, 2026

Fixes #25

Problem

When OpenCode is launched from a directory that is not a git repository (e.g. the user's home directory), the lore plugin crashes with an EROFS: read-only file system error trying to write to //AGENTS.md.

The root cause: projectPath resolves to / when there is no git worktree and ctx.directory is the filesystem root. The template literal ${projectPath}/${cfg.agentsFile.path} then produces //AGENTS.md, and mkdirSync(dirname("//AGENTS.md")) attempts to write to the system root — which is read-only on macOS (EROFS).

Fix

  1. Add isValidProjectPath() guard — returns false for root (/), empty, or falsy paths. Guards both the import (startup) and export (session.idle) code paths to prevent any filesystem operations on invalid project paths.

  2. Use path.join() instead of template literal concatenation — replaces \${projectPath}/${path}`withjoin(projectPath, path)` for proper path construction. This is a defense-in-depth measure that normalizes paths correctly.

Changes

  • src/index.ts: Added isValidProjectPath() export, guarded both agents-file blocks, switched to join()
  • test/index.test.ts: Added unit tests for isValidProjectPath and integration test verifying plugin initializes without crashing when projectPath is /

When OpenCode launches outside a git repo, projectPath resolves to '/'
and AGENTS.md file operations attempt to write to the filesystem root
(e.g. mkdirSync('//') on macOS → EROFS).

Changes:
- Add isValidProjectPath() guard that rejects '/', '', and falsy paths
- Replace template literal path construction with path.join()
- Guard both AGENTS.md import and export blocks with the path check
- Add unit tests for isValidProjectPath and plugin init with '/' path

Fixes #25
@BYK BYK enabled auto-merge (squash) March 20, 2026 14:53
@BYK BYK merged commit cb15369 into main Mar 20, 2026
2 checks passed
@BYK BYK deleted the fix/erofs-root-path-crash branch March 20, 2026 14:53
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.

EROFS crash when OpenCode launched outside a git repo (//AGENTS.md path)

1 participant