Xcode 14.2 fails to compile tests if an app depends on a package with transitive Objective-C target (Missing required module 'xyz')

Originator:ncreated
Number:rdar://FB11875280 Date Originated:15th December 2022
Status:open Resolved:
Product:Xcode Product Version:14.2
Classification: Reproducible:always
 
Xcode 14.2 fails to compile tests (Missing required module 'xyz') if an app depends on a package with transitive Objective-C target.
It happens all the time when trying to compile unit tests.

The default `TEST_HOST` build setting set in Xcode 14.2 seems to be wrong and makes the test target fail to compile ("Missing required module 'xyz'") if the app target depends on SPM package that transitively links Objective-C target. This corresponds o typical vendor setup of offering Swift lib with Obj-c utility target.

According to our observation, the problem disappears when `/$(BUNDLE_EXECUTABLE_FOLDER_PATH)` is removed from the default `TEST_HOST` path set by Xcode 14.2:
```
$(BUILT_PRODUCTS_DIR)/X14App.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/X14App
```
Otherwise, the default TEST_HOST path resolves to include `../X14App.app//X14App` which makes the build process fail on `modulemap` lookup (as `-Xcc -fmodule-map-file=(...).modulemap"` is not added in `SwiftEmitModule` build step).

### Steps to reproduce manually:
1. Create SPM package that provides Swift library with internal Objective-C target (see sample structure + content in appendix).
2. Create new iOS app project with unit tests using Xcode 14.2.
3. Link SPM package to the app target (either locally or through git remote).
4. Build app target - it works.
5. Build unit tests target - it fails due to "<unknown>:0: error: missing required module 'objc_target'"

### Alternatively: 
1. Open the attached project (XC14App.xcworkspace)
2. Build unit tests target - it fails due to "<unknown>:0: error: missing required module 'objc_target'"

### Appendix

The structure of sample SPM package with minimal code examples sufficient to reproduce the problem manually:
```
spm-lib/
  Package.swift
  Sources/
    spm-lib/
       SwiftLib.swift
    objc-target/
       include/ObjcFoo.h
       ObjcFoo.m
```

```
// Package.swift
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
    name: "spm-lib",
    products: [
        .library(name: "spm-lib", targets: ["spm-lib"]),
    ],
    dependencies: [],
    targets: [
        .target(name: "spm-lib", dependencies: ["objc-target"]),
        .target(name: "objc-target"),
    ]
)
```

```
// ObjcFoo.h

#import <Foundation/Foundation.h>

@interface ObjcFoo : NSObject
-(NSString *)foo;
@end
```

```
// ObjcFoo.m

#import "ObjcFoo.h"

@implementation ObjcFoo
- (NSString*)foo {
    return @"objc foo";
}
@end
```

```
// SwiftLib.swift

import objc_target

public struct SwiftLib {
    public var text: String { ObjcFoo().foo() }
    public init() {}
}
```

Comments

Related thread on apple/swift-package-manager: https://github.com/apple/swift-package-manager/issues/4581


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!