Skip to content

Commit 2088b06

Browse files
authored
Add missing runfiles to pyo3 targets (#3917)
Relates to #3902 but I think it's still correct to collect all runfiles for the underlying library target.
1 parent d674bd6 commit 2088b06

7 files changed

Lines changed: 54 additions & 15 deletions

File tree

extensions/pyo3/private/pyo3.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _py_pyo3_library_impl(ctx):
153153
runfiles = ctx.runfiles(
154154
files = [ext],
155155
transitive_files = depset(files, transitive = [crate_info.data]),
156-
),
156+
).merge(ctx.attr.extension[DefaultInfo].default_runfiles),
157157
),
158158
PyInfo(
159159
imports = _get_imports(ctx, ctx.attr.imports),

extensions/pyo3/test/module_prefix_imports/bar.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ fn bar(m: &Bound<'_, PyModule>) -> PyResult<()> {
1010
m.add_function(wrap_pyfunction!(thing, m)?)?;
1111
Ok(())
1212
}
13-

extensions/pyo3/test/runfiles/BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
load("@rules_python//python:defs.bzl", "py_test")
2+
load("@rules_rust//rust:defs.bzl", "rust_library")
23
load("//:defs.bzl", "pyo3_extension")
34

5+
rust_library(
6+
name = "reader_helper",
7+
srcs = ["reader_helper.rs"],
8+
data = ["helper_data.txt"],
9+
edition = "2021",
10+
deps = ["@rules_rust//tools/runfiles"],
11+
)
12+
413
pyo3_extension(
514
name = "reader",
615
srcs = ["reader.rs"],
716
data = ["data.txt"],
817
edition = "2021",
9-
deps = ["@rules_rust//tools/runfiles"],
18+
deps = [
19+
":reader_helper",
20+
"@rules_rust//tools/runfiles",
21+
],
1022
)
1123

1224
py_test(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Transitive helper data from rust_library.

extensions/pyo3/test/runfiles/reader.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22
33
use pyo3::exceptions::PyFileNotFoundError;
44
use pyo3::prelude::*;
5+
use reader_helper::read_helper_data;
56
use runfiles::{rlocation, Runfiles};
67

7-
/// Formats the sum of two numbers as string.
8-
#[pyfunction]
9-
fn read_data() -> PyResult<String> {
10-
let r = Runfiles::create().unwrap();
11-
let path = rlocation!(r, "rules_rust_pyo3/test/runfiles/data.txt").unwrap();
12-
13-
std::fs::read_to_string(path).map_err(PyFileNotFoundError::new_err)
14-
}
15-
168
/// A Python module implemented in Rust. The name of this function must match
179
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
1810
/// import the module.
1911
#[pymodule]
20-
fn reader(m: &Bound<'_, PyModule>) -> PyResult<()> {
21-
m.add_function(wrap_pyfunction!(read_data, m)?)?;
12+
mod reader {
13+
14+
use super::*;
15+
16+
/// Formats the sum of two numbers as string.
17+
#[pyfunction]
18+
fn read_data() -> PyResult<String> {
19+
let r = Runfiles::create().unwrap();
20+
let path = rlocation!(r, "rules_rust_pyo3/test/runfiles/data.txt").unwrap();
21+
22+
std::fs::read_to_string(path).map_err(PyFileNotFoundError::new_err)
23+
}
2224

23-
Ok(())
25+
#[pyfunction]
26+
fn read_transitive_data() -> PyResult<String> {
27+
read_helper_data().map_err(PyFileNotFoundError::new_err)
28+
}
2429
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! A helper crate for loading transitive runfile data.
2+
3+
use runfiles::{rlocation, Runfiles};
4+
5+
pub fn read_helper_data() -> std::io::Result<String> {
6+
let runfiles = Runfiles::create().map_err(std::io::Error::other)?;
7+
let path =
8+
rlocation!(runfiles, "rules_rust_pyo3/test/runfiles/helper_data.txt").ok_or_else(|| {
9+
std::io::Error::new(
10+
std::io::ErrorKind::NotFound,
11+
"helper runfile path could not be resolved",
12+
)
13+
})?;
14+
std::fs::read_to_string(path)
15+
}

extensions/pyo3/test/runfiles/reader_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def test_transitive_runfiles_access(self) -> None:
5050
"La-Li-Lu-Le-Lo", data_file.read_text(encoding="utf-8").strip()
5151
)
5252

53+
def test_rust_library_data_via_pyo3(self) -> None:
54+
"""A test which reads transitive rust_library data through pyo3."""
55+
56+
result = reader.read_transitive_data()
57+
self.assertIsInstance(result, str)
58+
self.assertEqual("Transitive helper data from rust_library.", result.strip())
59+
5360

5461
if __name__ == "__main__":
5562
unittest.main()

0 commit comments

Comments
 (0)