Skip to content

Commit 890e60c

Browse files
authored
feat: add sanity check to make sure Python and engine version consistent (#1323)
1 parent d5ef487 commit 890e60c

File tree

6 files changed

+67
-28
lines changed

6 files changed

+67
-28
lines changed

.github/scripts/update_version.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ fi
2020

2121
# Update Cargo.toml
2222
sed "${SED_INLINE[@]}" "s/^version = .*/version = \"$VERSION\"/" Cargo.toml
23+
echo "__version__ = \"$VERSION\"" > python/cocoindex/_version.py

.github/workflows/release.yml

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,13 @@ permissions:
1515
pages: write
1616

1717
jobs:
18-
create-versioned-toml:
19-
runs-on: ubuntu-latest
20-
steps:
21-
- uses: actions/checkout@v4
22-
with:
23-
fetch-depth: 1
24-
- run: ./.github/scripts/update_version.sh
25-
- uses: actions/upload-artifact@v4
26-
with:
27-
name: Cargo.toml
28-
path: Cargo.toml
29-
3018
generate-3p-notices:
3119
runs-on: ubuntu-latest
32-
needs: [create-versioned-toml]
3320
steps:
3421
- uses: actions/checkout@v4
3522
with:
3623
fetch-depth: 1
37-
- uses: actions/download-artifact@v4
38-
with:
39-
name: Cargo.toml
24+
- run: ./.github/scripts/update_version.sh
4025
- name: Install Rust toolchain
4126
uses: dtolnay/rust-toolchain@stable
4227
- uses: taiki-e/install-action@v2
@@ -54,7 +39,7 @@ jobs:
5439

5540
build:
5641
runs-on: ${{ matrix.platform.runner }}
57-
needs: [create-versioned-toml, generate-3p-notices]
42+
needs: [generate-3p-notices]
5843
strategy:
5944
matrix:
6045
platform:
@@ -67,9 +52,7 @@ jobs:
6752
- uses: actions/checkout@v4
6853
with:
6954
fetch-depth: 1
70-
- uses: actions/download-artifact@v4
71-
with:
72-
name: Cargo.toml
55+
- run: ./.github/scripts/update_version.sh
7356
- uses: actions/download-artifact@v4
7457
with:
7558
name: THIRD_PARTY_NOTICES.html
@@ -110,14 +93,12 @@ jobs:
11093

11194
sdist:
11295
runs-on: ubuntu-latest
113-
needs: [create-versioned-toml, generate-3p-notices]
96+
needs: [generate-3p-notices]
11497
steps:
11598
- uses: actions/checkout@v4
11699
with:
117100
fetch-depth: 1
118-
- uses: actions/download-artifact@v4
119-
with:
120-
name: Cargo.toml
101+
- run: ./.github/scripts/update_version.sh
121102
- uses: actions/download-artifact@v4
122103
with:
123104
name: THIRD_PARTY_NOTICES.html
@@ -135,7 +116,7 @@ jobs:
135116
release:
136117
name: Release
137118
runs-on: ubuntu-latest
138-
needs: [create-versioned-toml, build, test-abi3, sdist, generate-3p-notices]
119+
needs: [build, test-abi3, sdist, generate-3p-notices]
139120
permissions:
140121
# Use to sign the release artifacts
141122
id-token: write
@@ -148,9 +129,7 @@ jobs:
148129
- uses: actions/checkout@v4
149130
with:
150131
fetch-depth: 1
151-
- uses: actions/download-artifact@v4
152-
with:
153-
name: Cargo.toml
132+
- run: ./.github/scripts/update_version.sh
154133
- uses: actions/download-artifact@v4
155134
with:
156135
name: THIRD_PARTY_NOTICES.html

python/cocoindex/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Cocoindex is a framework for building and running indexing pipelines.
33
"""
44

5+
from ._version import __version__
6+
7+
from . import _version_check
8+
59
from . import _engine # type: ignore
610
from . import functions, sources, targets, cli, utils
711

@@ -46,6 +50,7 @@
4650
_engine.init_pyo3_runtime()
4751

4852
__all__ = [
53+
"__version__",
4954
# Submodules
5055
"_engine",
5156
"functions",

python/cocoindex/_version.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This file will be rewritten by the release workflow.
2+
# DO NOT ADD ANYTHING ELSE TO THIS FILE.
3+
__version__ = "999.0.0"

python/cocoindex/_version_check.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from . import _engine
5+
from . import __version__
6+
7+
8+
def _sanity_check_engine() -> None:
9+
engine_file = getattr(_engine, "__file__", "<unknown>")
10+
engine_version = getattr(_engine, "__version__", None)
11+
12+
problems: list[str] = []
13+
14+
# Version mismatch (if the engine exposes its own version)
15+
if engine_version is not None and engine_version != __version__:
16+
problems.append(
17+
f"Version mismatch: Python package is {__version__!r}, "
18+
f"but cocoindex._engine reports {engine_version!r}."
19+
)
20+
21+
if problems:
22+
# Helpful diagnostic message for users
23+
msg_lines = [
24+
"Inconsistent cocoindex installation detected:",
25+
*[f" - {p}" for p in problems],
26+
"",
27+
f"Python executable: {sys.executable}",
28+
f"cocoindex package file: {__file__}",
29+
f"cocoindex._engine file: {engine_file}",
30+
"",
31+
"This usually happens when:",
32+
" * An old 'cocoindex._engine' .pyd is still present in the",
33+
" package directory, or",
34+
" * Multiple 'cocoindex' copies exist on sys.path",
35+
" (e.g. a local checkout + an installed wheel).",
36+
"",
37+
"Suggested fix:",
38+
" 1. Uninstall cocoindex completely:",
39+
" pip uninstall cocoindex",
40+
" 2. Reinstall it cleanly:",
41+
" pip install --no-cache-dir cocoindex",
42+
" 3. Ensure there is no local 'cocoindex' directory or old",
43+
" .pyd shadowing the installed package.",
44+
]
45+
raise RuntimeError("\n".join(msg_lines))
46+
47+
48+
_sanity_check_engine()
49+
del _sanity_check_engine

rust/cocoindex/src/py/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,8 @@ fn seder_roundtrip<'py>(
606606
#[pymodule]
607607
#[pyo3(name = "_engine")]
608608
fn cocoindex_engine(m: &Bound<'_, PyModule>) -> PyResult<()> {
609+
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
610+
609611
m.add_function(wrap_pyfunction!(init_pyo3_runtime, m)?)?;
610612
m.add_function(wrap_pyfunction!(init, m)?)?;
611613
m.add_function(wrap_pyfunction!(set_settings_fn, m)?)?;

0 commit comments

Comments
 (0)