Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit ebfeac7

Browse files
authored
Check if Rust lib needs rebuilding. (#13759)
This protects against the common mistake of failing to remember to rebuild Rust code after making changes.
1 parent 4c4889c commit ebfeac7

File tree

7 files changed

+149
-1
lines changed

7 files changed

+149
-1
lines changed

changelog.d/13759.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a check for editable installs if the Rust library needs rebuilding.

rust/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ name = "synapse.synapse_rust"
1919

2020
[dependencies]
2121
pyo3 = { version = "0.16.5", features = ["extension-module", "macros", "abi3", "abi3-py37"] }
22+
23+
[build-dependencies]
24+
blake2 = "0.10.4"
25+
hex = "0.4.3"

rust/build.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! This build script calculates the hash of all files in the `src/`
2+
//! directory and adds it as an environment variable during build time.
3+
//!
4+
//! This is used so that the python code can detect when the built native module
5+
//! does not match the source in-tree, helping to detect the case where the
6+
//! source has been updated but the library hasn't been rebuilt.
7+
8+
use std::path::PathBuf;
9+
10+
use blake2::{Blake2b512, Digest};
11+
12+
fn main() -> Result<(), std::io::Error> {
13+
let mut dirs = vec![PathBuf::from("src")];
14+
15+
let mut paths = Vec::new();
16+
while let Some(path) = dirs.pop() {
17+
let mut entries = std::fs::read_dir(path)?
18+
.map(|res| res.map(|e| e.path()))
19+
.collect::<Result<Vec<_>, std::io::Error>>()?;
20+
21+
entries.sort();
22+
23+
for entry in entries {
24+
if entry.is_dir() {
25+
dirs.push(entry)
26+
} else {
27+
paths.push(entry.to_str().expect("valid rust paths").to_string());
28+
}
29+
}
30+
}
31+
32+
paths.sort();
33+
34+
let mut hasher = Blake2b512::new();
35+
36+
for path in paths {
37+
let bytes = std::fs::read(path)?;
38+
hasher.update(bytes);
39+
}
40+
41+
let hex_digest = hex::encode(hasher.finalize());
42+
println!("cargo:rustc-env=SYNAPSE_RUST_DIGEST={hex_digest}");
43+
44+
Ok(())
45+
}

rust/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
use pyo3::prelude::*;
22

3+
/// Returns the hash of all the rust source files at the time it was compiled.
4+
///
5+
/// Used by python to detect if the rust library is outdated.
6+
#[pyfunction]
7+
fn get_rust_file_digest() -> &'static str {
8+
env!("SYNAPSE_RUST_DIGEST")
9+
}
10+
311
/// Formats the sum of two numbers as string.
412
#[pyfunction]
513
#[pyo3(text_signature = "(a, b, /)")]
@@ -11,6 +19,6 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
1119
#[pymodule]
1220
fn synapse_rust(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
1321
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
14-
22+
m.add_function(wrap_pyfunction!(get_rust_file_digest, m)?)?;
1523
Ok(())
1624
}

stubs/synapse/synapse_rust.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
def sum_as_string(a: int, b: int) -> str: ...
2+
def get_rust_file_digest() -> str: ...

synapse/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import os
2121
import sys
2222

23+
from synapse.util.rust import check_rust_lib_up_to_date
24+
2325
# Check that we're not running on an unsupported Python version.
2426
if sys.version_info < (3, 7):
2527
print("Synapse requires Python 3.7 or above.")
@@ -78,3 +80,6 @@
7880
from synapse.util.patch_inline_callbacks import do_patch
7981

8082
do_patch()
83+
84+
85+
check_rust_lib_up_to_date()

synapse/util/rust.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2022 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import sys
17+
from hashlib import blake2b
18+
19+
import synapse
20+
from synapse.synapse_rust import get_rust_file_digest
21+
22+
23+
def check_rust_lib_up_to_date() -> None:
24+
"""For editable installs check if the rust library is outdated and needs to
25+
be rebuilt.
26+
"""
27+
28+
if not _dist_is_editable():
29+
return
30+
31+
synapse_dir = os.path.dirname(synapse.__file__)
32+
synapse_root = os.path.abspath(os.path.join(synapse_dir, ".."))
33+
34+
# Double check we've not gone into site-packages...
35+
if os.path.basename(synapse_root) == "site-packages":
36+
return
37+
38+
# ... and it looks like the root of a python project.
39+
if not os.path.exists("pyproject.toml"):
40+
return
41+
42+
# Get the hash of all Rust source files
43+
hash = _hash_rust_files_in_directory(os.path.join(synapse_root, "rust", "src"))
44+
45+
if hash != get_rust_file_digest():
46+
raise Exception("Rust module outdated. Please rebuild using `poetry install`")
47+
48+
49+
def _hash_rust_files_in_directory(directory: str) -> str:
50+
"""Get the hash of all files in a directory (recursively)"""
51+
52+
directory = os.path.abspath(directory)
53+
54+
paths = []
55+
56+
dirs = [directory]
57+
while dirs:
58+
dir = dirs.pop()
59+
with os.scandir(dir) as d:
60+
for entry in d:
61+
if entry.is_dir():
62+
dirs.append(entry.path)
63+
else:
64+
paths.append(entry.path)
65+
66+
# We sort to make sure that we get a consistent and well-defined ordering.
67+
paths.sort()
68+
69+
hasher = blake2b()
70+
71+
for path in paths:
72+
with open(os.path.join(directory, path), "rb") as f:
73+
hasher.update(f.read())
74+
75+
return hasher.hexdigest()
76+
77+
78+
def _dist_is_editable() -> bool:
79+
"""Is distribution an editable install?"""
80+
for path_item in sys.path:
81+
egg_link = os.path.join(path_item, "matrix-synapse.egg-link")
82+
if os.path.isfile(egg_link):
83+
return True
84+
return False

0 commit comments

Comments
 (0)