cgrindel / rules_spm

Provide a means for integrating external Swift packages built by Swift Package Manager into Bazel build using rules_swift.
Apache License 2.0
58 stars 13 forks source link

Unable to add dependency on `Stripe` #162

Open achew22 opened 2 years ago

achew22 commented 2 years ago

I'm unable to add a dependency on Stripe. I believe this is a reasonable way of loading it in (happy to be told I'm wrong on that front).

WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "cgrindel_rules_spm",
    sha256 = "03718eb865a100ba4449ebcbca6d97bf6ea78fa17346ce6d55532312e8bf9aa8",
    strip_prefix = "rules_spm-0.11.0",
    urls = [
        "http://github.com/cgrindel/rules_spm/archive/v0.11.0.tar.gz",
    ],
)

load(
    "@cgrindel_rules_spm//spm:deps.bzl",
    "spm_rules_dependencies",
)

spm_rules_dependencies()

load(
    "@build_bazel_rules_swift//swift:repositories.bzl",
    "swift_rules_dependencies",
)

swift_rules_dependencies()

load(
    "@build_bazel_rules_swift//swift:extras.bzl",
    "swift_rules_extra_dependencies",
)

swift_rules_extra_dependencies()

load("@cgrindel_rules_spm//spm:defs.bzl", "spm_pkg", "spm_repositories")

spm_repositories(
    name = "swift_pkgs",
    dependencies = [
        spm_pkg(
            "https://github.com/stripe/stripe-ios.git",
            exact_version = "22.6.0",
            products = ["Stripe"], # https://github.com/stripe/stripe-ios/blob/f5b0b5973aaa7ce6a2865e56f5942637cfef53f7/Package.swift#L11-L14
        ),
    ],
)

BUILD:

load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")

swift_binary(
    name = "simple",
    srcs = ["main.swift"],
    visibility = ["//swift:__subpackages__"],
    deps = [
      "@swift_pkgs//stripe:Stripe",
    ]
)

main.swift:

import Swift

print("Hello world")

Even when I try to do something relatively simple like query #swift_pkgs , not even running the binary, I get the following output:

$ bazel query @swift_pkgs//...
INFO: Repository swift_pkgs instantiated at:
  /Users/achew/Projects/rules_spm_stripe/WORKSPACE:35:17: in <toplevel>
Repository rule spm_repositories defined at:
  /private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl:595:35: in <toplevel>
ERROR: An error occurred during the fetch of repository 'swift_pkgs':
   Traceback (most recent call last):
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 593, column 30, in _spm_repositories_impl
                _configure_spm_repository(repository_ctx, pkgs, env)
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 524, column 49, in _configure_spm_repository
                product = pds.get_product(pkg_descs_dict[pkg_name], product_name)
Error: key "stripe-ios" not found in dictionary
ERROR: /Users/achew/Projects/rules_spm_stripe/WORKSPACE:35:17: fetching spm_repositories rule //external:swift_pkgs: Traceback (most recent call last):
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 593, column 30, in _spm_repositories_impl
                _configure_spm_repository(repository_ctx, pkgs, env)
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 524, column 49, in _configure_spm_repository
                product = pds.get_product(pkg_descs_dict[pkg_name], product_name)
Error: key "stripe-ios" not found in dictionary
ERROR: key "stripe-ios" not found in dictionary
Loading: 0 packages loaded

And bazel run //:simple has a similar error:

$ bazel run //:simple
INFO: Repository swift_pkgs instantiated at:
  /Users/achew/Projects/rules_spm_stripe/WORKSPACE:35:17: in <toplevel>
Repository rule spm_repositories defined at:
  /private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl:595:35: in <toplevel>
ERROR: An error occurred during the fetch of repository 'swift_pkgs':
   Traceback (most recent call last):
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 593, column 30, in _spm_repositories_impl
                _configure_spm_repository(repository_ctx, pkgs, env)
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 524, column 49, in _configure_spm_repository
                product = pds.get_product(pkg_descs_dict[pkg_name], product_name)
