bazelbuild / rules_swift

Bazel rules to build Swift on Apple and Linux platforms
Apache License 2.0
313 stars 138 forks source link

Error adding "apple_core_data_model" dependency to "swift_library" #1160

Closed cs2be closed 10 months ago

cs2be commented 10 months ago

Hi, I'm trying to add core data build dependencies to my swift project, however I got an error when trying to run the build rule:

apple_core_data_model(
   name = "coreDataModel",
   srcs = ["//Resources:CoreDataModel.xcdatamodeld"]
)

swift_library(
    name = "lib",
    srcs = glob(["Sources/**/*.swift"]),
    deps = [
        ":coreDataModel"
    ]
)

Running "bazel build //:lib", I get the following errors:

ChatGPTDemoiOS cs2be$ bazel build //:lib
ERROR: /Users/cs2be/programming/chatgpt_demo/ChatGPTDemoiOS/BUILD:24:14: in deps attribute of swift_library rule //:lib: '//:coreDataModel' does not have mandatory providers: 'CcInfo' or 'SwiftInfo' or 'objc'
ERROR: /Users/cs2be/programming/chatgpt_demo/ChatGPTDemoiOS/BUILD:24:14: Analysis of target '//:lib' failed
ERROR: Analysis of target '//:lib' failed; build aborted
INFO: Elapsed time: 0.092s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

I suspect the build rule apple_core_data_model is missing SwiftInfo dictionary. The swift_library code that pulls SwiftInfo from deps is here.

I'm using these versions of rules_apple and rules_swift:

bazel_dep(name = "rules_apple", version = "3.2.1")
bazel_dep(name = "rules_swift", version = "1.15.1")

Thanks!

keith commented 10 months ago

You want to put :coreDataModel in the srcs attribute instead, it doesn't produce a library it only generates source files

cs2be commented 10 months ago

Thank you Keith, that worked and the core data swift files are now part of the project.

apple_core_data_model(
   name = "coreDataModel",
   srcs = ["//Resources:CoreDataModel.xcdatamodeld"]
)

swift_library(
    name = "lib",
    srcs = glob(["Sources/**/*.swift"]) + [":coreDataModel"],
    data = ["//Resources:CoreDataModel.xcdatamodeld"]
)

ios_application(
    name = "ChatGPTDemoApp",
    bundle_id = "com.midoriapple.ChatGPTDemoApp",
    families = [
        "iphone",
        "ipad",
    ],
    infoplists = ["Info.plist"],
    launch_storyboard = "//Resources:Launch.storyboard",
    minimum_os_version = "15.0",
    visibility = ["//visibility:public"],
    deps = [":lib"],
)

However, it seems there were some compile problems for those generated core data swift classes, wonder if you've seen this issue before? Thanks for your help!

cs2be$ bazel build //:ChatGPTDemoApp
INFO: Analyzed target //:ChatGPTDemoApp (1 packages loaded, 15 targets configured).
ERROR: /Users/cs2be/programming/chatgpt_demo/ChatGPTDemoiOS/BUILD:24:14: Compiling Swift module //:lib failed: (Exit 1): worker failed: error executing SwiftCompile command (from target //:lib) bazel-out/darwin_arm64-opt-exec-ST-13d3ddad9198/bin/external/rules_swift~1.13.0/tools/worker/worker swiftc ... (remaining 1 argument skipped)
remark: Incremental compilation has been disabled: ChatThread+CoreDataClass.swift has no swiftDeps file
remark: Incremental compilation has been disabled: ChatThread+CoreDataClass.swift has no swiftDeps file
remark: Incremental compilation has been disabled: malformed dependencies file 'none?!'
remark: Incremental compilation has been disabled: ChatThread+CoreDataProperties.swift has no swiftDeps file
remark: Incremental compilation has been disabled: ChatThread+CoreDataProperties.swift has no swiftDeps file
remark: Incremental compilation has been disabled: malformed dependencies file 'none?!'
remark: Incremental compilation has been disabled: CoreDataModel+CoreDataModel.swift has no swiftDeps file
remark: Incremental compilation has been disabled: CoreDataModel+CoreDataModel.swift has no swiftDeps file
remark: Incremental compilation has been disabled: malformed dependencies file 'none?!'
swift_worker: Could not copy bazel-out/ios_sim_arm64-fastbuild-ios-sim_arm64-min15.0-applebin_ios-ST-52085178dc52/bin/_swift_incremental/lib_objs/coredatamodel.coreDataModel.coredata.sources.o to bazel-out/ios_sim_arm64-fastbuild-ios-sim_arm64-min15.0-applebin_ios-ST-52085178dc52/bin/lib_objs/coredatamodel.coreDataModel.coredata.sources.o (No such file or directory)
Target //:ChatGPTDemoApp failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.061s, Critical Path: 0.94s
INFO: 2 processes: 2 internal.
ERROR: Build did NOT complete successfully
keith commented 10 months ago

