Skip to content

Commit 3bbca26

Browse files
authored
[6.0/Prepare] Don't skip non-exportable-decls when enable-testing (#7675) (#7686)
Explanation: @testable doesn't work if you skip non-exportable-decls. Make sure we don't turn on that flag if the target is enable-testing. Scope: Prepare for indexing only Risk: isolated to prepare for indexing with is behind an experimental flag Original PR: #7675 Testing: Unit-Test
1 parent c21a148 commit 3bbca26

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,13 @@ public final class SwiftTargetBuildDescription {
539539
}
540540

541541
if self.buildParameters.prepareForIndexing {
542+
if !args.contains("-enable-testing") {
543+
// enable-testing needs the non-exportable-decls
544+
args += ["-Xfrontend", "-experimental-skip-non-exportable-decls"]
545+
}
542546
args += [
543547
"-Xfrontend", "-experimental-skip-all-function-bodies",
544548
"-Xfrontend", "-experimental-lazy-typecheck",
545-
"-Xfrontend", "-experimental-skip-non-exportable-decls",
546549
"-Xfrontend", "-experimental-allow-module-with-compiler-errors",
547550
"-Xfrontend", "-empty-abi-descriptor"
548551
]

Tests/BuildTests/PrepareForIndexTests.swift

+76-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
import Build
1414
import Foundation
1515
import LLBuildManifest
16-
@_spi(SwiftPMInternal)
17-
import SPMTestSupport
1816
import TSCBasic
1917
import XCTest
18+
@_spi(SwiftPMInternal)
19+
import SPMTestSupport
20+
import class Basics.ObservabilitySystem
21+
@_spi(DontAdoptOutsideOfSwiftPMExposedForBenchmarksAndTestsOnly)
22+
import class PackageModel.Manifest
23+
import struct PackageModel.TargetDescription
2024

2125
class PrepareForIndexTests: XCTestCase {
2226
func testPrepare() throws {
@@ -82,4 +86,74 @@ class PrepareForIndexTests: XCTestCase {
8286
let name = lib.getLLBuildTargetName(buildParameters: plan.destinationBuildParameters)
8387
XCTAssertTrue(manifest.targets.keys.contains(name))
8488
}
89+
90+
// enable-testing requires the non-exportable-decls, make sure they aren't skipped.
91+
func testEnableTesting() throws {
92+
let fs = InMemoryFileSystem(
93+
emptyFiles:
94+
"/Pkg/Sources/lib/lib.swift",
95+
"/Pkg/Tests/test/TestCase.swift"
96+
)
97+
98+
let observability = ObservabilitySystem.makeForTesting()
99+
let scope = observability.topScope
100+
101+
let graph = try loadModulesGraph(
102+
fileSystem: fs,
103+
manifests: [
104+
Manifest.createRootManifest(
105+
displayName: "Pkg",
106+
path: "/Pkg",
107+
targets: [
108+
TargetDescription(name: "lib", dependencies: []),
109+
TargetDescription(name: "test", dependencies: ["lib"], type: .test),
110+
]
111+
),
112+
],
113+
observabilityScope: scope
114+
)
115+
XCTAssertNoDiagnostics(observability.diagnostics)
116+
117+
// Under debug, enable-testing is turned on by default. Make sure the flag is not added.
118+
let debugPlan = try BuildPlan(
119+
destinationBuildParameters: mockBuildParameters(destination: .target, config: .debug, prepareForIndexing: true),
120+
toolsBuildParameters: mockBuildParameters(destination: .host, prepareForIndexing: false),
121+
graph: graph,
122+
fileSystem: fs,
123+
observabilityScope: observability.topScope
124+
)
125+
let debugBuilder = LLBuildManifestBuilder(debugPlan, fileSystem: fs, observabilityScope: scope)
126+
let debugManifest = try debugBuilder.generatePrepareManifest(at: "/manifest")
127+
128+
XCTAssertNil(debugManifest.commands.values.first(where: {
129+
guard let swiftCommand = $0.tool as? SwiftCompilerTool,
130+
swiftCommand.outputs.contains(where: { $0.name.hasSuffix("/lib.swiftmodule")})
131+
else {
132+
return false
133+
}
134+
return swiftCommand.otherArguments.contains("-experimental-skip-non-exportable-decls")
135+
&& !swiftCommand.otherArguments.contains("-enable-testing")
136+
}))
137+
138+
// Under release, enable-testing is turned off by default so we should see our flag
139+
let releasePlan = try BuildPlan(
140+
destinationBuildParameters: mockBuildParameters(destination: .target, config: .release, prepareForIndexing: true),
141+
toolsBuildParameters: mockBuildParameters(destination: .host, prepareForIndexing: false),
142+
graph: graph,
143+
fileSystem: fs,
144+
observabilityScope: observability.topScope
145+
)
146+
let releaseBuilder = LLBuildManifestBuilder(releasePlan, fileSystem: fs, observabilityScope: scope)
147+
let releaseManifest = try releaseBuilder.generatePrepareManifest(at: "/manifest")
148+
149+
XCTAssertEqual(releaseManifest.commands.values.filter({
150+
guard let swiftCommand = $0.tool as? SwiftCompilerTool,
151+
swiftCommand.outputs.contains(where: { $0.name.hasSuffix("/lib.swiftmodule")})
152+
else {
153+
return false
154+
}
155+
return swiftCommand.otherArguments.contains("-experimental-skip-non-exportable-decls")
156+
&& !swiftCommand.otherArguments.contains("-enable-testing")
157+
}).count, 1)
158+
}
85159
}

0 commit comments

Comments
 (0)