bash scripting
file permissions
iOS development
sandboxing
debugging

Sandbox bash72986 deny1 file-write-data /Users/XXX/ios/Pods/resources-to-copy-XXXShareExtension.txt

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error Sandbox: bash(72986) deny(1) file-write-data /Users/XXX/ios/Pods/resources-to-copy-XXXShareExtension.txt occurs during iOS builds, typically when using CocoaPods in a React Native or native iOS project. This is a macOS sandbox violation where a build script tries to write to the Pods directory but is denied by the operating system's sandboxing security mechanism.

Why This Happens

Starting with Xcode 14 and macOS Ventura, Apple tightened the sandboxing rules for build scripts (shell script build phases). By default, Xcode now sandboxes user scripts, preventing them from writing to locations outside the build directory. CocoaPods' script phases (like [CP] Copy Pods Resources) attempt to write files into the Pods directory, which the sandbox blocks.

The key factors:

  1. Xcode build setting: ENABLE_USER_SCRIPT_SANDBOXING defaults to YES in new projects on Xcode 14+
  2. CocoaPods scripts: CocoaPods adds build phase scripts that write intermediate files to the Pods directory
  3. Share extensions: Extensions like Share Extensions have additional resource copying steps that trigger the violation

How to Fix It

In your Xcode project, disable the sandbox for build scripts:

Via Xcode UI:

  1. Select your project in the navigator
  2. Select the target that is failing (e.g., your Share Extension target)
  3. Go to Build Settings
  4. Search for User Script Sandboxing
  5. Set ENABLE_USER_SCRIPT_SANDBOXING to No

Via Podfile (applies to all targets):

ruby
1post_install do |installer|
2  installer.pods_project.targets.each do |target|
3    target.build_configurations.each do |config|
4      config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'
5    end
6  end
7end

Then run:

bash
cd ios && pod install

Option 2: Disable for the Main Project via xcconfig

Add to your .xcconfig file or create one:

 
ENABLE_USER_SCRIPT_SANDBOXING = NO

Option 3: Update CocoaPods

Newer versions of CocoaPods (1.13+) have better support for sandboxed builds:

bash
sudo gem install cocoapods
cd ios && pod install --repo-update

CocoaPods 1.13+ declares proper input/output files for its script phases, which satisfies the sandbox requirements without disabling it entirely.

Option 4: Declare Script Input/Output Files

If you want to keep sandboxing enabled, you can manually declare the files that the script needs to access. In Xcode, select the build phase script and add:

  • Input Files: Files the script reads
  • Output Files: Files the script writes

For the [CP] Copy Pods Resources phase, add:

 
$(PODS_ROOT)/resources-to-copy-${PRODUCT_NAME}.txt

as an output file.

React Native Specific Fix

For React Native projects, the issue often appears after upgrading Xcode. Add this to your Podfile:

ruby
1post_install do |installer|
2  installer.pods_project.targets.each do |target|
3    target.build_configurations.each do |config|
4      config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'
5    end
6  end
7
8  # Also apply to the main project
9  installer.generated_projects.each do |project|
10    project.targets.each do |target|
11      target.build_configurations.each do |config|
12        config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'
13      end
14    end
15  end
16end

Verifying the Fix

After applying the fix:

bash
1# Clean build artifacts
2cd ios
3xcodebuild clean
4rm -rf ~/Library/Developer/Xcode/DerivedData
5
6# Reinstall pods
7pod install
8
9# Build
10xcodebuild build -workspace MyApp.xcworkspace -scheme MyApp

Common Pitfalls

  • Applying only to the Pods project: The sandbox setting must be disabled on both the Pods project and your app targets. If you only disable it for Pods, the app target's script phases may still fail.
  • Not cleaning after changes: After modifying the Podfile or build settings, always clean the build folder and derived data. Stale cached data can make it appear the fix did not work.
  • Security implications: Disabling script sandboxing reduces build-time security. For open-source projects or CI/CD environments, this is generally acceptable. For production environments, prefer updating CocoaPods to a version that supports sandboxing natively.
  • Multiple targets: If your project has multiple targets (app, share extension, widget, etc.), apply the fix to all of them. Each target has its own ENABLE_USER_SCRIPT_SANDBOXING setting.
  • Xcode version differences: This issue does not appear on Xcode 13 or earlier. If team members use different Xcode versions, the build may work for some but fail for others.

Summary

  • This error is caused by macOS sandboxing build scripts in Xcode 14+
  • Quickest fix: set ENABLE_USER_SCRIPT_SANDBOXING = NO in build settings
  • Best applied via post_install in your Podfile to affect all targets
  • Updating CocoaPods to 1.13+ provides native sandbox compatibility
  • Always clean build artifacts and derived data after applying the fix

Course illustration
Course illustration

All Rights Reserved.