From 1d0e412ed7f3f53133e143fa2eccc13b089765a4 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 20 Jun 2024 22:46:30 +0100 Subject: [PATCH 1/3] =?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. --- 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 4e53ec634e9..8a5049c2586 100644 --- a/Sources/SPMTestSupport/MockWorkspace.swift +++ b/Sources/SPMTestSupport/MockWorkspace.swift @@ -479,7 +479,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 b3a39688140..65b336409b8 100644 --- a/Sources/Workspace/Workspace.swift +++ b/Sources/Workspace/Workspace.swift @@ -1204,7 +1204,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 a6da9d0f7e7aa62fd8b07db437c2f91856d325a4 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 20 Jun 2024 22:51:30 +0100 Subject: [PATCH 2/3] Remove unused imports --- 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 { From 1c30d654a3aaf83347bb851be5bfd56fe7caa6b3 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 20 Jun 2024 23:16:48 +0100 Subject: [PATCH 3/3] Use `FileRuleDescription.header` --- Sources/Workspace/Workspace.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Workspace/Workspace.swift b/Sources/Workspace/Workspace.swift index 65b336409b8..9783717303c 100644 --- a/Sources/Workspace/Workspace.swift +++ b/Sources/Workspace/Workspace.swift @@ -1204,7 +1204,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`. - filePath.extension != "h" && FileRuleDescription.builtinRules.contains { fileRuleDescription in + !FileRuleDescription.header.match(path: filePath, toolsVersion: self.currentToolsVersion) && FileRuleDescription.builtinRules.contains { fileRuleDescription in fileRuleDescription.match(path: filePath, toolsVersion: self.currentToolsVersion) } }