-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a pure Swift runfiles library #1310
Open
cerisier
wants to merge
30
commits into
bazelbuild:master
Choose a base branch
from
cerisier:runfiles_library
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
4649c25
first commit
cerisier d8df061
workspace_name and not name
cerisier 9aca5fd
specify internal accessibility for constant enum
cerisier 19ad987
api fixes
cerisier 647bab2
add tests
cerisier 862e263
mutualize runfiles bzl
cerisier 25dc904
add examples
cerisier 0933c66
remove print
cerisier df79d83
add runfiles includes to swift-binary
cerisier ad99a68
Add documentation
cerisier d8b2ff7
add todo
cerisier 7c102f8
add proper throwable error enums
cerisier 6b269e4
format
cerisier 189a8fc
URL(filePath: does not exist in foundation for Linux
cerisier 589c964
Remove false test: argv0 is xctest without runfiles on macos but not …
cerisier ca0cd32
buildifier
cerisier 6c7f993
trigger
cerisier 030563f
Merge remote-tracking branch 'origin/master' into runfiles_library
cerisier 7383bb8
remove runfiles walk up the path discovery
cerisier 480e57c
fix wrong error name
cerisier 04751c6
missing new line at end of file
cerisier dfd1810
Merge remote-tracking branch 'origin/master' into runfiles_library
cerisier 8cacd1b
Merge remote-tracking branch 'origin/master' into runfiles_library
cerisier c16937f
Merge remote-tracking branch 'origin/master' into runfiles_library
cerisier 5e19b04
Deduce the source repository from #filePath
cerisier b5fae40
Implement runfiles directory detection from argv0
cerisier 39b8dfe
Update example
cerisier 0c5ce1e
fix tests
cerisier 9b426cf
Test runfiles path computation
cerisier 87ac141
Update swift/runfiles/README.md
cerisier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("//swift:swift.bzl", "swift_binary") | ||
|
||
swift_binary( | ||
name = "binary", | ||
srcs = ["main.swift"], | ||
data = [ | ||
"data/sample.txt", | ||
], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//swift/runfiles", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello runfiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import BazelRunfiles | ||
|
||
let runfiles = try Runfiles.create() | ||
|
||
// Runfiles lookup paths have the form `my_workspace/package/file`. | ||
// Runfiles path lookup may return nil. | ||
guard let runFile = runfiles.rlocation("build_bazel_rules_swift/examples/runfiles/data/sample.txt") else { | ||
fatalError("couldn't resolve runfile") | ||
} | ||
|
||
print(runFile) | ||
|
||
// Runfiles path lookup may return a non-existent path. | ||
let content = try String(contentsOf: runFile, encoding: .utf8) | ||
|
||
assert(content == "Hello runfiles") | ||
print(content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
load("//swift:swift_library.bzl", "swift_library") | ||
|
||
swift_library( | ||
name = "runfiles", | ||
srcs = [ | ||
"Runfiles.swift", | ||
], | ||
module_name = "BazelRunfiles", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Swift BazelRunfiles library | ||
|
||
This is a Bazel Runfiles lookup library for Bazel-built Swift binaries and tests. | ||
|
||
Learn about runfiles: read [Runfiles guide](https://bazel.build/extending/rules#runfiles) | ||
or watch [Fabian's BazelCon talk](https://www.youtube.com/watch?v=5NbgUMH1OGo). | ||
|
||
## Usage | ||
|
||
1. Depend on this runfiles library from your build rule: | ||
|
||
```python | ||
swift_binary( | ||
name = "my_binary", | ||
... | ||
data = ["//path/to/my/data.txt"], | ||
deps = ["@bazel_swift//swift/runfiles"], | ||
cerisier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
``` | ||
|
||
2. Include the runfiles library: | ||
|
||
```swift | ||
import BazelRunfiles | ||
``` | ||
|
||
3. Create a Runfiles instance and use `rlocation` to look up runfile urls: | ||
|
||
```swift | ||
import BazelRunfiles | ||
|
||
let runfiles = try? Runfiles.create(sourceRepository: BazelRunfilesConstants.currentRepository) | ||
|
||
let fileURL = runfiles?.rlocation("my_workspace/path/to/my/data.txt") | ||
``` | ||
|
||
> The code above creates a manifest- or directory-based implementation based on | ||
the environment variables in `Process.processInfo.environment`. | ||
See `Runfiles.create()` for more info. | ||
|
||
> The Runfiles.create function uses the runfiles manifest and the runfiles | ||
directory from the RUNFILES_MANIFEST_FILE and RUNFILES_DIR environment | ||
variables. If not present, the function looks for the manifest and directory | ||
near CommandLine.arguments.first (argv[0]), the path of the main program. | ||
|
||
> The BazelRunfilesConstants.currentRepository symbol is available in every | ||
target that depends on the runfiles library. | ||
|
||
If you want to start subprocesses, and the subprocess can't automatically | ||
cerisier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
find the correct runfiles directory, you can explicitly set the right | ||
environment variables for them: | ||
|
||
```swift | ||
import Foundation | ||
import BazelRunfiles | ||
|
||
let runfiles = try? Runfiles.create(sourceRepository: BazelRunfilesConstant.currentRepository) | ||
|
||
guard let executableURL = runfiles.rlocation("my_workspace/path/to/binary") else { | ||
return | ||
} | ||
|
||
let process = Process() | ||
process.executableURL = executableURL | ||
process.environment = runfiles.envVars() | ||
|
||
do { | ||
// Launch the process | ||
try process.run() | ||
process.waitUntilExit() | ||
} catch { | ||
// ... | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In which cases can it return
nil
, and should it throw instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns nil in 2 scenarios:
I can make it throw instead indeed.