Comments (3)
This is one place where the way the rules work at runtime fundamentally differs between Apple and Linux.
On Linux, it's a good idea to think of swift_{library,binary,test}
as operating in the same fashion as cc_{library,binary,test}
in terms of things like runfiles and data dependencies. Forget everything you know about using the rules on Apple platforms 🙂 The behavior of much of our Apple build logic is special-cased for historical reasons and because of the nature of Apple bundling, and we haven't settled to a stable state yet where we can eliminate all of those special cases.
For Linux, the similarities to cc_{library,binary,test}
mean that runtime data needed during the execution should be passed via the data
attribute, which causes the files to be symlinked into the runfiles directory of the binary when you bazel run
or bazel test
it. Then, the current working directory of the binary will be (a view of) your workspace root, and you can access those files using their workspace-relative path at runtime.
If you're using a {ios,macos,tvos}_unit_test
target instead of a swift_test
, then the Apple rules apply, because those rules create test bundles. You need to use resources
or structured_resources
and then load those resources from wherever they exist in your bundle at runtime.
This means that, at this time, if you want to support running a test on both Apple and Linux, you need to do some repetition of your resources in the resources/structured_resources
and data
attributes (and you can use select
functions to select based on target platform, as well) to have them work uniformly. You may also need to have some conditional compilation in your Swift code to find the resources at the correct locations at runtime.†
This situation should be improved when we eventually retire the Apple-specific resource attributes and have everything use data
across the board, but that work's still pending.
† One of my teammates mentioned that the Foundation.Bundle
APIs have also seen some love recently in Swift on Linux, and that you can use those to access resources that are in the same package or subpackages, even for unbundled command line binaries (the "bundle" that the API returns is the directory containing the binary). I haven't looked at it myself yet so I don't know which version of Swift those changes are in, but that could also be helpful in making the Swift code that loads such resources identical on both platforms.
from rules_swift.
Thanks for the super detailed response. Now that swift_test
supports macOS, do you have any thoughts on how / if resources should work with that?
Then, the current working directory of the binary will be (a view of) your workspace root, and you can access those files using their workspace-relative path at runtime.
It looks like right now the test bundle does have access to the runfiles directory in the path Target.test-runner.sh.runfiles/__main__/workspace/root/to/data
. Is that the behavior we want from this long term?
from rules_swift.
It looks like, unsurprisingly, you also can't write to this directory in case you write and delete files throughout the run of the tests.
from rules_swift.
Related Issues (20)
- Bug: Incremental compilation cache poisoning HOT 1
- It is not possible to use `@testable` when `compilation_mode=opt` HOT 3
- On Xcode 15, binaries depending on swift_proto_library fail to link with error: duplicate -rpath '/usr/lib/swift' HOT 6
- How to build shared libraries using rule_swift (.so)? HOT 6
- Error in new_objc_provider: Key 'linkopt' no longer supported in ObjcProvider HOT 1
- Error: There is no configuration fragment named 'swift'. Available fragments: 'platform', 'coverage', 'bazel_py', 'android', 'bazel_android', 'apple', 'cpp', 'java', 'j2objc', 'objc', 'proto', 'py' HOT 1
- Duplicate symbol error when migrating to bzlmod HOT 4
- Error adding "apple_core_data_model" dependency to "swift_library" HOT 10
- CC_Library without a tag does not generate a ModuleMap HOT 6
- 'apple_cc_toolchain' is not defined in CI at Bazel@HEAD HOT 1
- Swift Macro - cc_wrapper.sh failed: error executing CppLink command HOT 6
- [Feature Request] Mixed language(C and Swift) swift_library support HOT 4
- Umbrella `objc_library` in `experimental_mixed_language_library` produces malformed `.swift.modulemap` HOT 3
- "Operation not permitted" when switching to `-swift.emit_swiftsourceinfo`, `swift.emit_swiftdoc`
- `swift.vfsoverlay` breaks `-strict-concurrency` checking HOT 4
- Add example of Swift <--> C++ interop HOT 3
- swift_rules_extra_dependencies() fails with rules_proto 6.0.0
- Add `@build_bazel_rules_swift//swift:defs.bzl` HOT 2
- Rename workspace name to `rules_swift` HOT 1
- Unable to compile Swift files with system-provided Swift Macro like #Preview when sandboxing is enabled
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rules_swift.