Skip to content

Commit f3bea18

Browse files
committed
fix: link time version errors instead of runtime checks
1 parent c7bb9af commit f3bea18

File tree

6 files changed

+18
-26
lines changed

6 files changed

+18
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
[package]
22
name = "tokenizers"
3-
# Bump minor.patch version every time we bump tokenizers dependency version.
4-
# Can't bump major version because Go doesn't like major version >= 2.
5-
version = "1.22.0"
3+
version = "1.22.1"
64
edition = "2021"
75

86
[lib]

example/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/daulet/tokenizers/example
22

33
go 1.22
44

5-
require github.com/daulet/tokenizers v1.21.1
5+
require github.com/daulet/tokenizers v1.22.0

example/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/daulet/tokenizers v1.21.1 h1:Y+cxx+6K06bCN+bTPQW72zZTTVlJl/8Hq7afVRKmNok=
2-
github.com/daulet/tokenizers v1.21.1/go.mod h1:tGnMdZthXdcWY6DGD07IygpwJqiPvG85FQUnhs/wSCs=
1+
github.com/daulet/tokenizers v1.22.0 h1:Bj7sRW99FpfGU4tNIp0s0j/IKr26pPHqYLb6i1oT+kA=
2+
github.com/daulet/tokenizers v1.22.0/go.mod h1:tGnMdZthXdcWY6DGD07IygpwJqiPvG85FQUnhs/wSCs=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ use tokenizers::tokenizer::Tokenizer;
66
use serde::{Deserialize, Serialize};
77
use tiktoken_rs;
88

9-
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
9+
// Version-specific symbol that will cause link failure if version doesn't match
10+
// Bump minor.patch version every time we bump tokenizers dependency version.
11+
// Can't bump major version because Go doesn't like major version >= 2.
12+
#[no_mangle]
13+
pub extern "C" fn tokenizers_version_1_22_1() {
14+
// This function exists purely as a link-time version check
15+
}
1016

1117
/// Truncation direction for tokenizer truncation
1218
#[repr(u8)]
@@ -168,11 +174,6 @@ pub struct tokenizers_buffer {
168174
len: usize,
169175
}
170176

171-
#[no_mangle]
172-
pub extern "C" fn tokenizers_version() -> *const libc::c_char {
173-
std::ffi::CString::new(CARGO_PKG_VERSION).unwrap().into_raw()
174-
}
175-
176177
#[no_mangle]
177178
pub extern "C" fn tokenizers_from_bytes(bytes: *const u8, len: u32, opts: &tokenizers_options, error: *mut *mut libc::c_char) -> *mut libc::c_void {
178179
if bytes.is_null() {

tokenizer.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ package tokenizers
66
#cgo LDFLAGS: -ltokenizers -ldl -lm -lstdc++
77
#include <stdlib.h>
88
#include "tokenizers.h"
9+
10+
// Link-time version check: this will fail to link if the library version doesn't match
11+
// Using a global variable that references the function ensures the linker must resolve it
12+
extern void tokenizers_version_1_22_1(void);
13+
void (*tokenizers_version_check)(void) = &tokenizers_version_1_22_1;
914
*/
1015
import "C"
1116

@@ -21,11 +26,7 @@ import (
2126
"unsafe"
2227
)
2328

24-
const (
25-
WANT_VERSION = "1.22.0"
26-
27-
baseURL = "https://huggingface.co"
28-
)
29+
const baseURL = "https://huggingface.co"
2930

3031
// List of necessary tokenizer files and their mandatory status.
3132
// True means mandatory, false means optional.
@@ -37,14 +38,6 @@ var tokenizerFiles = map[string]bool{
3738
"added_tokens.json": false,
3839
}
3940

40-
func init() {
41-
version := C.tokenizers_version()
42-
got := C.GoString(version)
43-
if got != WANT_VERSION {
44-
panic(fmt.Errorf("tokenizers library version mismatch, want: %s, got: %s", WANT_VERSION, got))
45-
}
46-
}
47-
4841
type Tokenizer struct {
4942
tokenizer unsafe.Pointer
5043
}

0 commit comments

Comments
 (0)