Interesting, I haven't seen this one, if you pass --swiftcopt=-wmo does it fix it?

cs2be commented 10 months ago

Unfortunately no :(. I'll dig into this a little further, thanks for helping

cs2be$ bazel build //:ChatGPTDemoApp --swiftcopt=-wmo --verbose_failures
INFO: Analyzed target //:ChatGPTDemoApp (0 packages loaded, 0 targets configured).
ERROR: /Users/cs2be/programming/chatgpt_demo/ChatGPTDemoiOS/BUILD:24:14: output 'lib_objs/coredatamodel.coreDataModel.coredata.sources.o' was not created
ERROR: /Users/cs2be/programming/chatgpt_demo/ChatGPTDemoiOS/BUILD:24:14: Compiling Swift module //:lib failed: not all outputs were created or valid
Target //:ChatGPTDemoApp failed to build
INFO: Elapsed time: 0.605s, Critical Path: 0.50s
INFO: 2 processes: 1 internal, 1 worker.
ERROR: Build did NOT complete successfully
keith commented 10 months ago

it seems like this must be a bug in the rules, if you provide a repro case someone might be able to debug further

cs2be commented 10 months ago

That would be awesome. Here's how to reproduce:

git clone git@github.com:cs2be/chatgpt_demo.git
cd chatgpt_demo/ChatGPTDemoiOS
bazel build //:ChatGPTDemoApp --verbose_failures --subcommands --verbose_explanations

This should produce the error, thanks again!

keith commented 10 months ago

Thanks for the repro, that is very helpful! Turns out the problem is the same as this: https://github.com/bazelbuild/rules_swift/issues/969

keith commented 10 months ago

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

cs2be commented 10 months ago

Thanks Keith, I can confirm it works with those flags.

# Builds and runs on simulator
bazel run //:ChatGPTDemoApp --swiftcopt=-wmo --swiftcopt=-num-threads --swiftcopt=0 

However strange thing is the xcode project still doesn't compile :(. Possibly I need to pass another set of flags to this build? Should I close this issue as a duplicate? Thanks!

bazel run //:xcodeproj --swiftcopt=-wmo --swiftcopt=-num-threads --swiftcopt=0
Screenshot 2024-01-25 at 10 19 35 AM
keith commented 10 months ago

Yea I think you'd have to pipe those flags to rules_xcodeproj too, you probably should just add them to your .bazelrc.

xuzhaocheng commented 2 months ago

Hi @keith , I am facing the same issue when I use the latest version of rule_apple and rule_swift. The workaround --swiftcopt=-wmo --swiftcopt=-num-threads --swiftcopt=0 doesn't work anymore.

I forked @cs2be 's repo and created a new branch new to reproduce this issue. Could you please help to take a look? Thanks

git@github.com:xuzhaocheng/chatgpt_demo.git
cd chatgpt_demo
git checkout new
cd ChatGPTDemoiOS
bazel build //:ChatGPTDemoApp --swiftcopt=-wmo --swiftcopt=-num-threads --swiftcopt=0 
leohidalgo commented 2 months ago

The latest rule versions where it worked was:

swift: 1.18.0 apple: 3.16.0

xuzhaocheng commented 2 months ago

Do we have any plan to fix this issue on swift 2.x?

leohidalgo commented 2 months ago

@xuzhaocheng maybe you could create a new issue since this one is closed