Skip to content

Commit def32f3

Browse files
authored
Ensure undeclared outputs are deleted when not tracked (#1533)
In rules_swift < 3.x the .swiftsourceinfo files are unconditionally written to the module path. In rules_swift >= 3.x these same files are no longer tracked by Bazel unless explicitly requested. When using non-sandboxed mode, previous builds will contain these files and cause build failures when Swift tries to use them, in order to work around this compatibility issue, we check the module path for the presence of .swiftsourceinfo files and if they are present but not requested, we remove them. Testing: - `bazel clean --expunge` - `git checkout 2.8.2` - `bazel build //examples/... --spawn_strategy=local` (pass) - `git checkout master` - `bazel build //examples/... --spawn_strategy=local` (failure) - `git checkout <this-branch>` - `bazel build //examples/... --spawn_strategy=local` (pass)
1 parent f172068 commit def32f3

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

swift/toolchains/config/compile_config.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ load(
4141
"SWIFT_FEATURE_COVERAGE_PREFIX_MAP",
4242
"SWIFT_FEATURE_DBG",
4343
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
44+
"SWIFT_FEATURE_DECLARE_SWIFTSOURCEINFO",
4445
"SWIFT_FEATURE_DISABLE_AVAILABILITY_CHECKING",
4546
"SWIFT_FEATURE_DISABLE_CLANG_SPI",
4647
"SWIFT_FEATURE_DISABLE_SWIFT_SANDBOX",
@@ -562,6 +563,15 @@ def compile_action_configs(
562563
[SWIFT_FEATURE_COVERAGE_PREFIX_MAP, SWIFT_FEATURE_COVERAGE],
563564
],
564565
),
566+
567+
# Ensure that .swiftsourceinfo files are tracked and not deleted by the worker
568+
ActionConfigInfo(
569+
actions = [
570+
SWIFT_ACTION_COMPILE,
571+
],
572+
configurators = [add_arg("-Xwrapped-swift=-emit-swiftsourceinfo")],
573+
features = [SWIFT_FEATURE_DECLARE_SWIFTSOURCEINFO],
574+
),
565575
]
566576

567577
#### Coverage and sanitizer instrumentation flags

tools/worker/swift_runner.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ SwiftRunner::SwiftRunner(const std::vector<std::string> &args,
116116
}
117117

118118
int SwiftRunner::Run(std::ostream *stderr_stream, bool stdout_to_stderr) {
119+
// In rules_swift < 3.x the .swiftsourceinfo files are unconditionally written to the module path.
120+
// In rules_swift >= 3.x these same files are no longer tracked by Bazel unless explicitly requested.
121+
// When using non-sandboxed mode, previous builds will contain these files and cause build failures
122+
// when Swift tries to use them, in order to work around this compatibility issue, we check the module path for
123+
// the presence of .swiftsourceinfo files and if they are present but not requested, we remove them.
124+
if (swift_source_info_path_ != "" && !emit_swift_source_info_) {
125+
std::filesystem::remove(swift_source_info_path_);
126+
}
127+
119128
int exit_code = RunSubProcess(
120129
args_, &job_env_, stderr_stream, stdout_to_stderr);
121130

@@ -392,6 +401,8 @@ std::vector<std::string> SwiftRunner::ParseArguments(Iterator itr) {
392401
target_label_ = arg;
393402
} else if (arg == "-file-prefix-pwd-is-dot") {
394403
file_prefix_pwd_is_dot_ = true;
404+
} else if (arg == "-emit-swiftsourceinfo") {
405+
emit_swift_source_info_ = true;
395406
}
396407
} else {
397408
if (arg == "-output-file-map") {
@@ -406,6 +417,12 @@ std::vector<std::string> SwiftRunner::ParseArguments(Iterator itr) {
406417
out_args.push_back(arg);
407418
} else if (arg == "-dump-ast") {
408419
is_dump_ast_ = true;
420+
} else if (arg == "-emit-module-path") {
421+
++it;
422+
arg = *it;
423+
std::filesystem::path module_path(arg);
424+
swift_source_info_path_ = module_path.replace_extension(".swiftsourceinfo").string();
425+
out_args.push_back(arg);
409426
}
410427
}
411428
}

tools/worker/swift_runner.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ class SwiftRunner {
176176
// `-index-store-path`. After running `swiftc` `index-import` copies relevant
177177
// index outputs into the `index_store_path` to integrate outputs with Bazel.
178178
std::string global_index_store_import_path_;
179+
180+
// The path where the module files will be written
181+
std::string swift_source_info_path_;
182+
183+
// Whether `.swiftsourceinfo` files are being generated.
184+
bool emit_swift_source_info_;
179185
};
180186

181187
#endif // BUILD_BAZEL_RULES_SWIFT_TOOLS_WORKER_SWIFT_RUNNER_H_

0 commit comments

Comments
 (0)