oficina / equipo Public

mac: put xcodebuild's result bundles and SwiftPM's config in scratch

The Studio's P2 on 0a6405b failed with "xcodebuild Error saving log
... Code 513". Its sandbox log (21:21 and 21:34) showed what was
denied: default result bundles in DARWIN_USER_TEMP_DIR
(T/ResultBundle_<date>.xcresult), NSItemReplacementDirectory staging
(T/TemporaryItems/NSIRD_xcodebuild_*), swift-driver's
T/TemporaryDirectory.*, Xcode's plugin cache in DARWIN_USER_CACHE_DIR
(C/com.apple.DeveloperTools/<ver>/Xcode/PlugInCache-xcodebuild.
xcplugincache) and ~/.swiftpm/security. This moves the two that a flag
or a documented variable can move. No path was added to the profile;
ChecksSettings is unchanged.

1. -resultBundlePath "$TMPDIR/Results/<action>.xcresult" on every
   xcodebuild action (mac.XcodeResult): the checks gate ("checks"), P2's
   script-build, package-resolve, test-build and test-run, and P6's
   "wait". Each action gets its own name because the path must not
   exist yet. `-list` has none because it is not an action.
2. XDG_CONFIG_HOME=<attempt scratch>/config in every srt gate's env
   (Gate.Run, beside CLAUDE_CODE_TMPDIR). SwiftPM's dotSwiftPM is
   $XDG_CONFIG_HOME/swiftpm when that is set, else ~/.swiftpm (Basics,
   FileSystem+Extensions.swift, since PR #7386, Swift 6.0). Its
   getOrCreateSwiftPMSecurityDirectory removes ~/.swiftpm/security
   whenever isDirectory() is true. isDirectory() follows symlinks, so
   the link it made last time is removed and remade on every resolve.
   That is the file-write-unlink the Studio logged. On a fresh user it
   would be a mkdir of ~/.swiftpm. The real security, configuration and
   cache folders stay in ~/Library/org.swift.swiftpm and ~/Library/
   Caches/org.swift.swiftpm, which are already writable. Unsandboxed
   gates are unchanged. Repository gates under srt get it too, so git
   no longer reads ~/.config/git there (it still reads ~/.gitconfig).

Not moved; options in the report, not here: T/TemporaryItems (NSIRD),
T/TemporaryDirectory.* and Swift Build's T/swbuild.tmp.* (Foundation's
temporaryDirectory ignores TMPDIR under Seatbelt on Xcode 27, per an
open Apple forums report, thread 853111), and the C/ plugin cache. The
xcactivitylog and result bundle saves are atomic replaces staged
through NSIRD, so "Error saving log" may stay until T is decided.

Tests: TestXcodeWritesStayInScratch now also checks that every action
has its own -resultBundlePath under "$TMPDIR/". TestXcodeGate pins the
argv. TestP2Script counts four result bundles.
TestGateKeepsTMPDIRUnderSRT pins XDG_CONFIG_HOME=<scratch>/config, and
TestGateEnvironmentFromScratch still rejects it outside srt. The fake
xcodebuild fails like the Studio when an action has no bundle in
TMPDIR (exit 65, Error saving log) and when a resolve's
XDG_CONFIG_HOME is not in TMPDIR (exit 74, .swiftpm/security).
Broken and seen failing: XcodeGate without its bundle (TestXcodeGate,
TestXcodeWritesStayInScratch); P2's package steps without bundles
(every P2 run test, TestP2Script); Gate.Run without XDG_CONFIG_HOME
(TestGateKeepsTMPDIRUnderSRT and every P2 run test, at package resolve
exit 74).

Self-review:
- Fails closed. An xcodebuild that refuses -resultBundlePath with
  -resolvePackageDependencies (not proven on a Mac) fails that step
  visibly, and P2 is still denied only when every step reports 0.
- Assumption: Xcode 27.2's in-process SwiftPM has the XDG_CONFIG_HOME
  lookup (Swift 6.0 or later). The "~/.swiftpm/security" path in the
  denial shows Xcode uses dotSwiftPM.
- XDG_CONFIG_HOME also changes where other XDG-aware tools in an srt
  gate look for config: the empty attempt scratch. That is more
  hermetic and loses nothing secret.
- Result bundles now stay in scratch and go with the attempt.
  Previously xcodebuild moved them into DerivedData, which is also in
  scratch.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 3b880a386dbb55b4154056e7b20d711f6ada235b parent 0a6405b