Error: key "stripe-ios" not found in dictionary
ERROR: /Users/achew/Projects/rules_spm_stripe/WORKSPACE:35:17: fetching spm_repositories rule //external:swift_pkgs: Traceback (most recent call last):
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 593, column 30, in _spm_repositories_impl
                _configure_spm_repository(repository_ctx, pkgs, env)
        File "/private/var/tmp/_bazel_achew/a0c57e4798e25f0c55e1618fdb2cda0e/external/cgrindel_rules_spm/spm/private/spm_repositories.bzl", line 524, column 49, in _configure_spm_repository
                product = pds.get_product(pkg_descs_dict[pkg_name], product_name)
Error: key "stripe-ios" not found in dictionary
ERROR: /Users/achew/Projects/rules_spm_stripe/BUILD:3:13: //:simple depends on @swift_pkgs//stripe-ios:Stripe in repository @swift_pkgs which failed to fetch. no such package '@swift_pkgs//stripe-ios': key "stripe-ios" not found in dictionary
ERROR: Analysis of target '//:simple' failed; build aborted: Analysis failed
INFO: Elapsed time: 3.222s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

I think there is a good chance that I'm holding it wrong, but I can't understand what I'm doing wrong here. Happy to provide any additional debug information you might want.

$ bazel version
Bazelisk version: development
Build label: 5.2.0
Build target: bazel-out/darwin_arm64-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Jun 7 16:11:09 2022 (1654618269)
Build timestamp: 1654618269
Build timestamp as int: 1654618269
cgrindel commented 2 years ago

Sorry for the delay in my response.

I think that you are running into the issue where the repository name does not match the package name. The Stripe Package.swift specifies the name of the package as Stripe. The spm_repositories rule needs to know the name of the package to set up the Bazel packages. By default, it uses the basename of the git repository. When the basename of the git repository does not match the name specified in the Package.swift, rules_spm can't find what it expects to find.

If you add the name attribute to the spm_pkg and update your BUILD file to point to @swift_pkgs//Stripe:Stripe, rules_spm should be able to fetch and start compiling the project.


spm_repositories(
    name = "swift_pkgs",
    dependencies = [
        spm_pkg(
            "https://github.com/stripe/stripe-ios.git",
            name = "Stripe",
            exact_version = "22.6.0",
            products = ["Stripe"],  # https://github.com/stripe/stripe-ios/blob/f5b0b5973aaa7ce6a2865e56f5942637cfef53f7/Package.swift#L11-L14
        ),
    ],
)

That seems to address the fetch issue. Unfortunately, I am seeing a compilation issue with the Stripe package. I'll poke at it for a minute to glean some information.

cgrindel commented 2 years ago

I cloned stripe/strip-ios and ran swift build. I am getting the following output:

❯ swift build
Building for debugging...
In file included from /Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/UIColor+ThirteenSupport.m:9:
/Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/UIColor+ThirteenSupport.h:9:9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
In file included from /Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/STDSTextFieldCustomization.m:9:
'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
In file included from /Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/UIViewController+Stripe3DS2.m:9:
: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
In file included from /Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/STDSTransaction.m:9:
/Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/include/STDSTransaction.h:10:9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
In file included from /Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/UIColor+DefaultColors.m:9:
/Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/UIColor+DefaultColors.h:9:9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
In file included from /Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/STDSThreeDS2Service.m:18:
In file included from /Users/chuck/code/stripe/stripe-ios/Stripe3DS2/Stripe3DS2/STDSDirectoryServerCertificate.h:13:
9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
1 error generated.
1 error generated.
[461/539] Compiling Stripe3DS2 UIColor+ThirteenSupport.m

Does building the Stripe package work for you?

achew22 commented 2 years ago

Huh, I get very similar errors.

 swift build
