Skip to content

Commit b154c3c

Browse files
authored
Set -derivedDataPath when building the EditorExtension Xcode project (#2099)
Previously, we were sometimes seeing a `build` directory being created in the `swift-syntax` sources directory, which contained dummy input files that didn’t have a trailing newline, which caused swift-format to complain about their formatting. And while at it, also make sure that we clean the temporary directory that we create to verify the generated files.
1 parent b57df1e commit b154c3c

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/Build.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Build: ParsableCommand, BuildCommand {
2626
private func buildEditorExtension() throws {
2727
#if os(macOS)
2828
logSection("Building Editor Extension")
29-
try invokeXcodeBuild(projectPath: Paths.editorExtensionProjectPath)
29+
try invokeXcodeBuild(projectPath: Paths.editorExtensionProjectPath, scheme: "SwiftRefactorExtension")
3030
#endif
3131
}
3232
}

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/VerifySourceCode.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ struct VerifySourceCode: ParsableCommand, SourceCodeGeneratorCommand {
2222
var arguments: SourceCodeGeneratorArguments
2323

2424
func run() throws {
25-
let tempDir = FileManager.default.temporaryDirectory
25+
try withTemporaryDirectory(verifyCodeGeneratedFiles(tempDir:))
26+
}
2627

28+
private func verifyCodeGeneratedFiles(tempDir: URL) throws {
2729
try self.runCodeGeneration(sourceDir: tempDir)
2830

2931
logSection("Verifing code generated files")

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/BuildCommand.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,30 @@ extension BuildCommand {
9090
}
9191

9292
@discardableResult
93-
func invokeXcodeBuild(projectPath: URL) throws -> ProcessResult {
94-
guard let xcodebuildExec = Paths.xcodebuildExec else {
95-
throw ScriptExectutionError(
96-
message: """
97-
Error: Could not find xcodebuild.
98-
Looking at '\(Paths.xcodebuildExec?.path ?? "N/A")'.
99-
"""
93+
func invokeXcodeBuild(projectPath: URL, scheme: String) throws -> ProcessResult {
94+
return try withTemporaryDirectory { tempDir in
95+
guard let xcodebuildExec = Paths.xcodebuildExec else {
96+
throw ScriptExectutionError(
97+
message: """
98+
Error: Could not find xcodebuild.
99+
Looking at '\(Paths.xcodebuildExec?.path ?? "N/A")'.
100+
"""
101+
)
102+
}
103+
let processRunner = ProcessRunner(
104+
executableURL: xcodebuildExec,
105+
arguments: [
106+
"-project", projectPath.path,
107+
"-scheme", scheme,
108+
"-derivedDataPath", tempDir.path,
109+
],
110+
additionalEnvironment: [:]
100111
)
101-
}
102-
let processRunner = ProcessRunner(
103-
executableURL: xcodebuildExec,
104-
arguments: ["-project", projectPath.path],
105-
additionalEnvironment: [:]
106-
)
107112

108-
let result = try processRunner.run(verbose: arguments.verbose)
113+
let result = try processRunner.run(verbose: arguments.verbose)
109114

110-
return result
115+
return result
116+
}
111117
}
112118

113119
private func build(packageDir: URL, name: String, isProduct: Bool) throws {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 Foundation
14+
15+
/// Executes body with a URL to a temporary directory that will be deleted after
16+
/// the closure finishes executing.
17+
func withTemporaryDirectory<T>(_ body: (URL) throws -> T) throws -> T {
18+
let tempDirURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
19+
try FileManager.default.createDirectory(at: tempDirURL, withIntermediateDirectories: true)
20+
defer {
21+
try? FileManager.default.removeItem(at: tempDirURL)
22+
}
23+
return try body(tempDirURL)
24+
}

0 commit comments

Comments
 (0)