File: TargetSourcesBuilder.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (849 lines) | stat: -rw-r--r-- 33,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2021 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 Foundation
import PackageModel

/// A utility to compute the source/resource files of a target.
public struct TargetSourcesBuilder {
    /// The package identity.
    public let packageIdentity: PackageIdentity

    /// The package kind.
    public let packageKind: PackageReference.Kind

    /// The package path.
    public let packagePath: AbsolutePath

    /// The target for which we're computing source/resource files.
    public let target: TargetDescription

    /// The path of the target.
    public let targetPath: AbsolutePath

    /// The list of declared sources in the package manifest.
    public let declaredSources: [AbsolutePath]?

    /// The list of declared resources in the package manifest.
    public let declaredResources: [(path: AbsolutePath, rule: TargetDescription.Resource.Rule)]

    /// The default localization.
    public let defaultLocalization: String?

    /// The rules that can be applied to files in the target.
    public let rules: [FileRuleDescription]

    /// The tools version associated with the target's package.
    public let toolsVersion: ToolsVersion

    /// The set of paths that should be excluded from any consideration.
    public let excludedPaths: Set<AbsolutePath>

    /// The set of opaque directories extensions (should not be treated as source)
    private let opaqueDirectoriesExtensions: Set<String>

    /// The file system to operate on.
    private let fileSystem: FileSystem

    /// ObservabilityScope with which to emit diagnostics
    private let observabilityScope: ObservabilityScope

    /// Create a new target builder.
    public init(
        packageIdentity: PackageIdentity,
        packageKind: PackageReference.Kind,
        packagePath: AbsolutePath,
        target: TargetDescription,
        path: AbsolutePath,
        defaultLocalization: String?,
        additionalFileRules: [FileRuleDescription],
        toolsVersion: ToolsVersion,
        fileSystem: FileSystem,
        observabilityScope: ObservabilityScope
    ) {
        self.packageIdentity = packageIdentity
        self.packageKind = packageKind
        self.packagePath = packagePath
        self.target = target
        self.defaultLocalization = defaultLocalization
        self.targetPath = path
        self.rules = Self.rules(additionalFileRules: additionalFileRules, toolsVersion: toolsVersion)
        self.toolsVersion = toolsVersion
        let excludedPaths = target.exclude.compactMap { try? AbsolutePath(validating: $0, relativeTo: path) }
        self.excludedPaths = Set(excludedPaths)
        self.opaqueDirectoriesExtensions = FileRuleDescription.opaqueDirectoriesExtensions.union(
            additionalFileRules.reduce(into: Set<String>(), { partial, item in
                partial.formUnion(item.fileTypes)
            })
        )
        self.fileSystem = fileSystem

        self.observabilityScope = observabilityScope.makeChildScope(description: "TargetSourcesBuilder") {
            var metadata = ObservabilityMetadata.packageMetadata(identity: packageIdentity, kind: packageKind)
            metadata.moduleName = target.name
            return metadata
        }

        let declaredSources = target.sources?.compactMap { try? AbsolutePath(validating: $0, relativeTo: path) }
        if let declaredSources {
            // Diagnose duplicate entries.
            let duplicates = declaredSources.spm_findDuplicateElements()
            if !duplicates.isEmpty {
                for duplicate in duplicates {
                    self.observabilityScope.emit(warning: "found duplicate sources declaration in the package manifest: \(duplicate.map{ $0.pathString }[0])")
                }
            }
        }
        self.declaredSources = declaredSources?.spm_uniqueElements()

        self.declaredResources = (try? target.resources.map {
            (path: try AbsolutePath(validating: $0.path, relativeTo: path), rule: $0.rule)
        }) ?? []

        self.excludedPaths.forEach { exclude in
            if let message = validTargetPath(at: exclude), self.packageKind.emitAuthorWarnings {
                let warning = "Invalid Exclude '\(exclude)': \(message)."
                self.observabilityScope.emit(warning: warning)
            }
        }

        self.declaredSources?.forEach { source in
            if let message = validTargetPath(at: source) {
                let warning = "Invalid Source '\(source)': \(message)."
                self.observabilityScope.emit(warning: warning)
            }
        }

      #if DEBUG
        validateRules(self.rules)
      #endif
    }

