Skip to content

Commit ca96138

Browse files
authored
Merge pull request swiftlang#260 from ghvg1313/swift-working-directory
Append working directory to swift compiler args when presented in build settings
2 parents e6448df + 8b16168 commit ca96138

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

Sources/SKCore/FileBuildSettings.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,17 @@ public struct FileBuildSettings: Equatable {
3232
workingDirectory: String? = nil,
3333
language: Language)
3434
{
35-
self.compilerArguments = compilerArguments
3635
self.workingDirectory = workingDirectory
3736
self.language = language
37+
// For swift, sourcekit only accepts `working-directry` through compiler args.
38+
// Injecting the arg here if workingDirectory presents in the settings,
39+
// but not in the actual compiler args.
40+
var compilerArgumentsWithWorkingDirectory = compilerArguments
41+
if language == .swift,
42+
let workingDirectory = workingDirectory,
43+
!compilerArguments.contains("-working-directory") {
44+
compilerArgumentsWithWorkingDirectory += ["-working-directory", workingDirectory]
45+
}
46+
self.compilerArguments = compilerArgumentsWithWorkingDirectory
3847
}
3948
}

Tests/SKCoreTests/BuildServerBuildSystemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class BuildServerBuildSystemTests: XCTestCase {
4141
let fileURL = URL(fileURLWithPath: "/path/to/some/file.swift")
4242
let settings = buildSystem.settings(for: DocumentURI(fileURL), Language.swift)
4343
XCTAssertNotNil(settings)
44-
XCTAssertEqual(settings?.compilerArguments, ["-a", "-b"])
44+
XCTAssertEqual(settings?.compilerArguments, ["-a", "-b", "-working-directory", "/path/to/some"])
4545
XCTAssertEqual(settings?.workingDirectory, fileURL.deletingLastPathComponent().path)
4646

4747
// test error

Tests/SKCoreTests/CompilationDatabaseTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ final class CompilationDatabaseTests: XCTestCase {
200200
let settings = buildSystem.settings(for: DocumentURI(URL(fileURLWithPath: "/a/a.swift")), .swift)
201201
XCTAssertNotNil(settings)
202202
XCTAssertEqual(settings?.workingDirectory, "/a")
203-
XCTAssertEqual(settings?.compilerArguments, ["-swift-version", "4", "/a/a.swift"])
203+
XCTAssertEqual(settings?.compilerArguments, ["-swift-version", "4", "/a/a.swift", "-working-directory", "/a"])
204204
XCTAssertNil(buildSystem.indexStorePath)
205205
XCTAssertNil(buildSystem.indexDatabasePath)
206206
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SKCore
14+
import XCTest
15+
16+
final class FileBuildSettingsTests: XCTestCase {
17+
18+
func testSwiftCompilerArg() throws {
19+
let swiftFileBuildSettingsWithWorkingDiectory = FileBuildSettings(compilerArguments: ["a", "b"], workingDirectory: "/path/to/root", language: .swift)
20+
let swiftFileBuildSettingsWithoutWorkingDiectory = FileBuildSettings(compilerArguments: ["a", "b"], language: .swift)
21+
let swiftFileBuildSettingsWithWorkingDiectoryArg = FileBuildSettings(compilerArguments: ["a", "b", "-working-directory", "/custom-root"], workingDirectory: "/path/to/root", language: .swift)
22+
let objcFileBuildSettingsWithWorkingDiectory = FileBuildSettings(compilerArguments: ["a", "b"], workingDirectory: "/path/to/root", language: .objective_c)
23+
XCTAssertEqual(swiftFileBuildSettingsWithWorkingDiectory.compilerArguments, ["a", "b", "-working-directory", "/path/to/root"])
24+
XCTAssertEqual(swiftFileBuildSettingsWithoutWorkingDiectory.compilerArguments, ["a", "b"])
25+
XCTAssertEqual(swiftFileBuildSettingsWithWorkingDiectoryArg.compilerArguments, ["a", "b", "-working-directory", "/custom-root"])
26+
XCTAssertEqual(objcFileBuildSettingsWithWorkingDiectory.compilerArguments, ["a", "b"])
27+
}
28+
}

Tests/SKCoreTests/XCTestManifests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ extension FallbackBuildSystemTests {
7777
]
7878
}
7979

80+
extension FileBuildSettingsTests {
81+
// DO NOT MODIFY: This is autogenerated, use:
82+
// `swift test --generate-linuxmain`
83+
// to regenerate.
84+
static let __allTests__FileBuildSettingsTests = [
85+
("testSwiftCompilerArg", testSwiftCompilerArg),
86+
]
87+
}
88+
8089
extension ToolchainRegistryTests {
8190
// DO NOT MODIFY: This is autogenerated, use:
8291
// `swift test --generate-linuxmain`
@@ -108,6 +117,7 @@ public func __allTests() -> [XCTestCaseEntry] {
108117
testCase(CompilationDatabasePerfTests.__allTests__CompilationDatabasePerfTests),
109118
testCase(CompilationDatabaseTests.__allTests__CompilationDatabaseTests),
110119
testCase(FallbackBuildSystemTests.__allTests__FallbackBuildSystemTests),
120+
testCase(FileBuildSettingsTests.__allTests__FileBuildSettingsTests),
111121
testCase(ToolchainRegistryTests.__allTests__ToolchainRegistryTests),
112122
]
113123
}

0 commit comments

Comments
 (0)