rules_swift icon indicating copy to clipboard operation
rules_swift copied to clipboard

Unable to use swift_library with a directory

Open erikkerber opened this issue 2 years ago • 7 comments

When using a directory in the srcs of swift_library, the rules declare an incorrect intermediate object file for the directory and the build fails:

swift_worker: Could not copy bazel-out/darwin_arm64-fastbuild/bin/_swift_incremental/d_objs/c.o to bazel-out/darwin_arm64-fastbuild/bin/d_objs/c.o (No such file or directory)

NOTE: Where the failure happens is less important than there always is a <directory>.o object file declared

This works using objc_library:

Repro (credit @thii)
# rule.bzl
def _impl(ctx):
    dir_name = ctx.attr.name
    out = ctx.actions.declare_directory(dir_name)
    ctx.actions.run_shell(
        outputs = [out],
        command = """
            cd {}
            mkdir {}
            touch {}/File{}
        """.format(
            ctx.bin_dir.path,
            dir_name,
            dir_name,
            ctx.attr.ext,
            dir_name,
            ctx.attr.ext,
        ),
    )
    return [DefaultInfo(files = depset([out]))]

my_rule = rule(
    _impl,
    attrs = {
        "ext": attr.string()
    },
)
# BUILD
load(":rule.bzl", "my_rule")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")

my_rule(
    name = "a",
    ext = ".m",
)
objc_library(
    name = "b",
    srcs = [":a"],
)

my_rule(
    name = "c",
    ext = ".swift",
)
swift_library(
    name = "d",
    srcs = [":c"],
)

erikkerber avatar Jan 10 '23 17:01 erikkerber

A quick hack workaround is to pass --swiftcopt=-wmo --swiftcopt=-num-threads --swiftcopt=0 but this might have other downsides

keith avatar Jan 25 '24 17:01 keith

https://github.com/bazelbuild/bazel/issues/21031

keith avatar Jan 25 '24 18:01 keith

@keith Think we should close this one as will-never-happen or think there's a maybe-world where this is possible?

erikkerber avatar Jan 26 '24 04:01 erikkerber

I'll close as a dup of that one, if that one ever moves we should re-evaluate if we need to make changes

keith avatar Jan 26 '24 16:01 keith

I'm re-opening because it seems like Bazel will not be able to support this within Starlark (analysis time) but if we can move this work to the execution phase to match rules like objc_library which do work with directory artifacts then thats something worth discussing.

luispadron avatar Sep 18 '24 18:09 luispadron

Updating to rules_swift 3.x.x I am seeing:

swift_worker: Could not copy bazel-out/ios_sim_arm64-fastbuild-ios-sim_arm64-min16.0-applebin_ios-ST-5cb69ec9a0ed/bin/_swift_incremental/platform/ios/app-swift/Sources.swiftsourceinfo to bazel-out/ios_sim_arm64-fastbuild-ios-sim_arm64-min16.0-applebin_ios-ST-5cb69ec9a0ed/bin/platform/ios/app-swift/Sources.swiftsourceinfo (File exists)

louwers avatar Jun 15 '25 19:06 louwers

https://github.com/bazelbuild/rules_swift/issues/1543

brentleyjones avatar Jun 16 '25 14:06 brentleyjones