MobileNativeFoundation / rules_xcodeproj

Bazel rules for generating Xcode projects.
MIT License
519 stars 82 forks source link

Bug: Error with direct dependency of a ios_application on incremental project generation. #2893

Closed narlei closed 7 months ago

narlei commented 7 months ago

Description

We can't declare a library that is directly dependent on the top_level_target AND the transitive dependencies on the xcschemes.scheme.run.build_targets.xcschemes.top_level_anchor_target.library_targets

To understand the problem, this is my graph:

MySampleApp (ios_application) -> MySampleSource (swift_library) -> [LibraryACore, LibraryAInterface] (swift_library)

I'm using:

run = xcschemes.run(
    build_targets = [
        xcschemes.top_level_anchor_target(
            label = "//:MySampleApp", # My top Level
            library_targets = [
                "//:MySampleSource", # MySampleApp imports directly only it
                "//Libraries/LibraryA:LibraryACore", # MySampleSource imports it
                "//Libraries/LibraryA:LibraryAInterface", # MySampleSource imports it
            ],
        ),
    ],
    # Comment the launch_target to simulate working without the app on focus targets
    launch_target = xcschemes.launch_target(
        "//:MySampleApp",
    ),
),

The error:

ERROR: Internal precondition failure:
tools/generators/xcschemes/src/Generator/CreateCustomSchemeInfos.swift:461: Run build target "@//:MySampleSource ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c" not found in:
[@//:MySampleApp ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryA:LibraryACore ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryA:LibraryAInterface ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryB:LibraryB ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryC:LibraryC ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryD:LibraryD ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c]

If I don't declare the //:MySampleSource on the library_targets it will work, creating the project. On Xcode it will show the sources (when focusing on //:MySampleSource). But the library //:MySampleSource will not show on the Targets list:

Screenshot 2024-02-07 at 14 58 42

The other way to make it work is moving all dependencies to the ios_application deps:

From:

MySampleApp (ios_application) -> MySampleSource (swift_library) -> [LibraryACore, LibraryAInterface] (swift_library)

To:

MySampleApp (ios_application) ->  [MySampleSource, LibraryACore, LibraryAInterface] (swift_library)

Then it will create the project and show all the libraries on the Targets list:

Screenshot 2024-02-07 at 15 01 08

The problem only happens when I have dependencies of a dependency on the libraries list, the direct dependency is not found on the list.

Reproduction steps

You can find the example to reproduce on: https://github.com/narlei/bazel_sample/tree/bug/application_direct_dependency

We're creating an ios_application with a dependency on MySampleSource:

ios_application(
    name = "MySampleApp",
    bundle_id = "com.narlei.sample",
    families = [
        "iphone",
        "ipad",
    ],
    infoplists = ["App/Info.plist"],
    minimum_os_version = "16.0",
    resources = [":PerformanceTestAppAssets"],
    version = ":BazelSampleVersion",
    visibility = ["//visibility:public"],
    deps = [
        ":MySampleSource",
    ],
)

The MySampleSource contains the app dependencies LibraryACore, LibraryAInterface:

swift_library(
    name = "MySampleSource",
    srcs = glob(["App/**/*.swift"]),
    module_name = "MySampleSource",
    tags = ["manual"],
    visibility = ["//visibility:public"],
    deps = [
        "//Libraries/LibraryA:LibraryACore",
        "//Libraries/LibraryA:LibraryAInterface",
    ],
)

My scheme:

xcschemes.scheme(
        name = "ProjectSample",
        profile = None,
        run = xcschemes.run(
            build_targets = [
                xcschemes.top_level_anchor_target(
                    label = "//:MySampleApp",
                    library_targets = [
                        "//:MySampleSource",
                        "//Libraries/LibraryA:LibraryACore",
                        "//Libraries/LibraryA:LibraryAInterface",
                    ],
                ),
            ],
            launch_target = xcschemes.launch_target(
                "//:MySampleApp",
            ),
        ),
 ),

The error:

ERROR: Internal precondition failure:
tools/generators/xcschemes/src/Generator/CreateCustomSchemeInfos.swift:461: Run build target "@//:MySampleSource ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c" not found in:
[@//:MySampleApp ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryA:LibraryACore ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryA:LibraryAInterface ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryB:LibraryB ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryC:LibraryC ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c, @//Libraries/LibraryD:LibraryD ios-sim_arm64-min16.0-applebin_ios-ios_sim_arm64-dbg-ST-21282182d87c]

The rules_xcodeproj isn't finding the MySampleSource on the target lists.

Expected behavior

I want to add all dependencies of my top_level that I want to create the scheme manually.

rules_xcodeproj version

1.16.0

Xcode version

15.0.0

Bazel version

6.4.0

rules_apple version

3.1.1

rules_swift version

1.13.0

Additional information

No response

brentleyjones commented 7 months ago

The issue is that //:MySampleSource was merged into //:MySampleApp, so there is no library target to put in the scheme.

We have a couple options:

I like the second option, since it works in both focused and unfocused cases, and you don't have to concern yourself with target merging.