Skip to content

Commit 184fa12

Browse files
Allow scratchPath to be relative paths
Interpret it as relative to the project root directory if it's a relative path.
1 parent bce0993 commit 184fa12

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Documentation/Configuration File.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The structure of the file is currently not guaranteed to be stable. Options may
1515

1616
- `swiftPM`: Dictionary with the following keys, defining options for SwiftPM workspaces
1717
- `configuration: "debug"|"release"`: The configuration to build the project for during background indexing and the configuration whose build folder should be used for Swift modules if background indexing is disabled. Equivalent to SwiftPM's `--configuration` option.
18-
- `scratchPath: string`: Build artifacts directory path. If nil, the build system may choose a default value. Equivalent to SwiftPM's `--scratch-path` option.
18+
- `scratchPath: string`: Build artifacts directory path. If nil, the build system may choose a default value. This path can be specified as a relative path, which will be interpreted relative to the project root. Equivalent to SwiftPM's `--scratch-path` option.
1919
- `swiftSDKsDirectory: string`: Equivalent to SwiftPM's `--swift-sdks-path` option
2020
- `swiftSDK: string`: Equivalent to SwiftPM's `--swift-sdk` option
2121
- `triple: string`: Equivalent to SwiftPM's `--triple` option

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
324324
if options.backgroundIndexingOrDefault {
325325
location.scratchDirectory = AbsolutePath(projectRoot.appending(components: ".build", "index-build"))
326326
} else if let scratchDirectory = options.swiftPMOrDefault.scratchPath,
327-
let scratchDirectoryPath = try? AbsolutePath(validating: scratchDirectory)
327+
let scratchDirectoryPath = try? AbsolutePath(validating: scratchDirectory, relativeTo: AbsolutePath(projectRoot))
328328
{
329329
location.scratchDirectory = scratchDirectoryPath
330330
}

Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ final class SwiftPMBuildSystemTests: XCTestCase {
9090
}
9191
}
9292

93+
func testRelativeScratchPath() async throws {
94+
try await withTestScratchDir { tempDir in
95+
try localFileSystem.createFiles(
96+
root: tempDir,
97+
files: [
98+
"pkg/Sources/lib/a.swift": "",
99+
"pkg/Package.swift": """
100+
// swift-tools-version:4.2
101+
import PackageDescription
102+
let package = Package(
103+
name: "a",
104+
targets: [.target(name: "lib")]
105+
)
106+
""",
107+
]
108+
)
109+
let packageRoot = tempDir.appending(component: "pkg")
110+
let options = SourceKitLSPOptions(
111+
swiftPM: .init(
112+
scratchPath: "non_default_relative_build_path"
113+
),
114+
backgroundIndexing: false
115+
)
116+
let swiftpmBuildSystem = try await SwiftPMBuildSystem(
117+
projectRoot: packageRoot,
118+
toolchainRegistry: .forTesting,
119+
options: options,
120+
connectionToSourceKitLSP: LocalConnection(receiverName: "dummy"),
121+
testHooks: SwiftPMTestHooks()
122+
)
123+
124+
let dataPath = await swiftpmBuildSystem.destinationBuildParameters.dataPath
125+
let expectedScratchPath = packageRoot.appending(component: try XCTUnwrap(options.swiftPMOrDefault.scratchPath))
126+
XCTAssertTrue(AbsolutePath(dataPath).isDescendant(of: expectedScratchPath))
127+
}
128+
}
129+
93130
func testBasicSwiftArgs() async throws {
94131
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
95132
try await withTestScratchDir { tempDir in

0 commit comments

Comments
 (0)