Skip to content

Commit 2ff7467

Browse files
authored
Merge pull request tree-sitter#3172 from tree-sitter/remove-which-crate
Remove dependency on which crate
2 parents 7687d88 + 99a720c commit 2ff7467

File tree

5 files changed

+23
-61
lines changed

5 files changed

+23
-61
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ unindent = "0.2.3"
8484
walkdir = "2.5.0"
8585
wasmparser = "0.201.0"
8686
webbrowser = "0.8.13"
87-
which = "6.0.0"
8887

8988
tree-sitter = { version = "0.22.0", path = "./lib" }
9089
tree-sitter-loader = { version = "0.22.0", path = "./cli/loader" }

cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ tiny_http.workspace = true
5353
walkdir.workspace = true
5454
wasmparser.workspace = true
5555
webbrowser.workspace = true
56-
which.workspace = true
5756

5857
tree-sitter.workspace = true
5958
tree-sitter-config.workspace = true

cli/loader/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ once_cell.workspace = true
2626
regex.workspace = true
2727
serde.workspace = true
2828
serde_json.workspace = true
29-
which.workspace = true
3029

3130
tree-sitter.workspace = true
3231
tree-sitter-highlight.workspace = true

cli/loader/src/lib.rs

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use serde::{Deserialize, Deserializer, Serialize};
2020
use tree_sitter::{Language, QueryError, QueryErrorKind};
2121
use tree_sitter_highlight::HighlightConfiguration;
2222
use tree_sitter_tags::{Error as TagsError, TagsConfiguration};
23-
use which::which;
2423

2524
pub const EMSCRIPTEN_TAG: &str = concat!("docker.io/emscripten/emsdk:", env!("EMSCRIPTEN_VERSION"));
2625

@@ -712,62 +711,43 @@ impl Loader {
712711
) -> Result<(), Error> {
713712
#[derive(PartialEq, Eq)]
714713
enum EmccSource {
715-
Native(PathBuf),
714+
Native,
716715
Docker,
717716
Podman,
718717
}
719718

720-
fn path_of_bin(
721-
name: &str,
722-
test: impl Fn(&Path) -> std::io::Result<std::process::Output>,
723-
) -> Option<PathBuf> {
724-
let bin_path = which(name).ok()?;
725-
if test(&bin_path).is_ok() {
726-
Some(bin_path)
727-
} else {
728-
None
729-
}
730-
}
719+
let emcc_name = if cfg!(windows) { "emcc.bat" } else { "emcc" };
731720

732721
// Order of preference: emscripten > docker > podman > error
733-
let source = if force_docker {
734-
None
722+
let source = if !force_docker && Command::new(emcc_name).output().is_ok() {
723+
EmccSource::Native
724+
} else if Command::new("docker")
725+
.arg("info")
726+
.output()
727+
.map_or(false, |out| out.status.success())
728+
{
729+
EmccSource::Docker
730+
} else if Command::new("podman")
731+
.arg("--version")
732+
.output()
733+
.map_or(false, |out| out.status.success())
734+
{
735+
EmccSource::Podman
735736
} else {
736-
path_of_bin(if cfg!(windows) { "emcc.bat" } else { "emcc" }, |p| {
737-
Command::new(p).output()
738-
})
739-
.map(EmccSource::Native)
740-
}
741-
.or_else(|| {
742-
path_of_bin("docker", |docker| {
743-
// `docker info` should succeed iff the daemon is running
744-
// see https://docs.docker.com/config/daemon/troubleshoot/#check-whether-docker-is-running
745-
Command::new(docker).args(["info"]).output()
746-
})
747-
.map(|_| EmccSource::Docker)
748-
})
749-
.or_else(|| {
750-
path_of_bin("podman", |podman| {
751-
Command::new(podman).arg("--version").output()
752-
})
753-
.map(|_| EmccSource::Podman)
754-
});
755-
756-
let Some(cmd) = source else {
757737
return Err(anyhow!(
758-
"You must have either emcc or docker on your PATH to run this command"
738+
"You must have either emcc, docker, or podman on your PATH to run this command"
759739
));
760740
};
761741

762-
let mut command = match cmd {
763-
EmccSource::Native(emcc_path) => {
764-
let mut command = Command::new(emcc_path);
742+
let mut command = match source {
743+
EmccSource::Native => {
744+
let mut command = Command::new(emcc_name);
765745
command.current_dir(src_path);
766746
command
767747
}
768748

769749
EmccSource::Docker | EmccSource::Podman => {
770-
let mut command = match cmd {
750+
let mut command = match source {
771751
EmccSource::Docker => Command::new("docker"),
772752
EmccSource::Podman => Command::new("podman"),
773753
_ => unreachable!(),
@@ -798,7 +778,7 @@ impl Loader {
798778
fn getuid() -> u32;
799779
}
800780
// don't need to set user for podman since PODMAN_USERNS=keep-id is already set
801-
if cmd == EmccSource::Docker {
781+
if source == EmccSource::Docker {
802782
let user_id = unsafe { getuid() };
803783
command.args(["--user", &user_id.to_string()]);
804784
}

0 commit comments

Comments
 (0)