    private static func rules(additionalFileRules: [FileRuleDescription], toolsVersion: ToolsVersion) -> [FileRuleDescription] {
        // In version 5.4 and earlier, SwiftPM did not support `additionalFileRules` and always implicitly included XCBuild file types.
        let actualAdditionalRules = (toolsVersion <= .v5_4 ? FileRuleDescription.xcbuildFileTypes : additionalFileRules)
        return FileRuleDescription.builtinRules + actualAdditionalRules
    }

    @discardableResult
    private func validTargetPath(at: AbsolutePath) -> Error? {
        // Check if paths that are enumerated in targets: [] exist
        guard self.fileSystem.exists(at) else {
            return StringError("File not found")
        }

        // Excludes, Sources, and Resources should be found at the root of the package and or
        // its subdirectories
        guard at.pathString.hasPrefix(self.packagePath.pathString) else {
            return StringError("File must be within the package directory structure")
        }

        return nil
    }

    /// Emits an error in debug mode if we have conflicting rules for any file type.
    private func validateRules(_ rules: [FileRuleDescription]) {
        var extensionMap: [String: FileRuleDescription] = [:]
        for rule in rules {
            for ext in rule.fileTypes {
                if let existingRule = extensionMap[ext] {
                    self.observabilityScope.emit(error: "conflicting rules \(rule) and \(existingRule) for extension \(ext)")
                }
                extensionMap[ext] = rule
            }
        }
    }

    /// Run the builder to produce the sources of the target.
    public func run() throws -> (sources: Sources, resources: [Resource], headers: [AbsolutePath], ignored: [AbsolutePath], others: [AbsolutePath]) {
        let contents = self.computeContents()
        var pathToRule: [AbsolutePath: FileRuleDescription.Rule] = [:]

        var handledResources = [AbsolutePath]()
        for path in contents {
            pathToRule[path] = Self.computeRule(
                for: path,
                toolsVersion: toolsVersion,
                rules: rules,
                declaredResources: declaredResources,
                declaredSources: declaredSources,
                matchingResourceRuleHandler: {
                    handledResources.append($0)
                },
                observabilityScope: observabilityScope
            )
        }

        let additionalResources: [Resource]
        if toolsVersion >= .v6_0 {
            additionalResources = declaredResources.compactMap { resource in
                if handledResources.contains(resource.path) {
                    return nil
                } else {
                    print("Found unhandled resource at \(resource.path)")
                    return self.resource(for: resource.path, with: .init(resource.rule))
                }
            }
        } else {
            additionalResources = []
        }

        let headers = pathToRule.lazy.filter { $0.value == .header }.map { $0.key }.sorted()
        let compilePaths = pathToRule.lazy.filter { $0.value == .compile }.map { $0.key }
        let sources = Sources(paths: Array(compilePaths).sorted(), root: targetPath)
        let resources: [Resource] = (pathToRule.compactMap { resource(for: $0.key, with: $0.value) } + additionalResources).sorted { a, b in
            a.path.pathString < b.path.pathString
        }
        let ignored = pathToRule.filter { $0.value == .ignored }.map { $0.key }.sorted()
        let others = pathToRule.filter { $0.value == .none }.map { $0.key }.sorted()

        try diagnoseConflictingResources(in: resources)
        diagnoseCopyConflictsWithLocalizationDirectories(in: resources)
        diagnoseLocalizedAndUnlocalizedVariants(in: resources)
        try diagnoseInfoPlistConflicts(in: resources)
        diagnoseInvalidResource(in: target.resources)

        // It's an error to contain mixed language source files.
        if sources.containsMixedLanguage {
            throw Module.Error.mixedSources(targetPath)
        }

        return (sources, resources, headers, ignored, others)
    }