Building for debugging...
In file included from /Users/achew/Projects/stripe-ios/Stripe3DS2/Stripe3DS2/UIViewController+Stripe3DS2.m:9:
/Users/achew/Projects/stripe-ios/Stripe3DS2/Stripe3DS2/UIViewController+Stripe3DS2.h:9:9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
In file included from /Users/achew/Projects/stripe-ios/Stripe3DS2/Stripe3DS2/UIFont+DefaultFonts.m:9:
/Users/achew/Projects/stripe-ios/Stripe3DS2/Stripe3DS2/UIFont+DefaultFonts.h:9:9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
1 error generated.
[460/539] Compiling Stripe3DS2 UIViewController+Stripe3DS2.m

I wonder in what way I'm hodling it wrong... Let's ask https://github.com/stripe/stripe-ios/issues/1983

achew22 commented 2 years ago

Looking at the response from the stripe team, I think the issue is in rules_spm. I modified my import following your instructions but now I get the following output:

spm_repositories(
    name = "swift_pkgs",
    dependencies = [
        spm_pkg(
            url = "https://github.com/pointfreeco/swift-composable-architecture.git",
            exact_version = "0.38.3",
            products = ["ComposableArchitecture"],
        ),
        spm_pkg(
            "https://github.com/stripe/stripe-ios-spm.git",
            name = "Stripe",
            exact_version = "22.6.0",
            products = ["Stripe"],
        ),
        spm_pkg(
            "https://github.com/pointfreeco/combine-schedulers.git",
            exact_version = "0.6.0",
            products = ["CombineSchedulers"],
        ),
        spm_pkg(
            "https://github.com/square/Valet",
            exact_version = "4.1.2",
            products = ["Valet"],
        ),
        spm_pkg(
            "https://github.com/grpc/grpc-swift",
            exact_version = "1.7.3",
            products = ["GRPC"],
        ),
        spm_pkg(
            "https://github.com/apple/swift-nio-ssl.git",
            exact_version = "2.19.0",
            products = ["NIOSSL"],
        ),
    ],
    platforms = [
        ".iOS(.v14)",
        ".macOS(.v10_15)",
    ],
)

Note that I include the platform .iOS(.v14) which I would expect to bring in UIKit.

In file included from /private/var/tmp/_bazel_achew/86cdfd37d379cf1772409fa48c6ef35e/sandbox/darwin-sandbox/4139/execroot/__main__/bazel-out/ios-sim_arm64-min15.0-applebin_ios-ios_sim_arm64-fastbuild-ST-4e6c2a19403f/bin/external/swift_pkgs/spm_build/checkouts/stripe-ios-spm/Stripe3DS2/Stripe3DS2/UIViewController+Stripe3DS2.m:9:
/private/var/tmp/_bazel_achew/86cdfd37d379cf1772409fa48c6ef35e/sandbox/darwin-sandbox/4139/execroot/__main__/bazel-out/ios-sim_arm64-min15.0-applebin_ios-ios_sim_arm64-fastbuild-ST-4e6c2a19403f/bin/external/swift_pkgs/spm_build/checkouts/stripe-ios-spm/Stripe3DS2/Stripe3DS2/UIViewController+Stripe3DS2.h:9:9: fatal error: 'UIKit/UIKit.h' file not found
#import <UIKit/UIKit.h>
        ^~~~~~~~~~~~~~~
