diff --git a/.cargo/config.toml b/.cargo/config.toml index ce666a159..838b8d683 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,7 +1,7 @@ [env] STRINGS_JSON = { value = "out/rslib/i18n/strings.json", relative = true } # build script will append .exe if necessary -PROTOC_BINARY = { value = "out/extracted/protoc/bin/protoc", relative = true } +PROTOC = { value = "out/extracted/protoc/bin/protoc", relative = true } PYO3_NO_PYTHON = "1" MACOSX_DEPLOYMENT_TARGET = "10.13.4" diff --git a/rslib/build/protobuf.rs b/rslib/build/protobuf.rs index 2e4affa5b..4f8b3b3ca 100644 --- a/rslib/build/protobuf.rs +++ b/rslib/build/protobuf.rs @@ -73,7 +73,7 @@ fn service_generator() -> Box { } pub fn write_backend_proto_rs() { - maybe_add_protobuf_to_path(); + set_protoc_path(); let proto_dir = PathBuf::from("../proto"); let subfolders = &["anki"]; @@ -122,13 +122,14 @@ pub fn write_backend_proto_rs() { .unwrap(); } -/// If PROTOC is not defined, and protoc is not on path, use the protoc -/// fetched by Bazel so that Rust Analyzer does not fail. -fn maybe_add_protobuf_to_path() { - if let Ok(protoc) = env::var("PROTOC_BINARY") { - env::set_var("PROTOC", &protoc); - if cfg!(windows) && !protoc.ends_with(".exe") { - env::set_var("PROTOC", format!("{protoc}.exe")); +/// Set PROTOC to the custom path provided by PROTOC_BINARY, or add .exe to +/// the standard path if on Windows. +fn set_protoc_path() { + if let Ok(custom_protoc) = env::var("PROTOC_BINARY") { + env::set_var("PROTOC", custom_protoc); + } else if let Ok(bundled_protoc) = env::var("PROTOC") { + if cfg!(windows) && !bundled_protoc.ends_with(".exe") { + env::set_var("PROTOC", format!("{bundled_protoc}.exe")); } } }