    /// Compute the rule for the given path.
    private static func computeRule(for path: AbsolutePath,
                                    toolsVersion: ToolsVersion,
                                    additionalFileRules: [FileRuleDescription],
                                    observabilityScope: ObservabilityScope) -> FileRuleDescription.Rule {
        let rules = Self.rules(additionalFileRules: additionalFileRules, toolsVersion: toolsVersion)
        // For now, we are not passing in any declared resources or sources here and instead handle any generated files automatically at the callsite. Eventually, we will want the ability to declare opinions for generated files in the manifest as well.
        return Self.computeRule(for: path, toolsVersion: toolsVersion, rules: rules, declaredResources: [], declaredSources: nil, observabilityScope: observabilityScope)
    }

    private static func computeRule(
        for path: AbsolutePath, 
        toolsVersion: ToolsVersion,
        rules: [FileRuleDescription],
        declaredResources: [(path: AbsolutePath, rule: TargetDescription.Resource.Rule)],
        declaredSources: [AbsolutePath]?,
        matchingResourceRuleHandler: (AbsolutePath) -> () = { _ in },
        observabilityScope: ObservabilityScope
    ) -> FileRuleDescription.Rule {
        var matchedRule: FileRuleDescription.Rule = .none

        // First match any resources explicitly declared in the manifest file.
        for declaredResource in declaredResources {
            let resourcePath = declaredResource.path
            if path.isDescendantOfOrEqual(to: resourcePath) {
                if matchedRule != .none {
                    observabilityScope.emit(error: "duplicate resource rule '\(declaredResource.rule)' found for file at '\(path)'")
                }
                matchedRule = .init(declaredResource.rule)
                matchingResourceRuleHandler(declaredResource.path)
            }
        }

        // Match any sources explicitly declared in the manifest file.
        if let declaredSources {
            for sourcePath in declaredSources {
                if path.isDescendantOfOrEqual(to: sourcePath) {
                    if matchedRule != .none {
                        observabilityScope.emit(error: "duplicate rule found for file at '\(path)'")
                    }

                    // Check for header files as they're allowed to be mixed with sources.
                    if let ext = path.extension,
                      FileRuleDescription.header.fileTypes.contains(ext) {
                        matchedRule = .header
                    } else if toolsVersion >= .v5_3 {
                        matchedRule = .compile
                    } else if let ext = path.extension,
                      SupportedLanguageExtension.validExtensions(toolsVersion: toolsVersion).contains(ext) {
                        matchedRule = .compile
                    }
                    // The source file might have been declared twice so
                    // exit on first match.
                    // FIXME: We should emitting warnings for duplicate// declarations.
                    break
                }
            }
        }

        // We haven't found a rule using that's explicitly declared in the manifest
        // so try doing an automatic match.
        if matchedRule == .none {
            let effectiveRules: [FileRuleDescription] = {
                // Don't automatically match compile rules if target's sources are
                // explicitly declared in the package manifest.
                if declaredSources != nil {
                    return rules.filter { $0.rule != .compile }
                }
                return rules
            }()

            if let needle = effectiveRules.first(where: { $0.match(path: path, toolsVersion: toolsVersion) }) {
                matchedRule = needle.rule
            } else if path.parentDirectory.extension == Resource.localizationDirectoryExtension {
                matchedRule = .processResource(localization: .none)
            }
        }

        return matchedRule
    }

    /// Returns the `Resource` file associated with a file and a particular rule, if there is one.
    private static func resource(for path: AbsolutePath, with rule: FileRuleDescription.Rule, defaultLocalization: String?, targetName: String, targetPath: AbsolutePath, observabilityScope: ObservabilityScope) -> Resource? {
        switch rule {
        case .compile, .header, .none, .modulemap, .ignored:
            return nil
        case .processResource:
            let implicitLocalization: String? = {
                if path.parentDirectory.extension == Resource.localizationDirectoryExtension {
                    return path.parentDirectory.basenameWithoutExt
                } else {
                    return nil
                }
            }()

            let explicitLocalization: String? = {
                switch rule  {
                case .processResource(localization: .default):
                    return defaultLocalization ?? "en"
                case .processResource(localization: .base):
                    return "Base"
                default:
                    return .none
                }
            }()

            // If a resource is both inside a localization directory and has an explicit localization, it's ambiguous.
            guard implicitLocalization == nil || explicitLocalization == nil else {
                let relativePath = path.relative(to: targetPath)
                observabilityScope.emit(.localizationAmbiguity(path: relativePath, targetName: targetName))
                return nil
            }

            return Resource(rule: .process(localization: implicitLocalization ?? explicitLocalization), path: path)
        case .copyResource:
            return Resource(rule: .copy, path: path)
        case .embedResourceInCode:
            return Resource(rule: .embedInCode, path: path)
        }
    }

