From 9e04d3c111f9ab53672361d61fd7de7255d44550 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 20 Jun 2024 22:46:30 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Don=E2=80=99t=20reload=20package=20manifest?= =?UTF-8?q?=20when=20a=20header=20is=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, any new header file causes a package reload. This means that while background indexing is writing header files into the index build directory, the package will get reloaded, which is unnecessary. (cherry picked from commit 1d0e412ed7f3f53133e143fa2eccc13b089765a4) --- Sources/SPMTestSupport/MockWorkspace.swift | 2 +- Sources/Workspace/Workspace.swift | 2 +- .../WorkspaceTests/WorkspaceReloadTests.swift | 54 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 Tests/WorkspaceTests/WorkspaceReloadTests.swift diff --git a/Sources/SPMTestSupport/MockWorkspace.swift b/Sources/SPMTestSupport/MockWorkspace.swift index 701ca053058..1959783e713 100644 --- a/Sources/SPMTestSupport/MockWorkspace.swift +++ b/Sources/SPMTestSupport/MockWorkspace.swift @@ -475,7 +475,7 @@ public final class MockWorkspace { public func checkPackageGraph( roots: [String] = [], deps: [MockDependency], - _ result: (ModulesGraph, [Basics.Diagnostic]) -> Void + _ result: (ModulesGraph, [Basics.Diagnostic]) throws -> Void ) throws { let dependencies = try deps.map { try $0.convert(baseURL: packagesDir, identityResolver: self.identityResolver) } try self.checkPackageGraph(roots: roots, dependencies: dependencies, result) diff --git a/Sources/Workspace/Workspace.swift b/Sources/Workspace/Workspace.swift index 668274d4fac..569dc1ef116 100644 --- a/Sources/Workspace/Workspace.swift +++ b/Sources/Workspace/Workspace.swift @@ -1208,7 +1208,7 @@ extension Workspace { /// generated by SwiftPM. public func fileAffectsSwiftOrClangBuildSettings(filePath: AbsolutePath, packageGraph: ModulesGraph) -> Bool { // TODO: Implement a more sophisticated check that also verifies if the file is in the sources directories of the passed in `packageGraph`. - FileRuleDescription.builtinRules.contains { fileRuleDescription in + filePath.extension != "h" && FileRuleDescription.builtinRules.contains { fileRuleDescription in fileRuleDescription.match(path: filePath, toolsVersion: self.currentToolsVersion) } } diff --git a/Tests/WorkspaceTests/WorkspaceReloadTests.swift b/Tests/WorkspaceTests/WorkspaceReloadTests.swift new file mode 100644 index 00000000000..b454b5ae83e --- /dev/null +++ b/Tests/WorkspaceTests/WorkspaceReloadTests.swift @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift 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 http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import Basics +import PackageFingerprint +@testable import PackageGraph +import PackageLoading +import PackageModel +import PackageRegistry +import PackageSigning +import SourceControl +import SPMBuildCore +import SPMTestSupport +@testable import Workspace +import XCTest + +final class WorkspaceReloadTests: XCTestCase { + func testHeaderReloading() throws { + let sandbox = AbsolutePath("/tmp/ws/") + let fs = InMemoryFileSystem() + + let workspace = try MockWorkspace( + sandbox: sandbox, + fileSystem: fs, + roots: [ + MockPackage( + name: "Foo", + targets: [ + MockTarget(name: "Foo", dependencies: []), + ] + ), + ] + ) + + try workspace.checkPackageGraph(roots: ["Foo"], deps: []) { graph, diagnostics in + try XCTAssertFalse( + workspace.getOrCreateWorkspace().fileAffectsSwiftOrClangBuildSettings( + filePath: "/tmp/ws/.build/header.h", + packageGraph: graph + ) + ) + XCTAssertNoDiagnostics(diagnostics) + } + } +} From 7f453bbb7a8e6640724d7b52f79b7043e713cb61 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 20 Jun 2024 22:51:30 +0100 Subject: [PATCH 2/2] Remove unused imports (cherry picked from commit a6da9d0f7e7aa62fd8b07db437c2f91856d325a4) --- Tests/WorkspaceTests/WorkspaceReloadTests.swift | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Tests/WorkspaceTests/WorkspaceReloadTests.swift b/Tests/WorkspaceTests/WorkspaceReloadTests.swift index b454b5ae83e..2a6baa99a06 100644 --- a/Tests/WorkspaceTests/WorkspaceReloadTests.swift +++ b/Tests/WorkspaceTests/WorkspaceReloadTests.swift @@ -11,16 +11,8 @@ //===----------------------------------------------------------------------===// import Basics -import PackageFingerprint -@testable import PackageGraph -import PackageLoading -import PackageModel -import PackageRegistry -import PackageSigning -import SourceControl -import SPMBuildCore import SPMTestSupport -@testable import Workspace +import Workspace import XCTest final class WorkspaceReloadTests: XCTestCase {