Skip to content

Commit 22d6585

Browse files
committed
feat(loader): allow specifying the wasi sdk path
1 parent f0cfaff commit 22d6585

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

crates/loader/src/loader.rs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,21 +1105,51 @@ impl Loader {
11051105
Ok(())
11061106
}
11071107

1108+
/// This ensures that the wasi-sdk is available, downloading and extracting it if necessary,
1109+
/// and returns the path to the `clang` executable.
1110+
///
1111+
/// If `TREE_SITTER_WASI_SDK_PATH` is set, it will use that path to look for the clang executable.
11081112
fn ensure_wasi_sdk_exists(&self) -> Result<PathBuf, Error> {
1113+
let possible_executables = if cfg!(windows) {
1114+
vec![
1115+
"clang.exe",
1116+
"wasm32-unknown-wasi-clang.exe",
1117+
"wasm32-wasi-clang.exe",
1118+
]
1119+
} else {
1120+
vec!["clang", "wasm32-unknown-wasi-clang", "wasm32-wasi-clang"]
1121+
};
1122+
1123+
if let Ok(wasi_sdk_path) = std::env::var("TREE_SITTER_WASI_SDK_PATH") {
1124+
let wasi_sdk_dir = PathBuf::from(wasi_sdk_path);
1125+
1126+
for exe in &possible_executables {
1127+
let clang_exe = wasi_sdk_dir.join("bin").join(exe);
1128+
if clang_exe.exists() {
1129+
return Ok(clang_exe);
1130+
}
1131+
}
1132+
1133+
return Err(anyhow!(
1134+
"TREE_SITTER_WASI_SDK_PATH is set to '{}', but no clang executable found in 'bin/' directory. \
1135+
Looked for: {}",
1136+
wasi_sdk_dir.display(),
1137+
possible_executables.join(", ")
1138+
));
1139+
}
1140+
11091141
let cache_dir = etcetera::choose_base_strategy()?
11101142
.cache_dir()
11111143
.join("tree-sitter");
11121144
fs::create_dir_all(&cache_dir)?;
11131145

11141146
let wasi_sdk_dir = cache_dir.join("wasi-sdk");
1115-
let clang_exe = if cfg!(windows) {
1116-
wasi_sdk_dir.join("bin").join("clang.exe")
1117-
} else {
1118-
wasi_sdk_dir.join("bin").join("clang")
1119-
};
11201147

1121-
if clang_exe.exists() {
1122-
return Ok(clang_exe);
1148+
for exe in &possible_executables {
1149+
let clang_exe = wasi_sdk_dir.join("bin").join(exe);
1150+
if clang_exe.exists() {
1151+
return Ok(clang_exe);
1152+
}
11231153
}
11241154

11251155
fs::create_dir_all(&wasi_sdk_dir)?;
@@ -1176,14 +1206,20 @@ impl Loader {
11761206
.context("Failed to extract wasi-sdk archive")?;
11771207

11781208
fs::remove_file(temp_tar_path).ok();
1179-
if !clang_exe.exists() {
1180-
return Err(anyhow!(
1209+
for exe in &possible_executables {
1210+
let clang_exe = wasi_sdk_dir.join("bin").join(exe);
1211+
if !clang_exe.exists() {
1212+
return Err(anyhow!(
11811213
"Failed to extract wasi-sdk correctly. Clang executable not found at expected location: {}",
11821214
clang_exe.display()
11831215
));
1216+
}
11841217
}
11851218

1186-
Ok(clang_exe)
1219+
Err(anyhow!(
1220+
"Failed to find clang executable in downloaded wasi-sdk at '{}'",
1221+
wasi_sdk_dir.display()
1222+
))
11871223
}
11881224

11891225
#[must_use]

0 commit comments

Comments
 (0)