Skip to content

Commit d143276

Browse files
authored
Fix rust source check when using .egg-info (#19251)
We have checks to try and catch the case where Synapse is being run from a source directory, but the compiled Rust code is out-of-date. This commonly happens when Synapse is updated without running `poetry install` (or equivalent). These checks did not correctly handle `.egg-info` installs, and so were not run. Currently, the `.egg-info` directory is created automatically by poetry (due to using setuptools to build Rust).
1 parent 034c5e6 commit d143276

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

changelog.d/19251.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix check of the Rust compiled code being outdated when using source checkout and `.egg-info`.

synapse/util/rust.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,38 @@ def get_synapse_source_directory() -> str | None:
111111
# c.f. https://packaging.python.org/en/latest/specifications/direct-url/
112112
direct_url_json = package.read_text("direct_url.json")
113113
if direct_url_json is None:
114-
return None
114+
# No direct url metadata. Check if this is an egg-info install.
115+
#
116+
# An egg-info install is when there exists a `matrix_synapse.egg-info`
117+
# directory alongside the source tree, containing the package metadata.
118+
# This allows discovering packages in the current directory, without
119+
# installing them properly to the environment wide `site-packages`
120+
# directory.
121+
#
122+
# When searching for a package, Python will look for `.egg-info` files
123+
# in the current working directory before looking in `site-packages`.
124+
# This means that when running Synapse (or the tests) from the source
125+
# tree Python will pick up the synapse package from the egg-info
126+
# install.
127+
#
128+
# Poetry will create an egg-info install when running `poetry install`.
129+
#
130+
# The combination of the above means that it is very common for
131+
# developers (e.g. running tests) to encounter egg-info installs.
132+
#
133+
# In this case we can find the source tree by looking for the
134+
# `matrix_synapse.egg-info/PKG-INFO` file, and going up two directories
135+
# from there.
136+
137+
metadata_path = package.locate_file("matrix_synapse.egg-info/PKG-INFO")
138+
if not os.path.exists(str(metadata_path)):
139+
# Not an egg-info install.
140+
return None
141+
142+
# `metadata_path` points to the egg-info/PKG-INFO file, so go up two
143+
# directories to get the root of the source tree.
144+
source_dir = metadata_path.parent.parent
145+
return os.fspath(source_dir)
115146

116147
# c.f. https://packaging.python.org/en/latest/specifications/direct-url/ for
117148
# the format

0 commit comments

Comments
 (0)