    private func resource(for path: AbsolutePath, with rule: FileRuleDescription.Rule) -> Resource? {
        return Self.resource(for: path, with: rule, defaultLocalization: defaultLocalization, targetName: target.name, targetPath: targetPath, observabilityScope: observabilityScope)
    }

    private func diagnoseConflictingResources(in resources: [Resource]) throws {
        let duplicateResources = resources.spm_findDuplicateElements(by: \.destinationForGrouping)
        for resources in duplicateResources {
            try self.observabilityScope.emit(.conflictingResource(path: resources[0].destination, targetName: target.name))

            for resource in resources {
                let relativePath = resource.path.relative(to: targetPath)
                self.observabilityScope.emit(.fileReference(path: relativePath))
            }
        }
    }

    private func diagnoseCopyConflictsWithLocalizationDirectories(in resources: [Resource]) {
        let localizationDirectories = Set(resources
            .lazy
            .compactMap({ $0.localization })
            .map({ "\($0).\(Resource.localizationDirectoryExtension)" }))

        for resource in resources where resource.rule == .copy {
            if localizationDirectories.contains(resource.path.basename.lowercased()) {
                let relativePath = resource.path.relative(to: targetPath)
                self.observabilityScope.emit(.copyConflictWithLocalizationDirectory(path: relativePath, targetName: target.name))
            }
        }
    }

    private func diagnoseLocalizedAndUnlocalizedVariants(in resources: [Resource]) {
        let resourcesByBasename = Dictionary(grouping: resources, by: { $0.path.basename })
        for (basename, resources) in resourcesByBasename {
            let hasLocalizations = resources.contains(where: { $0.localization != nil })
            let hasUnlocalized = resources.contains(where: { $0.localization == nil })
            if hasLocalizations && hasUnlocalized {
                self.observabilityScope.emit(.localizedAndUnlocalizedVariants(resource: basename, targetName: target.name))
            }
        }
    }

    private func diagnoseInfoPlistConflicts(in resources: [Resource]) throws {
        for resource in resources {
            if try resource.destination == RelativePath(validating: "Info.plist") {
                self.observabilityScope.emit(.infoPlistResourceConflict(
                    path: resource.path.relative(to: targetPath),
                    targetName: target.name))
            }
        }
    }

    private func diagnoseInvalidResource(in resources: [TargetDescription.Resource]) {
        resources.forEach { resource in
            guard let resourcePath = try? AbsolutePath(validating: resource.path, relativeTo: self.targetPath) else {
                return
            }
            if let message = validTargetPath(at: resourcePath), self.packageKind.emitAuthorWarnings {
                let warning = "Invalid Resource '\(resource.path)': \(message)."
                self.observabilityScope.emit(warning: warning)
            }
        }
    }

    /// Returns true if the given path is a declared source.
    func isDeclaredSource(_ path: AbsolutePath) -> Bool {
        return path == targetPath || declaredSources?.contains(path) == true
    }

