Skip to content

Commit

Permalink
Include Swift sources in runfiles to ensure they can be read during i…
Browse files Browse the repository at this point in the history
…ndexing (#797)
  • Loading branch information
ileitch authored Aug 20, 2024
1 parent 5bf1c46 commit 92c0267
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ on:
pull_request: {}
env:
swift_package_resolve: swift package resolve
swift_build: swift build --build-tests
swift_test: swift test
periphery_scan: ./.build/debug/periphery scan --quiet --skip-build
periphery_scan: ./.build/debug/periphery scan --quiet --clean-build
cache_version: 1
jobs:
macOS:
Expand Down Expand Up @@ -45,12 +44,10 @@ jobs:
- name: Resolve dependencies
if: steps.cache-resolved-dependencies.outputs.cache-hit != 'true'
run: ${{ env.swift_package_resolve }}
- name: Build
run: ${{ env.swift_build }}
- name: Test
run: ${{ env.swift_test }}
- name: Scan
run: ${{ env.periphery_scan }} --strict
- name: Test
run: ${{ env.swift_test }}
linux:
strategy:
fail-fast: false
Expand Down Expand Up @@ -86,9 +83,7 @@ jobs:
- name: Resolve dependencies
if: steps.cache-resolved-dependencies.outputs.cache-hit != 'true'
run: ${{ env.swift_package_resolve }}
- name: Build
run: ${{ env.swift_build }}
- name: Test
run: ${{ env.swift_test }}
- name: Scan
run: ${{ env.periphery_scan }}
- name: Test
run: ${{ env.swift_test }}
20 changes: 12 additions & 8 deletions Sources/Indexer/SourceFileCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ public struct SourceFileCollector {
let units = indexStore.units(includeSystem: false)

return try units.compactMap { unit -> (FilePath, IndexStore, IndexStoreUnit, String?)? in
guard let filePath = try indexStore.mainFilePath(for: unit), !filePath.isEmpty else { return nil }
guard let filePath = try indexStore.mainFilePath(for: unit), !filePath.isEmpty else {
return nil
}

let file = FilePath.makeAbsolute(filePath, relativeTo: currentFilePath)

if file.exists {
if !isExcluded(file) {
let module = try indexStore.moduleName(for: unit)
if let module, excludedTargets.contains(module) {
return nil
}
guard file.exists else {
throw PeripheryError.pathDoesNotExist(path: file.string)
}

return (file, indexStore, unit, module)
if !isExcluded(file) {
let module = try indexStore.moduleName(for: unit)
if let module, excludedTargets.contains(module) {
return nil
}

return (file, indexStore, unit, module)
}

return nil
Expand Down
20 changes: 18 additions & 2 deletions bazel/internal/scan.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load("@rules_apple//apple:providers.bzl", "AppleResourceInfo")
PeripheryInfo = provider(
doc = "Provides inputs needed to generate a generic project configuration file.",
fields = {
"swift_srcs": "A depset of Swift source files.",
"indexstores": "A depset of .indexstore files.",
"plists": "A depset of .plist files.",
"xibs": "A depset of .xib and .storyboard files.",
Expand Down Expand Up @@ -32,6 +33,7 @@ _force_indexstore = transition(
)

def _scan_inputs_aspect_impl(target, ctx):
swift_srcs = []
indexstores = []
test_targets = []
plists = []
Expand All @@ -43,6 +45,9 @@ def _scan_inputs_aspect_impl(target, ctx):
if SwiftInfo in target and hasattr(target[SwiftInfo], "direct_modules"):
for module in target[SwiftInfo].direct_modules:
if hasattr(module, "swift"):
if hasattr(module.compilation_context, "direct_sources"):
swift_srcs.extend([src for src in module.compilation_context.direct_sources if src.extension == "swift"])

if ctx.rule.attr.testonly:
test_targets.append(module.name)

Expand Down Expand Up @@ -76,6 +81,10 @@ def _scan_inputs_aspect_impl(target, ctx):

deps = getattr(ctx.rule.attr, "deps", [])

swift_srcs_depset = depset(
swift_srcs,
transitive = [dep[PeripheryInfo].swift_srcs for dep in deps],
)
indexstores_depset = depset(
indexstores,
transitive = [dep[PeripheryInfo].indexstores for dep in deps],
Expand Down Expand Up @@ -103,6 +112,7 @@ def _scan_inputs_aspect_impl(target, ctx):

return [
PeripheryInfo(
swift_srcs = swift_srcs_depset,
indexstores = indexstores_depset,
plists = plists_depset,
xibs = xibs_depset,
Expand All @@ -113,6 +123,7 @@ def _scan_inputs_aspect_impl(target, ctx):
]

def _scan_impl(ctx):
swift_srcs_set = sets.make()
indexstores_set = sets.make()
plists_set = sets.make()
xibs_set = sets.make()
Expand All @@ -121,13 +132,15 @@ def _scan_impl(ctx):
test_targets_set = sets.make()

for dep in ctx.attr.deps:
swift_srcs_set = sets.union(swift_srcs_set, sets.make(dep[PeripheryInfo].swift_srcs.to_list()))
indexstores_set = sets.union(indexstores_set, sets.make(dep[PeripheryInfo].indexstores.to_list()))
plists_set = sets.union(plists_set, sets.make(dep[PeripheryInfo].plists.to_list()))
xibs_set = sets.union(xibs_set, sets.make(dep[PeripheryInfo].xibs.to_list()))
xcdatamodels_set = sets.union(xcdatamodels_set, sets.make(dep[PeripheryInfo].xcdatamodels.to_list()))
xcmappingmodels_set = sets.union(xcmappingmodels_set, sets.make(dep[PeripheryInfo].xcmappingmodels.to_list()))
test_targets_set = sets.union(test_targets_set, sets.make(dep[PeripheryInfo].test_targets.to_list()))

swift_srcs = sets.to_list(swift_srcs_set)
indexstores = sets.to_list(indexstores_set)
plists = sets.to_list(plists_set)
xibs = sets.to_list(xibs_set)
Expand Down Expand Up @@ -164,13 +177,16 @@ def _scan_impl(ctx):
[ctx.outputs.scan, project_config_file],
),
runfiles = ctx.runfiles(
files = indexstores + plists + xibs + xcdatamodels + xcmappingmodels,
# Swift sources are not included in the generate project file, yet they are referenced
# in the indexstores and will be read by Periphery, and therefore must be present in
# the runfiles.
files = swift_srcs + indexstores + plists + xibs + xcdatamodels + xcmappingmodels,
)
)

scan_inputs_aspect = aspect(
_scan_inputs_aspect_impl,
attr_aspects = ["deps"],
attr_aspects = ["deps", "swift_target"],
)

scan = rule(
Expand Down

0 comments on commit 92c0267

Please sign in to comment.