8 files changed, +67 −18
  • internal/runner/mac/caps_test.go +1 −1
  • internal/runner/mac/gate.go +9 −2
  • internal/runner/mac/gate_test.go +7 −2
  • internal/runner/mac/p2why_test.go +1 −1
  • internal/runner/mac/probe.go +10 −7
  • internal/runner/mac/probe_test.go +7 −1
  • internal/runner/mac/xcode.go +18 −3
  • internal/runner/mac/xcode_test.go +14 −1

CI passed — run details

diff --git a/internal/runner/mac/caps_test.go b/internal/runner/mac/caps_test.go
index 0095a317..5b80c8f1 100644
--- a/internal/runner/mac/caps_test.go
+++ b/internal/runner/mac/caps_test.go
@@ -96,7 +96,7 @@ func TestXcodeGate(t *testing.T) {
sims := ParseSimulators(mustRead(t, "testdata/simctl-devices.json"))
tree := woodstarTree(t, "Woodstar.xcodeproj/xcshareddata/xcschemes/Woodstar.xcscheme", "Woodstar/Swipe.swift")
argv, ok, reason := XcodeGate(tree, sims)
- want := `exec /usr/bin/xcodebuild test -project 'Woodstar.xcodeproj' -scheme 'Woodstar' -destination 'platform=iOS Simulator,id=8F0A1E2B-3D4F-4A5B-9C6D-7E8F90A1B2C3' -derivedDataPath "$TMPDIR/DerivedData" -clonedSourcePackagesDirPath "$TMPDIR/SourcePackages" -packageCachePath "$TMPDIR/PackageCache" CODE_SIGNING_ALLOWED=NO`
+ want := `exec /usr/bin/xcodebuild test -project 'Woodstar.xcodeproj' -scheme 'Woodstar' -destination 'platform=iOS Simulator,id=8F0A1E2B-3D4F-4A5B-9C6D-7E8F90A1B2C3' -derivedDataPath "$TMPDIR/DerivedData" -clonedSourcePackagesDirPath "$TMPDIR/SourcePackages" -packageCachePath "$TMPDIR/PackageCache" -resultBundlePath "$TMPDIR/Results/checks.xcresult" CODE_SIGNING_ALLOWED=NO`
if !ok || reason != "" || !slices.Equal(argv, []string{"/bin/bash", "-c", want}) {
t.Fatalf("%v %v %q", argv, ok, reason)
}
diff --git a/internal/runner/mac/gate.go b/internal/runner/mac/gate.go
index 7545f8c8..f72645ad 100644
--- a/internal/runner/mac/gate.go
+++ b/internal/runner/mac/gate.go
@@ -37,8 +37,8 @@ type Gate struct {
// Settings writes the attempt's srt settings for a tree and scratch.
Settings func(tree, attempt string) ([]byte, error)
// Env is the gate's whole environment but TMPDIR, EQUIPO_ATTEMPT and
- // EQUIPO_GATE_STATUS (and under srt CLAUDE_CODE_TMPDIR), which the
- // run adds.
+ // EQUIPO_GATE_STATUS (and under srt CLAUDE_CODE_TMPDIR and
+ // XDG_CONFIG_HOME), which the run adds.
Env []string
// Caffeinate keeps the Mac awake while a gate runs
// (/usr/bin/caffeinate); "" does not.
@@ -117,6 +117,13 @@ func (g *Gate) Run(ctx context.Context, attempt, dir string, argv []string, limi
// keep it this attempt's scratch, which the profile already lets
// the gate write and the run removes.
cmd.Env = append(cmd.Env, "CLAUDE_CODE_TMPDIR="+scratch)
+ // SwiftPM keeps ~/.swiftpm (links to its security, configuration
+ // and cache folders, and a lock it takes) at $XDG_CONFIG_HOME/
+ // swiftpm when that is set (dotSwiftPM in SwiftPM's Basics), and
+ // removes and remakes the security link on every resolve: under
+ // srt that is a write to the real home, which the profile
+ // refuses. In the attempt's scratch it is the attempt's alone.
+ cmd.Env = append(cmd.Env, "XDG_CONFIG_HOME="+filepath.Join(scratch, "config"))
}
if g.ScratchHome {
cmd.Env = append(cmd.Env, "HOME="+scratch)
diff --git a/internal/runner/mac/gate_test.go b/internal/runner/mac/gate_test.go
index 51075be4..f528c1d4 100644
--- a/internal/runner/mac/gate_test.go
+++ b/internal/runner/mac/gate_test.go
@@ -127,10 +127,15 @@ func TestGateEnvironmentFromScratch(t *testing.T) {
// other user's /tmp/claude decides whether it can write.
func TestGateKeepsTMPDIRUnderSRT(t *testing.T) {
g, dir := gateFixture(t, true)
- res, _ := run(g, dir, "checks-c-2", time.Minute, `echo "$TMPDIR"`)
- if res.Code != 0 || !strings.HasPrefix(res.Output, filepath.Dir(dir)+"/attempt-") {
+ res, _ := run(g, dir, "checks-c-2", time.Minute, `echo "$TMPDIR"; echo "$XDG_CONFIG_HOME"`)
+ lines := strings.Split(res.Output, "\n")
+ if res.Code != 0 || len(lines) != 2 || !strings.HasPrefix(lines[0], filepath.Dir(dir)+"/attempt-") {
t.Fatalf("TMPDIR under srt: %+v", res)
}
+ // SwiftPM's ~/.swiftpm goes to the attempt's scratch too.
+ if lines[1] != lines[0]+"/config" {
+ t.Fatalf("XDG_CONFIG_HOME under srt: %+v", res)
+ }
}
// Cancel and timeout: SIGTERM to the group, SIGKILL after the grace, a
diff --git a/internal/runner/mac/p2why_test.go b/internal/runner/mac/p2why_test.go
index a4859361..6ead65e4 100644
--- a/internal/runner/mac/p2why_test.go
+++ b/internal/runner/mac/p2why_test.go
@@ -202,7 +202,7 @@ func TestP2Script(t *testing.T) {
}
last = i
}
- if strings.Count(s, "-skipPackagePluginValidation") != 3 || strings.Count(s, XcodeWrites) != 5 || strings.Contains(s, "http") {
+ if strings.Count(s, "-skipPackagePluginValidation") != 3 || strings.Count(s, XcodeWrites) != 5 || strings.Count(s, `-resultBundlePath "$TMPDIR/Results/`) != 4 || strings.Contains(s, "http") {
t.Fatalf("script %s", s)
}
}
diff --git a/internal/runner/mac/probe.go b/internal/runner/mac/probe.go
index 0bf8ebd8..78b4ab37 100644
--- a/internal/runner/mac/probe.go
+++ b/internal/runner/mac/probe.go
@@ -387,18 +387,21 @@ func p2Detail(groups map[string][]string, total int, srtRan, ended bool, why str
// writes only into the attempt's scratch (XcodeWrites), as checks do;
// the script phase's target is built through its shared scheme because
// xcodebuild takes -derivedDataPath only for a scheme, and SYMROOT and
-// OBJROOT stay in scratch too should a build fall back to them.
+// OBJROOT stay in scratch too should a build fall back to them. Each
+// action has its own result bundle there (XcodeResult).
func p2Script(xb, xcrun, udid string) string {
dest := quote("platform=iOS Simulator,id=" + udid)
- pkg := ` -scheme "$s" ` + XcodeWrites + ` -skipPackagePluginValidation`
+ pkg := func(step string) string {
+ return ` -scheme "$s" ` + XcodeWrites + ` ` + XcodeResult(step) + ` -skipPackagePluginValidation`
+ }
return `phase() { p=$1; shift; "$@"; c=$?; echo "EQUIPO-PHASE $p $c"; return $c; }
-(cd project && phase script-build ` + xb + ` -project Hostile.xcodeproj -scheme HostileScript -configuration Debug build ` + XcodeWrites + ` SYMROOT="$TMPDIR/sym" OBJROOT="$TMPDIR/obj"); a=$?
+(cd project && phase script-build ` + xb + ` -project Hostile.xcodeproj -scheme HostileScript -configuration Debug build ` + XcodeWrites + ` ` + XcodeResult("script-build") + ` SYMROOT="$TMPDIR/sym" OBJROOT="$TMPDIR/obj"); a=$?
cd package || exit 1
s=Hostile-Package; ` + xb + ` -list ` + XcodeWrites + ` 2>/dev/null | grep -q Hostile-Package || s=Hostile
-phase package-resolve ` + xb + ` -resolvePackageDependencies` + pkg + ` &&
-phase test-build ` + xb + ` build-for-testing -destination ` + dest + pkg + ` &&
+phase package-resolve ` + xb + ` -resolvePackageDependencies` + pkg("package-resolve") + ` &&
+phase test-build ` + xb + ` build-for-testing -destination ` + dest + pkg("test-build") + ` &&
phase simulator-boot ` + xcrun + ` simctl bootstatus ` + quote(udid) + ` -b &&
-phase test-run ` + xb + ` test-without-building -destination ` + dest + ` -only-testing:HostileTests/HostileTests/testHostile` + pkg + `
+phase test-run ` + xb + ` test-without-building -destination ` + dest + ` -only-testing:HostileTests/HostileTests/testHostile` + pkg("test-run") + `
b=$?
[ "$a" = 0 ] && [ "$b" = 0 ]`
}
@@ -675,7 +678,7 @@ func probeP6(ctx context.Context, h ProbeHost, caps wire.Capabilities, srtRan bo
// attempt's scratch as P2's and the checks' are.
func p6Script(xb, destination string) string {
return `cd package && s=Hostile-Package; ` + xb + ` -list ` + XcodeWrites + ` 2>/dev/null | grep -q Hostile-Package || s=Hostile
-exec ` + xb + ` test -scheme "$s" -destination ` + quote(destination) + ` -only-testing:HostileTests/HostileTests/testWait ` + XcodeWrites + ` -skipPackagePluginValidation`
+exec ` + xb + ` test -scheme "$s" -destination ` + quote(destination) + ` -only-testing:HostileTests/HostileTests/testWait ` + XcodeWrites + ` ` + XcodeResult("wait") + ` -skipPackagePluginValidation`
}
// processNames are this user's processes by name (basename), counted.
diff --git a/internal/runner/mac/probe_test.go b/internal/runner/mac/probe_test.go
index 4084b907..e7b56cd9 100644
--- a/internal/runner/mac/probe_test.go
+++ b/internal/runner/mac/probe_test.go
@@ -24,12 +24,18 @@ import (
// FAKE_*_FAIL fails a step the way it fails on a Mac. A call that would
// write Xcode's derived data, package checkouts or package caches
// anywhere but the gate's TMPDIR fails as the Studio's P2 did under srt
-// (exit 65, the PIF cache's "workspace" folder not writable).
+// (exit 65, the PIF cache's "workspace" folder not writable); an action
+// without its result bundle there fails as the default bundle in
+// DARWIN_USER_TEMP_DIR did, and a resolve whose SwiftPM config folder
+// ($XDG_CONFIG_HOME, else ~/.swiftpm) is not in TMPDIR as ~/.swiftpm
+// did (exit 74).
const fakeXcodebuild = `#!/bin/bash
noise() { [ -n "$FAKE_NOISE" ] && for i in $(seq 1 300); do echo "CompileSwift normal arm64 step $i"; done; true; }
for f in -derivedDataPath -clonedSourcePackagesDirPath -packageCachePath; do
case " $* " in *" $f $TMPDIR/"*) ;; *) echo "error: Could not compute dependency graph: Error Domain=NSCocoaErrorDomain Code=513 (no permission to save in the folder workspace; $f not in TMPDIR)"; echo "** BUILD FAILED **"; exit 65 ;; esac
done
+case " $* " in *" -list "*|*" -resultBundlePath $TMPDIR/Results/"*) ;; *) echo "xcodebuild[1] Error saving log: Error Domain=NSCocoaErrorDomain Code=513 (ResultBundle in DARWIN_USER_TEMP_DIR: Operation not permitted)"; echo "** BUILD FAILED **"; exit 65 ;; esac
+case "$*" in *-resolvePackageDependencies*) case "$XDG_CONFIG_HOME" in "$TMPDIR"/*) ;; *) echo "xcodebuild: error: Could not resolve package dependencies:"; echo " .swiftpm/security: Operation not permitted"; exit 74 ;; esac ;; esac
case "$*" in
*-list*) echo " Schemes:"; echo " Hostile-Package"; exit 0 ;;
*HostileScript*)
diff --git a/internal/runner/mac/xcode.go b/internal/runner/mac/xcode.go
index 65ddb5de..dbba4665 100644
--- a/internal/runner/mac/xcode.go
+++ b/internal/runner/mac/xcode.go
@@ -77,10 +77,21 @@ func XcodeGate(tree string, sims []wire.Simulator) (argv []string, ok bool, reas
return nil, true, "checks_unavailable"
}
cmd := "exec /usr/bin/xcodebuild test " + flag + " " + quote(container) + " -scheme " + quote(scheme) +
- " -destination " + quote("platform=iOS Simulator,id="+sim.UDID) + " " + XcodeWrites + " CODE_SIGNING_ALLOWED=NO"
+ " -destination " + quote("platform=iOS Simulator,id="+sim.UDID) + " " + XcodeWrites + " " + XcodeResult("checks") + " CODE_SIGNING_ALLOWED=NO"
return []string{"/bin/bash", "-c", cmd}, true, ""
}
+// XcodeResult names an xcodebuild action's result bundle in the
+// attempt's scratch. Without it xcodebuild stages one as
+// ResultBundle_<date>.xcresult in the user's DARWIN_USER_TEMP_DIR
+// (/private/var/folders/<..>/T, from confstr, not $TMPDIR), which the
+// checks profile does not let it write: the Studio's sandbox log
+// showed those denials. The path must not exist yet, so each action in
+// one attempt has its own name; xcodebuild makes the folders.
+func XcodeResult(name string) string {
+ return `-resultBundlePath "$TMPDIR/Results/` + name + `.xcresult"`
+}
+
// XcodeWrites sends what xcodebuild writes for itself into the
// attempt's scratch ($TMPDIR: the gate's, which the checks profile lets
// it write and the run removes), not the runner user's ~/Library. Every
@@ -88,14 +99,18 @@ func XcodeGate(tree string, sims []wire.Simulator) (argv []string, ok bool, reas
// proves the way checks build.
// - -derivedDataPath: build products and intermediates, XCBuildData
// with its PIF cache (the "workspace" folder P2's script build could
-// not write under srt), the module cache, logs and a test's result
-// bundle.
+// not write under srt), the module cache and the activity logs.
+// The result bundle is XcodeResult's.
// - -clonedSourcePackagesDirPath: the packages' checkouts and
// workspace state (inside DerivedData by default; named so no
// default can move them).
// - -packageCachePath: SwiftPM's caches (~/Library/Caches/
// org.swift.swiftpm by default), in xcodebuild since Xcode 13.
//
+// SwiftPM's ~/.swiftpm (its security, configuration and cache links,
+// and the lock it takes there) moves with XDG_CONFIG_HOME, which a
+// sandboxed gate sets to the attempt's scratch (Gate.Run).
+//
// xcodebuild applies -derivedDataPath to an action on a scheme, so a
// target is built through its scheme.
const XcodeWrites = `-derivedDataPath "$TMPDIR/DerivedData" -clonedSourcePackagesDirPath "$TMPDIR/SourcePackages" -packageCachePath "$TMPDIR/PackageCache"`
diff --git a/internal/runner/mac/xcode_test.go b/internal/runner/mac/xcode_test.go
index 261349f3..4c9071c5 100644
--- a/internal/runner/mac/xcode_test.go
+++ b/internal/runner/mac/xcode_test.go
@@ -29,12 +29,25 @@ func TestXcodeWritesStayInScratch(t *testing.T) {
}
for name, s := range scripts {
calls := 0
+ bundles := map[string]bool{}
for _, seg := range regexp.MustCompile(`&&|\|\||[;|\n()]`).Split(s, -1) {
if !strings.Contains(seg, "xcodebuild") {
continue
}
calls++
- for _, flag := range []string{"-derivedDataPath", "-clonedSourcePackagesDirPath", "-packageCachePath"} {
+ flags := []string{"-derivedDataPath", "-clonedSourcePackagesDirPath", "-packageCachePath"}
+ if !strings.Contains(seg, " -list ") {
+ // An action's result bundle: its own, in scratch, never
+ // the default one in DARWIN_USER_TEMP_DIR.
+ flags = append(flags, "-resultBundlePath")
+ b := regexp.MustCompile(`-resultBundlePath ("[^"]*")`).FindStringSubmatch(seg)
+ if b == nil || bundles[b[1]] {
+ t.Errorf("%s: no result bundle of its own in %q", name, seg)
+ } else {
+ bundles[b[1]] = true
+ }
+ }
+ for _, flag := range flags {
if !strings.Contains(seg, flag+` "$TMPDIR/`) {
t.Errorf("%s: %s not in the attempt's scratch in %q", name, flag, seg)
}