    /// Compute the contents of the files in a target.
    ///
    /// This avoids recursing into certain directories like exclude or the
    /// ones that should be copied as-is.
    public func computeContents() -> [AbsolutePath] {
        var contents: [AbsolutePath] = []
        var queue: [AbsolutePath] = [targetPath]

        // Ignore xcodeproj and playground directories.
        var ignoredDirectoryExtensions = ["xcodeproj", "playground", "xcworkspace"]

        // Ignore localization directories if not supported.
        if toolsVersion < .v5_3 {
            ignoredDirectoryExtensions.append(Resource.localizationDirectoryExtension)
        }

        while let path = queue.popLast() {
            // Ignore dot files.
            if path.basename.hasPrefix(".") { continue }

            if let ext = path.extension, ignoredDirectoryExtensions.contains(ext) {
                continue
            }

            // Ignore manifest files.
            if path.parentDirectory == self.packagePath {
                if path.basename == Manifest.filename { continue }
                if path.basename == "Package.resolved" { continue }

                // Ignore version-specific manifest files.
                if path.basename.hasPrefix(Manifest.basename + "@") && path.extension == "swift" {
                    continue
                }
            }

            // Ignore if this is an excluded path.
            if self.excludedPaths.contains(path) { continue }

            if self.fileSystem.isSymlink(path) && !self.fileSystem.exists(path, followSymlink: true) {
                self.observabilityScope.emit(.brokenSymlink(path))
                continue
            }

            // Consider non-directories as source files.
            if !self.fileSystem.isDirectory(path) {
                contents.append(path)
                continue
            }

            // At this point, path can only be a directory.
            //
            // Starting tools version with resources, treat directories of known extension as resources
            // ie, do not include their content, and instead treat the directory itself as the content
            if toolsVersion >= .v5_6 {
                if let directoryExtension = path.extension,
                   self.opaqueDirectoriesExtensions.contains(directoryExtension),
                   directoryExtension != Resource.localizationDirectoryExtension,
                   !isDeclaredSource(path)
                {
                    contents.append(path)
                    continue
                }
            } else if toolsVersion >= .v5_3 {
                // maintain the broken behavior prior to fixing it in 5.6
                // see rdar://82933763
                if let directoryExtension = path.extension,
                   directoryExtension != Resource.localizationDirectoryExtension,
                   !isDeclaredSource(path)
                {
                    contents.append(path)
                    continue
                }
            }

            // Check if the directory is marked to be copied.
            let directoryMarkedToBeCopied = target.resources.contains{ resource in
                let resourcePath = try? AbsolutePath(validating: resource.path, relativeTo: self.targetPath)
                if resource.rule == .copy && resourcePath == path {
                    return true
                }
                return false
            }

            // If the directory is marked to be copied, don't look inside it.
            if directoryMarkedToBeCopied {
                contents.append(path)
                continue
            }

            // We found a directory inside a localization directory, which is forbidden.
            if path.parentDirectory.extension == Resource.localizationDirectoryExtension {
                let relativePath = path.parentDirectory.relative(to: targetPath)
                self.observabilityScope.emit(.localizationDirectoryContainsSubDirectories(
                    localizationDirectory: relativePath,
                    targetName: target.name))
                continue
            }

            // Otherwise, add its content to the queue.
            let dirContents = self.observabilityScope.trap {
                try self.fileSystem.getDirectoryContents(path).map({ path.appending(component: $0) })
            }
            queue += dirContents ?? []
        }

        return contents
    }

    public static func computeContents(for generatedFiles: [AbsolutePath], toolsVersion: ToolsVersion, additionalFileRules: [FileRuleDescription], defaultLocalization: String?, targetName: String, targetPath: AbsolutePath, observabilityScope: ObservabilityScope) -> (sources: [AbsolutePath], resources: [Resource]) {
        var sources = [AbsolutePath]()
        var resources = [Resource]()

        generatedFiles.forEach { absPath in
            // 5.6 handled treated all generated files as sources.
            if toolsVersion <= .v5_6 {
                sources.append(absPath)
                return
            }

            var rule = Self.computeRule(for: absPath, toolsVersion: toolsVersion, additionalFileRules: additionalFileRules, observabilityScope: observabilityScope)

            // If we did not find a rule for a generated file, we treat it as to be processed for now. Eventually, we should handle generated files the same as other files and require explicit handling in the manifest for unknown types.
            if rule == .none {
                rule = .processResource(localization: .none)
            }

            switch rule {
            case .compile:
                if absPath.extension == "swift" {
                    sources.append(absPath)
                } else {
                    observabilityScope.emit(warning: "Only Swift is supported for generated plugin source files at this time: \(absPath)")
                }
            case .copyResource, .processResource, .embedResourceInCode:
                if let resource = Self.resource(for: absPath, with: rule, defaultLocalization: defaultLocalization, targetName: targetName, targetPath: targetPath, observabilityScope: observabilityScope) {
                    resources.append(resource)
                } else {
                    // If this is reached, `TargetSourcesBuilder` already emitted a diagnostic, so we can ignore this case here.
                }
            case .header:
                observabilityScope.emit(warning: "Headers generated by plugins are not supported at this time: \(absPath)")
            case .modulemap:
                observabilityScope.emit(warning: "Module maps generated by plugins are not supported at this time: \(absPath)")
            case .ignored, .none:
                break
            }
        }

        return (sources, resources)
    }
}

