Skip to content

Support tree artifacts in Swift srcs - #3388

Open
erneestoc wants to merge 1 commit into
MobileNativeFoundation:mainfrom
erneestoc:ec/tree-artifact-srcs
Open

erneestoc wants to merge 1 commit into
MobileNativeFoundation:mainfrom
erneestoc:ec/tree-artifact-srcs

Conversation

@erneestoc

Copy link
Copy Markdown
Contributor

A swift_library can have a directory (tree artifact) in srcs, e.g. a directory of generated sources. rules_swift 4.2.0 compiles its files individually, but generating a project with one fails:

Generating Features/Episodes/EpisodesTestSupport.rules_xcodeproj.build_settings failed:
Failed to expand directory <generated file Features/Episodes/EpisodesTestSupport_apollo_test_mocks>.
Either add the directory as an input of the action or set 'expand_directories = False' ...

Three actions expand the directory without having it as an input:

  • WriteTargetBuildSettings reuses the SwiftCompile action's Args, which expand the directory. The target's directory srcs are now passed along with the Args (swift_directories) and are inputs of the action. They're taken from the target's srcs rather than from the SwiftCompile action's inputs, so no transitive inputs are flattened. Targets without directories get the same action as before.
  • WriteGeneratedDirectoriesFilelist and the generated xcfilelist map srcs to paths. They now use expand_directories = False and list a directory itself, since its files aren't known until it's built.

Tests:

  • New unit tests for both: write_generated_directories_filelist (no tests until now) and two write_target_build_settings cases. They fail without the fix; //test/... passes. The mock Args.use_param_file now accepts use_always.
  • Two iOS apps that use rules_apollo (generated GraphQL code as tree artifacts, schema/operations/mocks modules) generate projects and build with xcodebuild; without this change bazel run //:xcodeproj fails as above.

A `swift_library` can have a directory (tree artifact) in `srcs`, e.g.
generated sources. rules_swift 4.2.0 compiles its files individually,
but generating a project failed with "Failed to expand directory":

- `WriteTargetBuildSettings` reuses the `SwiftCompile` action's `Args`,
  which expand the directory, without having it as an input. The
  target's directory `srcs` are now inputs of that action.
- The generated directories filelist and the generated xcfilelist
  expand `srcs` too. They now list a directory itself, since its files
  aren't known until it's built.

Signed-off-by: Ernesto Cambuston <e.cambuston@gmail.com>
@erneestoc
erneestoc requested a review from a team as a code owner November 5, 2026 21:46
@aaronsky

aaronsky commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

It might make sense to generalize this support, rather than doing this just for Swift, seeing as CcCompile actions expand tree artifacts too.

@erneestoc

Copy link
Copy Markdown
Contributor Author

Good call to check. I tried cc_library (C and C++) and objc_library targets with a tree artifact in srcs next to a regular source file:

  • On 4.1.0 project generation fails for them too, in WriteGeneratedDirectoriesFilelist (Failed to expand directory <generated file lib/c_tree>). That part of this PR isn't Swift specific: the filelist and xcfilelist changes cover every target's srcs, and with this branch the project generates and xcodebuild builds.
  • WriteTargetBuildSettings doesn't hit it for C/C++: _get_unprocessed_cc_compiler_opts takes the Args of the CppCompile/ObjcCompile action whose -c file is one of the target's C/C++ sources, and a tree artifact never matches (no extension), so the reused Args don't contain it. The expansion in cc_compilation_helper.bzl is in the module map action, which isn't reused.

So only the Swift Args needed the directories as inputs. I could make swift_directories language-agnostic anyway, but then C-only targets with generated directories would wait for them to build during generation without needing them. Happy to do it if you'd rather have it general, or to add the C/ObjC case as a test.

@aaronsky aaronsky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to unblock CI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants