diff --git a/examples/bazel_build/MODULE.bazel b/examples/bazel_build/MODULE.bazel index d28da39f8ce..656e60a60b8 100644 --- a/examples/bazel_build/MODULE.bazel +++ b/examples/bazel_build/MODULE.bazel @@ -49,7 +49,7 @@ crate.annotation( ) crate.annotation( crate = "proc-macro-crate", - patches = ["@//external_crate_builds:0001-Fix-use-with-packaged-crates-and-Bazel.patch"], + patches = ["@//external_crate_builds:0001-Fix-use-with-Bazel.patch"], patch_args = ["-p1"], ) diff --git a/examples/bazel_build/external_crate_builds/0001-Fix-use-with-Bazel.patch b/examples/bazel_build/external_crate_builds/0001-Fix-use-with-Bazel.patch new file mode 100644 index 00000000000..e2e2b8a8219 --- /dev/null +++ b/examples/bazel_build/external_crate_builds/0001-Fix-use-with-Bazel.patch @@ -0,0 +1,57 @@ +# Copyright © 2025 SixtyFPS GmbH +# SPDX-License-Identifier: MIT + +# See https://github.com/bkchr/proc-macro-crate/pull/55 + +From db99213ae08c7f6cd1dadda60c3b42d16f2c0099 Mon Sep 17 00:00:00 2001 +From: Simon Hausmann +Date: Sun, 5 Jan 2025 10:12:35 +0100 +Subject: [PATCH] Fix use with Bazel + +When the application is built with Bazel as build system, environment +variables like CARGO_MANIFEST_DIR, etc. are set for compatibility, but +CARGO itself isn't, because Bazel is the tool of choice. Therefore any +attempt to invoke Cargo to locate the workspace manifest path fails. + +As a fallback, a lack of the CARGO environment variable now just means +no workspace support. +--- + src/lib.rs | 8 +++++--- + 1 file changed, 5 insertions(+), 3 deletions(-) + +diff --git a/src/lib.rs b/src/lib.rs +index fe46eb3..020d315 100644 +--- a/src/lib.rs ++++ b/src/lib.rs +@@ -100,7 +100,6 @@ use toml_edit::{DocumentMut, Item, Table, TomlError}; + pub enum Error { + NotFound(PathBuf), + CargoManifestDirNotSet, +- CargoEnvVariableNotSet, + FailedGettingWorkspaceManifestPath, + CouldNotRead { path: PathBuf, source: io::Error }, + InvalidToml { source: TomlError }, +@@ -138,7 +137,6 @@ impl fmt::Display for Error { + crate_name, + path.display(), + ), +- Error::CargoEnvVariableNotSet => f.write_str("`CARGO` env variable not set."), + Error::FailedGettingWorkspaceManifestPath => + f.write_str("Failed to get the path of the workspace manifest path."), + } +@@ -241,7 +239,11 @@ pub fn crate_name(orig_name: &str) -> Result { + } + + fn workspace_manifest_path(cargo_toml_manifest: &Path) -> Result, Error> { +- let stdout = Command::new(env::var("CARGO").map_err(|_| Error::CargoEnvVariableNotSet)?) ++ let Ok(cargo) = env::var("CARGO") else { ++ return Ok(None); ++ }; ++ ++ let stdout = Command::new(cargo) + .arg("locate-project") + .args(&["--workspace", "--message-format=plain"]) + .arg(format!("--manifest-path={}", cargo_toml_manifest.display())) +-- +2.39.5 (Apple Git-154) + diff --git a/examples/bazel_build/external_crate_builds/0001-Fix-use-with-packaged-crates-and-Bazel.patch b/examples/bazel_build/external_crate_builds/0001-Fix-use-with-packaged-crates-and-Bazel.patch deleted file mode 100644 index 57ace07d5e4..00000000000 --- a/examples/bazel_build/external_crate_builds/0001-Fix-use-with-packaged-crates-and-Bazel.patch +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright © 2025 SixtyFPS GmbH -# SPDX-License-Identifier: MIT - -# See https://github.com/bkchr/proc-macro-crate/pull/55 - -From 47d918dc6575e5a6302ce32780cf6e5fd32518fd Mon Sep 17 00:00:00 2001 -From: Simon Hausmann -Date: Sun, 5 Jan 2025 10:12:35 +0100 -Subject: [PATCH] Fix use with packaged crates and Bazel - -When the application is built with Bazel as build system, environment -variables like CARGO_MANIFEST_DIR, etc. are set for compatibility, but -CARGO itself isn't, because Bazel is the tool of choice. Therefore any -attempt to invoke Cargo to locate the workspace manifest path fails. - -Using proc-macro-crate with crates that are using Cargo workspaces makes -indeed only sense when Cargo is the build system. But when the crates -are packaged, then we don't even need to invoke cargo anyway to locate -a workspace, as there can't be any. - -This speeds up the general build and makes it possible to use -proc-macro-crate with Bazel builds. ---- - src/lib.rs | 5 +++++ - 1 file changed, 5 insertions(+) - -diff --git a/src/lib.rs b/src/lib.rs -index fe46eb3..45339f3 100644 ---- a/src/lib.rs -+++ b/src/lib.rs -@@ -241,6 +241,11 @@ pub fn crate_name(orig_name: &str) -> Result { - } - - fn workspace_manifest_path(cargo_toml_manifest: &Path) -> Result, Error> { -+ // Skip invoking Cargo if we're building a crate packaged with cargo. -+ if cargo_toml_manifest.with_extension("toml.orig").exists() { -+ return Ok(None); -+ } -+ - let stdout = Command::new(env::var("CARGO").map_err(|_| Error::CargoEnvVariableNotSet)?) - .arg("locate-project") - .args(&["--workspace", "--message-format=plain"]) --- -2.39.5 (Apple Git-154) -