Skip to content

Merge main into release/6.0 #1213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4d9105b
Skip new tests that use Swift modules if the host’s SwiftPM doesn’t s…
ahoppen Apr 24, 2024
1eb459e
Elevate the log level for messages that record which toolchain was us…
ahoppen Apr 24, 2024
1d9b67b
Don’t include files from package dependencies in the syntactic test i…
ahoppen Apr 24, 2024
76c0860
Support member access in tags
plemarquand Apr 19, 2024
def5285
Account for nested structs on Tag declarations
plemarquand Apr 19, 2024
35fe4aa
More robust fully qualified name resolution
plemarquand Apr 20, 2024
3870fae
Add test case for Tag.baz
plemarquand Apr 20, 2024
147f7d2
Break out testSwiftTestingTestWithTags into two tests
plemarquand Apr 22, 2024
bff4a1e
Support statically defined tags
plemarquand Apr 22, 2024
f77f22b
All statics on Tag have a leading .
plemarquand Apr 22, 2024
4628626
Simplify getting MemberAccessExprSyntax.components
plemarquand Apr 22, 2024
3bca8da
Remove leading dot on string tag representation
plemarquand Apr 24, 2024
3b77989
Update tag tests
plemarquand Apr 24, 2024
e26461e
More robust fully qualified name resolution
plemarquand Apr 20, 2024
542de55
Add test case for Tag.baz
plemarquand Apr 20, 2024
ef4c7fb
Break out testSwiftTestingTestWithTags into two tests
plemarquand Apr 22, 2024
6f850ab
Update tag tests
plemarquand Apr 24, 2024
f01bb50
Merge pull request #1203 from ahoppen/skip-module-tests
ahoppen Apr 24, 2024
1dd429b
Merge pull request #1204 from ahoppen/elevate-toolchain-selection-log…
ahoppen Apr 24, 2024
eb106c0
Lint fix
plemarquand Apr 24, 2024
d11230c
Fix JSON encoding for TextDocumentEdit and VersionedTextDocumentIdent…
DougGregor Apr 20, 2024
531d577
Add refactoring action for adding a test target to a package manifest
DougGregor Apr 20, 2024
0830a95
Address code review and add "Add product to export this target" action
DougGregor Apr 23, 2024
5428b9c
Reformat
DougGregor Apr 23, 2024
f6d286e
Fixup for coding change to OptionalVersionedTextDocumentIdentifier
DougGregor Apr 23, 2024
e540f16
Add PackageManifestEdits.swift to CMake build
DougGregor Apr 23, 2024
502c9a8
[CMake] Add dependency on SwiftPM's PackageModuleSyntax
DougGregor Apr 24, 2024
3674e7f
Address code review feedback
DougGregor Apr 25, 2024
7015084
Lint fix again
plemarquand Apr 25, 2024
bde432b
Remove obsolete `availableLibraries` argument from `loadPackageGraph`
xedin Apr 26, 2024
8046100
Merge pull request #1207 from xedin/remove-available-libraries-parameter
xedin Apr 26, 2024
e0f7221
Merge pull request #1193 from DougGregor/package-editing-refactor
DougGregor Apr 27, 2024
8f8e50e
Merge pull request #1191 from plemarquand/tag-member-access-syntax
plemarquand Apr 29, 2024
f89cf51
Trim parameter names in SwiftTestingScanner
plemarquand Apr 30, 2024
ccabf7a
Use .text over .trimmed
plemarquand Apr 30, 2024
7c4acf9
Merge pull request #1209 from plemarquand/fix-test-item-arg-trivia-tr…
plemarquand May 1, 2024
ab68dc5
Merge pull request #1201 from ahoppen/no-tests-from-package-dependencies
ahoppen May 1, 2024
365fb21
Merge branch 'main' into 6.0/merge-main-2024-05-01
ahoppen May 1, 2024
fe4bbd3
Revert "Remove obsolete `availableLibraries` argument from `loadPacka…
ahoppen May 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ let package = Package(
.product(name: "SwiftRefactor", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
.product(name: "SwiftPM-auto", package: "swift-package-manager"),
],
exclude: ["CMakeLists.txt"]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@ public struct OptionalVersionedTextDocumentIdentifier: Hashable, Codable, Sendab
self.uri = uri
self.version = version
}

enum CodingKeys: CodingKey {
case uri
case version
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.uri, forKey: .uri)