/// Describes a rule for including a source or resource file in a target.
public struct FileRuleDescription: Sendable {
    /// A rule semantically describes a file/directory in a target.
    ///
    /// It is up to the build system to translate a rule into a build command.
    public enum Rule: Sendable, Equatable {
        /// The compile rule for `sources` in a package.
        case compile

        /// Process resource file rule for any type of platform-specific processing.
        ///
        /// This defaults to copy if there's no specialized behavior.
        case processResource(localization: TargetDescription.Resource.Localization?)

        /// The embed rule.
        case embedResourceInCode

        /// The copy rule.
        case copyResource

        /// The modulemap rule.
        case modulemap

        /// A header file.
        case header

        /// Indicates that the file should be treated as ignored, without causing an unhandled-file warning.
        case ignored

        /// Sentinel to indicate that no rule was chosen for a given file.
        case none
    }

    /// The rule associated with this description.
    public let rule: Rule

    /// The tools version supported by this rule.
    public let toolsVersion: ToolsVersion

    /// The list of file extensions support by this rule.
    ///
    /// No two rule can have the same file extension.
    public let fileTypes: Set<String>

    public init(rule: Rule, toolsVersion: ToolsVersion, fileTypes: Set<String>) {
        self.rule = rule
        self.toolsVersion = toolsVersion
        self.fileTypes = fileTypes
    }

    /// Match the given path to the rule.
    public func match(path: AbsolutePath, toolsVersion: ToolsVersion) -> Bool {
        if toolsVersion < self.toolsVersion {
            return false
        }

        if let ext = path.extension {
            return self.fileTypes.contains(ext)
        }
        return false
    }

    /// The swift compiler rule.
    public static let swift: FileRuleDescription = {
        .init(
            rule: .compile,
            toolsVersion: .minimumRequired,
            fileTypes: ["swift"]
        )
    }()

    /// The clang compiler rule.
    public static let clang: FileRuleDescription = {
        .init(
            rule: .compile,
            toolsVersion: .minimumRequired,
            fileTypes: ["c", "m", "mm", "cc", "cpp", "cxx"]
        )
    }()

    /// The rule for compiling asm files.
    public static let asm: FileRuleDescription = {
        .init(
            rule: .compile,
            toolsVersion: .v5,
            fileTypes: ["s", "S"]
        )
    }()

    /// The rule for detecting modulemap files.
    public static let modulemap: FileRuleDescription = {
        .init(
            rule: .modulemap,
            toolsVersion: .minimumRequired,
            fileTypes: ["modulemap"]
        )
    }()

    /// The rule for detecting header files.
    public static let header: FileRuleDescription = {
        .init(
            rule: .header,
            toolsVersion: .minimumRequired,
            fileTypes: ["h", "hh", "hpp", "h++", "hp", "hxx", "H", "ipp", "def"]
        )
    }()

    /// File types related to the interface builder and storyboards.
    public static let xib: FileRuleDescription = {
        .init(
            rule: .processResource(localization: .none),
            toolsVersion: .v5_3,
            fileTypes: ["nib", "xib", "storyboard"]
        )
    }()

    /// File types related to the asset catalog.
    public static let assetCatalog: FileRuleDescription = {
        .init(
            rule: .processResource(localization: .none),
            toolsVersion: .v5_3,
            fileTypes: ["xcassets"]
        )
    }()