[323/851] Compiling Stripe3DS2 resource_bundle_accessor.m
clang: warning: using sysroot for 'MacOSX' but targeting 'iPhone' [-Wincompatible-sysroot]
1 error generated.
[323/851] Compiling Stripe3DS2 UIViewController+Stripe3DS2.m
[323/851] Compiling Stripe3DS2 UIView+LayoutSupport.m
[324/851] Compiling cipher.c
[325/851] Compiling aes_nohw.c
[326/851] Compiling aes128gcmsiv-x86_64.mac.x86_64.S
[327/851] Compiling printf.c
[328/851] Compiling socket.c
[329/851] Compiling pair.c
[330/851] Compiling hexdump.c
/private/var/tmp/_bazel_achew/86cdfd37d379cf1772409fa48c6ef35e/sandbox/darwin-sandbox/4139/execroot/__main__/bazel-out/ios-sim_arm64-min15.0-applebin_ios-ios_sim_arm64-fastbuild-ST-4e6c2a19403f/bin/external/swift_pkgs/spm_build/checkouts/Valet/Sources/Valet/SecureEnclave.swift:69:26: warning: 'kSecUseOperationPrompt' was deprecated in iOS 14.0: Use kSecUseAuthenticationContext and set LAContext.localizedReason property
            secItemQuery[kSecUseOperationPrompt as String] = userPrompt
                         ^
/private/var/tmp/_bazel_achew/86cdfd37d379cf1772409fa48c6ef35e/sandbox/darwin-sandbox/4139/execroot/__main__/bazel-out/ios-sim_arm64-min15.0-applebin_ios-ios_sim_arm64-fastbuild-ST-4e6c2a19403f/bin/external/swift_pkgs/spm_build/checkouts/Valet/Sources/Valet/SecureEnclave.swift:82:59: warning: 'kSecUseAuthenticationUIFail' was deprecated in iOS 14.0: Instead of kSecUseAuthenticationUI, use kSecUseAuthenticationContext and set LAContext.interactionNotAllowed property
        secItemQuery[kSecUseAuthenticationUI as String] = kSecUseAuthenticationUIFail
                                                          ^
/private/var/tmp/_bazel_achew/86cdfd37d379cf1772409fa48c6ef35e/sandbox/darwin-sandbox/4139/execroot/__main__/bazel-out/ios-sim_arm64-min15.0-applebin_ios-ios_sim_arm64-fastbuild-ST-4e6c2a19403f/bin/external/swift_pkgs/spm_build/checkouts/Valet/Sources/Valet/SecureEnclave.swift:118:26: warning: 'kSecUseOperationPrompt' was deprecated in iOS 14.0: Use kSecUseAuthenticationContext and set LAContext.localizedReason property
            secItemQuery[kSecUseOperationPrompt as String] = userPrompt
                         ^
/private/var/tmp/_bazel_achew/86cdfd37d379cf1772409fa48c6ef35e/sandbox/darwin-sandbox/4139/execroot/__main__/bazel-out/ios-sim_arm64-min15.0-applebin_ios-ios_sim_arm64-fastbuild-ST-4e6c2a19403f/bin/external/swift_pkgs/spm_build/checkouts/Valet/Sources/Valet/SinglePromptSecureEnclaveValet.swift:197:30: warning: 'kSecUseOperationPrompt' was deprecated in iOS 14.0: Use kSecUseAuthenticationContext and set LAContext.localizedReason property
                secItemQuery[kSecUseOperationPrompt as String] = userPrompt
                             ^

Do you have any theories @cgrindel ?

cgrindel commented 2 years ago

Were you able to clone their repo and run swift build? If so, did you have to specify some additional flags (e.g. --arch)? This is a limitation of the current rules_spm implementation. It does not handle compilation for other platforms very well. Alas, this is one of the issues that spurred me to gather feedback (#157).

johndpope commented 2 years ago

not sure it helps @achew22 - but uikit is specifically used on ios. appkit is for macos. if you have scripts that are importing - need to determine os

import <UIKit/UIKit.h>

https://stackoverflow.com/questions/68676690/macos-uikit-uikit-h-not-found

johndpope commented 2 years ago

UPDATE - I see someone is using bazel + stripe here

https://github.com/fakeNetflix/pinterest-repo-xchammer/blob/1475425b7ef0992984f74e348abbe5dc1c7ce850/sample/UrlGet/Vendor/Stripe/BUILD