// Note: we use encode(_:forKey:) here instead of encodeIf(_:forKey:)
// because VSCode will drop requests without the explicit 'null'.
try container.encode(self.version, forKey: .version)
}
}
13 changes: 9 additions & 4 deletions Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ extension SwiftPMBuildSystem {
self.fileToTarget = [AbsolutePath: SwiftBuildTarget](
modulesGraph.allTargets.flatMap { target in
return target.sources.paths.compactMap {
guard let buildTarget = buildDescription.getBuildTarget(for: target) else {
guard let buildTarget = buildDescription.getBuildTarget(for: target, in: modulesGraph) else {
return nil
}
return (key: $0, value: buildTarget)
Expand All @@ -277,7 +277,7 @@ extension SwiftPMBuildSystem {

self.sourceDirToTarget = [AbsolutePath: SwiftBuildTarget](
modulesGraph.allTargets.compactMap { (target) -> (AbsolutePath, SwiftBuildTarget)? in
guard let buildTarget = buildDescription.getBuildTarget(for: target) else {
guard let buildTarget = buildDescription.getBuildTarget(for: target, in: modulesGraph) else {
return nil
}
return (key: target.sources.root, value: buildTarget)
Expand Down Expand Up @@ -439,8 +439,13 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
}

public func testFiles() -> [DocumentURI] {
// We should only include source files from test targets (https://github.com/apple/sourcekit-lsp/issues/1174).
return fileToTarget.map { DocumentURI($0.key.asURL) }
return fileToTarget.compactMap { (path, target) -> DocumentURI? in
guard target.isPartOfRootPackage else {
// Don't consider files from package dependencies as possible test files.
return nil
}
return DocumentURI(path.asURL)
}
}

public func addTestFilesDidChangeCallback(_ callback: @Sendable @escaping () async -> Void) async {
Expand Down
17 changes: 11 additions & 6 deletions Sources/SKTestSupport/MultiFileTestProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public struct RelativeFileLocation: Hashable, ExpressibleByStringLiteral {
let components = value.components(separatedBy: "/")
self.init(directories: components.dropLast(), components.last!)
}

public func url(relativeTo: URL) -> URL {
var url = relativeTo
for directory in directories {
url = url.appendingPathComponent(directory)
}
url = url.appendingPathComponent(fileName)
return url
}
}

/// A test project that writes multiple files to disk and opens a `TestSourceKitLSPClient` client with a workspace
Expand Down Expand Up @@ -69,7 +78,7 @@ public class MultiFileTestProject {
/// File contents can also contain `$TEST_DIR`, which gets replaced by the temporary directory.
public init(
files: [RelativeFileLocation: String],
workspaces: (URL) -> [WorkspaceFolder] = { [WorkspaceFolder(uri: DocumentURI($0))] },
workspaces: (URL) async throws -> [WorkspaceFolder] = { [WorkspaceFolder(uri: DocumentURI($0))] },
usePullDiagnostics: Bool = true,
testName: String = #function
) async throws {
Expand All @@ -79,11 +88,7 @@ public class MultiFileTestProject {
var fileData: [String: FileData] = [:]
for (fileLocation, markedText) in files {
let markedText = markedText.replacingOccurrences(of: "$TEST_DIR", with: scratchDirectory.path)
var fileURL = scratchDirectory
for directory in fileLocation.directories {
fileURL = fileURL.appendingPathComponent(directory)
}
fileURL = fileURL.appendingPathComponent(fileLocation.fileName)
let fileURL = fileLocation.url(relativeTo: scratchDirectory)
try FileManager.default.createDirectory(
at: fileURL.deletingLastPathComponent(),
withIntermediateDirectories: true
Expand Down
108 changes: 108 additions & 0 deletions Sources/SKTestSupport/SwiftPMDependencyProject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import ISDBTibs
import XCTest

import struct TSCBasic.AbsolutePath
import class TSCBasic.Process
import enum TSCBasic.ProcessEnv
import struct TSCBasic.ProcessResult

/// A SwiftPM package that gets written to disk and for which a Git repository is initialized with a commit tagged
/// `1.0.0`. This repository can then be used as a dependency for another package, usually a `SwiftPMTestProject`.
public class SwiftPMDependencyProject {
/// The directory in which the repository lives.
public let packageDirectory: URL

private func runCommand(_ toolName: String, _ arguments: [String], workingDirectory: URL) async throws {
enum Error: Swift.Error {
case cannotFindTool(toolName: String)
case processedTerminatedWithNonZeroExitCode(ProcessResult)
}
guard let toolUrl = findTool(name: toolName) else {
if ProcessEnv.block["SWIFTCI_USE_LOCAL_DEPS"] == nil {
// Never skip the test in CI, similar to what SkipUnless does.
throw XCTSkip("\(toolName) cannot be found")
}
throw Error.cannotFindTool(toolName: toolName)
}
print([toolUrl.path] + arguments)
let process = TSCBasic.Process(
arguments: [toolUrl.path] + arguments,
workingDirectory: try AbsolutePath(validating: workingDirectory.path)
)
try process.launch()
let processResult = try await process.waitUntilExit()
guard processResult.exitStatus == .terminated(code: 0) else {
throw Error.processedTerminatedWithNonZeroExitCode(processResult)
}
}

public static let defaultPackageManifest: String = """
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
name: "MyDependency",
products: [.library(name: "MyDependency", targets: ["MyDependency"])],
targets: [.target(name: "MyDependency")]
)
"""

public init(
files: [RelativeFileLocation: String],
manifest: String = defaultPackageManifest,
testName: String = #function
) async throws {
packageDirectory = try testScratchDir(testName: testName).appendingPathComponent("MyDependency")

var files = files
files["Package.swift"] = manifest

for (fileLocation, contents) in files {
let fileURL = fileLocation.url(relativeTo: packageDirectory)
try FileManager.default.createDirectory(
at: fileURL.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try contents.write(to: fileURL, atomically: true, encoding: .utf8)
}

try await runCommand("git", ["init"], workingDirectory: packageDirectory)
try await runCommand(
"git",
["add"] + files.keys.map { $0.url(relativeTo: packageDirectory).path },
workingDirectory: packageDirectory
)
try await runCommand(
"git",
["-c", "user.name=Dummy", "-c", "[email protected]", "commit", "-m", "Initial commit"],
workingDirectory: packageDirectory
)
try await runCommand("git", ["tag", "1.0.0"], workingDirectory: packageDirectory)
}

deinit {
if cleanScratchDirectories {
try? FileManager.default.removeItem(at: packageDirectory)
}
}

/// Function that makes sure the project stays alive until this is called. Otherwise, the `SwiftPMDependencyProject`
/// might get deinitialized, which deletes the package on disk.
public func keepAlive() {
withExtendedLifetime(self) { _ in }
}
}
17 changes: 16 additions & 1 deletion Sources/SKTestSupport/SwiftPMTestProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SwiftPMTestProject: MultiFileTestProject {
public init(
files: [RelativeFileLocation: String],
manifest: String = SwiftPMTestProject.defaultPackageManifest,
workspaces: (URL) -> [WorkspaceFolder] = { [WorkspaceFolder(uri: DocumentURI($0))] },
workspaces: (URL) async throws -> [WorkspaceFolder] = { [WorkspaceFolder(uri: DocumentURI($0))] },
build: Bool = false,
allowBuildFailure: Bool = false,
usePullDiagnostics: Bool = true,
Expand All @@ -59,6 +59,7 @@ public class SwiftPMTestProject: MultiFileTestProject {
filesByPath[RelativeFileLocation(directories: directories, fileLocation.fileName)] = contents
}
filesByPath["Package.swift"] = manifest

try await super.init(
files: filesByPath,
workspaces: workspaces,
Expand Down Expand Up @@ -96,4 +97,18 @@ public class SwiftPMTestProject: MultiFileTestProject {
environment["SWIFTPM_ENABLE_CLANG_INDEX_STORE"] = "1"
try await Process.checkNonZeroExit(arguments: arguments, environmentBlock: environment)
}

/// Resolve package dependencies for the package at `path`.
public static func resolvePackageDependencies(at path: URL) async throws {
guard let swift = await ToolchainRegistry.forTesting.default?.swift?.asURL else {
throw Error.swiftNotFound
}
let arguments = [
swift.path,
"package",
"resolve",
"--package-path", path.path,
]
try await Process.checkNonZeroExit(arguments: arguments)
}
}
2 changes: 2 additions & 0 deletions Sources/SourceKitLSP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ target_sources(SourceKitLSP PRIVATE
target_sources(SourceKitLSP PRIVATE
Swift/AdjustPositionToStartOfIdentifier.swift
Swift/CodeActions/ConvertIntegerLiteral.swift
Swift/CodeActions/PackageManifestEdits.swift
Swift/CodeActions/SyntaxCodeActionProvider.swift
Swift/CodeActions/SyntaxCodeActions.swift
Swift/CodeActions/SyntaxRefactoringCodeActionProvider.swift
Expand Down Expand Up @@ -74,5 +75,6 @@ target_link_libraries(SourceKitLSP PUBLIC
SwiftSyntax::SwiftRefactor
SwiftSyntax::SwiftSyntax)
target_link_libraries(SourceKitLSP PRIVATE
PackageModelSyntax
$<$<NOT:$<PLATFORM_ID:Darwin>>:FoundationXML>)

2 changes: 1 addition & 1 deletion Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ public actor SourceKitLSPServer {
return nil
}

logger.info("Using toolchain \(toolchain.displayName) (\(toolchain.identifier)) for \(uri.forLogging)")
logger.log("Using toolchain \(toolchain.displayName) (\(toolchain.identifier)) for \(uri.forLogging)")

if let concurrentlySetService = workspace.documentService[uri] {
// Since we await the construction of `service`, another call to this
Expand Down
Loading