Skip to content

Commit 3e85693

Browse files
Disable Swift compiler sandboxing in Xcode 15.3+ to fix nested sandboxing (#1206)
This should solve #1202 and #1204 > There were issues with Swift compiler plugins (incl. macros) and nested sandboxes on macOS with Swift 5.9 that have been fixed with 5.10: > swift: [swiftlang/swift#70079](swiftlang/swift#70079) > swift-driver: [swiftlang/swift-driver#1493](swiftlang/swift-driver#1493) > swift-package-manager: [swiftlang/swift-package-manager#7167](swiftlang/swift-package-manager#7167) I am not sure whether this flag is required on Linux too. --------- Signed-off-by: Adin Cebic <[email protected]> Co-authored-by: Brentley Jones <[email protected]>
1 parent 4cc4c95 commit 3e85693

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

swift/internal/compiling.bzl

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ load(
4545
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
4646
"SWIFT_FEATURE_DBG",
4747
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
48+
"SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX",
4849
"SWIFT_FEATURE_DISABLE_SYSTEM_INDEX",
4950
"SWIFT_FEATURE_EMIT_BC",
5051
"SWIFT_FEATURE_EMIT_C_MODULE",
@@ -564,6 +565,19 @@ def compile_action_configs(
564565
features = [SWIFT_FEATURE_TREAT_WARNINGS_AS_ERRORS],
565566
),
566567

568+
# Disable Swift sandbox.
569+
swift_toolchain_config.action_config(
570+
actions = [
571+
swift_action_names.COMPILE,
572+
swift_action_names.DERIVE_FILES,
573+
swift_action_names.DUMP_AST,
574+
],
575+
configurators = [
576+
swift_toolchain_config.add_arg("-disable-sandbox"),
577+
],
578+
features = [SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX],
579+
),
580+
567581
# Set Developer Framework search paths
568582
swift_toolchain_config.action_config(
569583
actions = [

swift/internal/feature_names.bzl

+5
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ SWIFT_FEATURE__FORCE_ALWAYSLINK_TRUE = "swift._force_alwayslink_true"
341341
# feature.
342342
SWIFT_FEATURE__SUPPORTS_MACROS = "swift._supports_macros"
343343

344+
# Disables Swift sandbox which prevents issues with nested sandboxing when Swift code contains system-provided macros.
345+
# If enabled '#Preview' macro provided by SwiftUI fails to build and probably other system-provided macros.
346+
# Enabled by default for Swift 5.10+ on macOS.
347+
SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX = "swift.disable_swift_sandbox"
348+
344349
# Pass -warnings-as-errors to the compiler.
345350
SWIFT_FEATURE_TREAT_WARNINGS_AS_ERRORS = "swift.treat_warnings_as_errors"
346351

swift/internal/xcode_swift_toolchain.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ load(
3535
"SWIFT_FEATURE_COVERAGE",
3636
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
3737
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
38+
"SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX",
3839
"SWIFT_FEATURE_EMIT_SWIFTDOC",
3940
"SWIFT_FEATURE_EMIT_SWIFTSOURCEINFO",
4041
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
@@ -640,6 +641,9 @@ def _xcode_swift_toolchain_impl(ctx):
640641
requested_features.append(SWIFT_FEATURE__SUPPORTS_MACROS)
641642
requested_features.append(SWIFT_FEATURE__SUPPORTS_CONST_VALUE_EXTRACTION)
642643

644+
if _is_xcode_at_least_version(xcode_config, "15.3"):
645+
requested_features.append(SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX)
646+
643647
env = _xcode_env(target_triple = target_triple, xcode_config = xcode_config)
644648
execution_requirements = xcode_config.execution_info()
645649
generated_header_rewriter = ctx.executable.generated_header_rewriter

test/features_tests.bzl

+19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ use_global_index_store_index_while_building_test = make_action_command_line_test
6060
},
6161
)
6262

63+
disable_swift_sandbox_test = make_action_command_line_test_rule(
64+
config_settings = {
65+
"//command_line_option:features": [
66+
"swift.disable_swift_sandbox",
67+
],
68+
},
69+
)
70+
6371
vfsoverlay_test = make_action_command_line_test_rule(
6472
config_settings = {
6573
"//command_line_option:features": [
@@ -179,6 +187,17 @@ def features_test_suite(name):
179187
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
180188
)
181189

190+
disable_swift_sandbox_test(
191+
name = "{}_disable_swift_sandbox_test".format(name),
192+
tags = [name],
193+
expected_argv = [
194+
"-disable-sandbox",
195+
],
196+
mnemonic = "SwiftCompile",
197+
target_compatible_with = ["@platforms//os:macos"],
198+
target_under_test = "@build_bazel_rules_swift//test/fixtures/debug_settings:simple",
199+
)
200+
182201
default_opt_test(
183202
name = "{}_default_opt_test".format(name),
184203
tags = [name],

0 commit comments

Comments
 (0)