    /// File types related to the string catalog.
    public static let stringCatalog: FileRuleDescription = {
        .init(
            rule: .processResource(localization: .none),
            toolsVersion: .v5_9,
            fileTypes: ["xcstrings"]
        )
    }()

    /// File types related to the CoreData.
    public static let coredata: FileRuleDescription = {
        .init(
            rule: .processResource(localization: .none),
            toolsVersion: .v5_3,
            fileTypes: ["xcdatamodeld", "xcdatamodel", "xcmappingmodel"]
        )
    }()

    /// File types related to Metal.
    public static let metal: FileRuleDescription = {
        .init(
            rule: .processResource(localization: .none),
            toolsVersion: .v5_3,
            fileTypes: ["metal"]
        )
    }()

    /// File rule to ignore .docc (in the SwiftPM build system).
    public static let docc: FileRuleDescription = {
        .init(
            rule: .ignored,
            toolsVersion: .v5_5,
            fileTypes: ["docc"]
        )
    }()

    /// File rule to copy `.xcprivacy` (in the Xcode build system).
    public static let xcprivacyCopied: FileRuleDescription = {
        .init(
            rule: .copyResource,
            toolsVersion: .v6_0,
            fileTypes: ["xcprivacy"]
        )
    }()

    /// File rule to ignore `.xcprivacy` (in the SwiftPM build system).
    public static let xcprivacyIgnored: FileRuleDescription = {
        .init(
            rule: .ignored,
            toolsVersion: .v6_0,
            fileTypes: ["xcprivacy"]
        )
    }()

    /// List of all the builtin rules.
    public static let builtinRules: [FileRuleDescription] = [
        swift,
        clang,
        asm,
        modulemap,
        header,
    ]

    /// List of file types that requires the Xcode build system.
    public static let xcbuildFileTypes: [FileRuleDescription] = [
        xib,
        assetCatalog,
        stringCatalog,
        coredata,
        metal,
        xcprivacyCopied,
    ]

    /// List of file types that apply just to the SwiftPM build system.
    public static let swiftpmFileTypes: [FileRuleDescription] = [
        docc,
        xcprivacyIgnored,
    ]

    /// List of file directory extensions that should be treated as opaque, non source, directories.
    public static var opaqueDirectoriesExtensions: Set<String> {
        let types = Self.xcbuildFileTypes + Self.swiftpmFileTypes
        return types.reduce(into: Set<String>(), { partial, item in
            partial.formUnion(item.fileTypes)
        })
    }
}

extension FileRuleDescription.Rule {
    init(_ seed: TargetDescription.Resource.Rule)  {
        switch seed {
        case .process(let localization):
            self = .processResource(localization: localization)
        case .copy:
            self = .copyResource
        case .embedInCode:
            self = .embedResourceInCode
        }
    }
}

extension Resource {
    var localization: String? {
        switch self.rule {
        case .process(let localization):
            return localization
        default:
            return .none
        }
    }
}

extension Basics.Diagnostic {
    static func symlinkInSources(symlink: RelativePath, targetName: String) -> Self {
        .warning("ignoring symlink at '\(symlink)' in target '\(targetName)'")
    }

    static func localizationDirectoryContainsSubDirectories(
        localizationDirectory: RelativePath,
        targetName: String
    ) -> Self {
        .error("localization directory '\(localizationDirectory)' in target '\(targetName)' contains sub-directories, which is forbidden")
    }
}

extension ObservabilityMetadata {
    public var moduleName: String? {
        get {
            self[ModuleNameKey.self]
        }
        set {
            self[ModuleNameKey.self] = newValue
        }
    }

    enum ModuleNameKey: Key {
        typealias Value = String
    }
}

extension PackageReference.Kind {
    fileprivate var emitAuthorWarnings: Bool {
        switch self {
        case .remoteSourceControl, .registry:
            return false
        default:
            return true
        }
    }
}

extension PackageModel.Resource {
    fileprivate var destinationForGrouping: RelativePath? {
        do {
            return try self.destination
        } catch {
            return .none
        }
    }
}