|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +@_spi(Testing) import SwiftDriver |
| 17 | +import TSCBasic |
| 18 | +import TestUtilities |
| 19 | +import Testing |
| 20 | + |
| 21 | +@Suite struct DirectoryDependenciesTests { |
| 22 | + /// Write a Clang module with an umbrella directory, which gives it a directory |
| 23 | + /// dependency. |
| 24 | + private func writeUmbrellaDirModule(at root: AbsolutePath) throws { |
| 25 | + let include = root.appending(component: "include") |
| 26 | + try localFileSystem.createDirectory(include.appending(component: "sub"), recursive: true) |
| 27 | + try localFileSystem.writeFileContents(include.appending(component: "module.modulemap")) { |
| 28 | + $0.send("module UmbDir { umbrella \"sub\" module * { export * } }\n") |
| 29 | + } |
| 30 | + try localFileSystem.writeFileContents( |
| 31 | + include.appending(components: "sub", "a.h")) { $0.send("void a(void);\n") } |
| 32 | + try localFileSystem.writeFileContents( |
| 33 | + root.appending(component: "test.swift")) { $0.send("import UmbDir\n") } |
| 34 | + } |
| 35 | + |
| 36 | + /// Scan UmbDir as a separate build, with a fresh oracle and `paths` reported |
| 37 | + /// first. |
| 38 | + private func scanBuild(root: AbsolutePath, |
| 39 | + invalidating paths: [AbsolutePath] = []) throws |
| 40 | + -> ModuleInfo { |
| 41 | + let (stdlibPath, shimsPath, toolchain, _) = try getDriverArtifactsForScanning() |
| 42 | + let oracle = InterModuleDependencyOracle() |
| 43 | + let scanLibPath = try #require(try toolchain.lookupSwiftScanLib()) |
| 44 | + try oracle.verifyOrCreateScannerInstance(swiftScanLibPath: scanLibPath) |
| 45 | + try oracle.addInvalidatedPaths(paths) |
| 46 | + var diagnostics: [ScannerDiagnosticPayload] = [] |
| 47 | + let graph = try oracle.getDependencies( |
| 48 | + workingDirectory: root, |
| 49 | + commandLine: [ |
| 50 | + "-scan-dependencies", |
| 51 | + "-module-name", "DirDepTest", |
| 52 | + "-module-cache-path", root.appending(component: "mcp").pathString, |
| 53 | + "-I", stdlibPath.nativePathString(escaped: false), |
| 54 | + "-I", shimsPath.nativePathString(escaped: false), |
| 55 | + "-I", root.appending(component: "include").pathString, |
| 56 | + "-disable-implicit-concurrency-module-import", |
| 57 | + "-disable-implicit-string-processing-module-import", |
| 58 | + root.appending(component: "test.swift").pathString, |
| 59 | + ] + ((try? Driver.sdkArgumentsForTesting()) ?? []), |
| 60 | + diagnostics: &diagnostics) |
| 61 | + return try #require(graph.modules[.clang("UmbDir")]) |
| 62 | + } |
| 63 | + |
| 64 | + /// The basenames of the headers `info` was built from. |
| 65 | + private func headers(_ info: ModuleInfo) -> Set<String> { |
| 66 | + Set((info.sourceFiles ?? []).filter { $0.hasSuffix(".h") }.map { |
| 67 | + (try? AbsolutePath(validating: $0))?.basename ?? $0 |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + @Test(.requireScannerSupportsDirectoryDependencies()) |
| 72 | + func reportsDirectoryDependencies() throws { |
| 73 | + try withTemporaryDirectory { root in |
| 74 | + try writeUmbrellaDirModule(at: root) |
| 75 | + let info = try scanBuild(root: root) |
| 76 | + guard case .clang(let details) = info.details else { |
| 77 | + Issue.record("UmbDir is not a Clang module") |
| 78 | + return |
| 79 | + } |
| 80 | + let reported = (details.directoryDependencies ?? []).map { |
| 81 | + VirtualPath.lookup($0.path).description |
| 82 | + } |
| 83 | + #expect(reported == [root.appending(components: "include", "sub").pathString]) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test(.requireScannerSupportsDirectoryDependencies()) |
| 88 | + func invalidatedDirectoryRebuildsModule() throws { |
| 89 | + try withTemporaryDirectory { root in |
| 90 | + try writeUmbrellaDirModule(at: root) |
| 91 | + let globbedDir = root.appending(components: "include", "sub") |
| 92 | + |
| 93 | + let first = try headers(scanBuild(root: root)) |
| 94 | + #expect(first.contains("a.h")) |
| 95 | + #expect(!first.contains("b.h")) |
| 96 | + |
| 97 | + // Add a header. Without a report the stale module is reused. Modules |
| 98 | + // built in the same second a scan starts count as up to date, so wait. |
| 99 | + Thread.sleep(forTimeInterval: 1) |
| 100 | + try localFileSystem.writeFileContents( |
| 101 | + globbedDir.appending(component: "b.h")) { $0.send("void b(void);\n") } |
| 102 | + let stale = try headers(scanBuild(root: root)) |
| 103 | + #expect(!stale.contains("b.h"), |
| 104 | + "expected the stale cached module to be reused without invalidation") |
| 105 | + |
| 106 | + // Reporting the directory picks up the header. |
| 107 | + let rebuilt = try headers(scanBuild(root: root, invalidating: [globbedDir])) |
| 108 | + #expect(rebuilt.contains("a.h")) |
| 109 | + #expect(rebuilt.contains("b.h"), |
| 110 | + "expected the added header after invalidating its directory") |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments