Skip to content

fix: adapt Go commands for macOS sandbox compatibility - #272

Closed
khossdev wants to merge 2 commits into
openai:mainfrom
khossdev:fix/go-sandbox-macos
Closed

khossdev wants to merge 2 commits into
openai:mainfrom
khossdev:fix/go-sandbox-macos

Conversation

@khossdev

@khossdev khossdev commented Apr 17, 2025 •

Copy link
Copy Markdown
Contributor

Fix: Adapt Go commands for macOS sandbox compatibility

Issue

This PR addresses issue #207 "Full auto mode cannot build Go code" where users on macOS encounter permission errors when running Go commands in full-auto mode: go: creating work dir: mkdir /var/folders/zv/b1sjg_v16tn6dpgblz8v4ls40000gn/T/go-build3375040063: operation not permitted

Root Cause

The issue occurs because:

  1. Codex CLI uses Apple Seatbelt (sandbox-exec) on macOS for sandboxing
  2. The sandbox restricts write access to system directories
  3. Go attempts to create temporary build directories in system locations by default
  4. This operation is blocked by the sandbox, causing the build to fail

Solution

This PR implements a platform-specific adaptation for Go commands on macOS:

  1. Detects when a Go command is being executed on macOS
  2. Creates a .tmp directory in the current working directory (which is writable in the sandbox)
  3. Sets the GOTMPDIR environment variable to point to this directory
  4. Go will use this directory for temporary build files instead of system locations

Implementation Details

  • Added a new adaptGoCommandForSandbox function in platform-commands.ts
  • Modified the existing adaptCommandForPlatform function to check for Go commands first
  • Added comprehensive unit tests for the new functionality

Testing

  • Added unit tests that verify the behavior on different platforms
  • Note: I developed this on Windows and haven't been able to test it directly on macOS. The solution is based on Go's documented behavior regarding the GOTMPDIR environment variable.

Additional Notes

This approach:

  • Is non-intrusive and only affects Go commands on macOS
  • Maintains sandbox security by only using directories that are already writable
  • Follows the same pattern as existing platform adaptations
  • Is well-tested and documented

Screenshots

N/A

Checklist

  • Added or updated tests
  • Added comments and documentation
  • Tested on Windows
  • Tested on macOS (not available)
  • Follows project coding standards

@jonchurch

jonchurch commented Apr 17, 2025 •

Copy link
Copy Markdown
Contributor

The os.tmpdir() is explicitly allowed in the sandbox config, and go will attempt to write to it.

This fixes the issue for go specifically by changing the tempdir that is being written to the cwd (which is not symlinked), but it does not fix the underlying root cause of: Why, if it was allowlisted, is the system tmp dir not writeable?

// Merge default writable roots with any user-specified ones.
const writableRoots = [
process.cwd(),
os.tmpdir(),
...additionalWritableRoots,
];
return execForSandbox(cmd, opts, writableRoots, abortSignal);
}

I think I fixed that in #275

@khossdev

Copy link
Copy Markdown
Contributor Author

Thank you for the detailed explanation! I now understand that PR #275 solves the root issue by canonicalizing paths with realpathSync().

This is indeed a more elegant and comprehensive solution than my Go-specific approach. The difference between /var/... and /private/var/... paths on macOS is a subtle but crucial detail I hadn't identified.

I'll abandon my solution in favor of PR #275 which addresses the fundamental problem for all tools, not just Go.

On another note, I'm considering proposing a modernization of the monorepo with pnpm and Turborepo to improve the development experience. Do you think this would be a useful contribution to the project?

@jonchurch

Copy link
Copy Markdown
Contributor

Cut an issue for it @alphajoop is my advice. Ive seen it discussed, so to not duplicate efforts I'd suggest an issue for it.
Im not a maintainer, so no input beyond cut an issue.

@tibo-openai

Copy link
Copy Markdown
Collaborator

I am in favor of this

pnpm and Turborepo to improve the development experience

but indeed post this as an issue and let's discuss there.

@tibo-openai

Copy link
Copy Markdown
Collaborator

Closing this PR as the change is too invasive and we have a different approach by @jonchurch that we'll likely merge shortly.

@github-actions github-actions Bot locked and limited conversation to collaborators Apr 17, 2025
@khossdev
khossdev deleted the fix/go-sandbox-macos branch April 19, 2025 14:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants