From 29c0d98ec15e395ff22ebad215446b95b1166261 Mon Sep 17 00:00:00 2001 From: raldone01 Date: Sun, 19 Jan 2025 23:03:41 +0100 Subject: [PATCH] `v0.3.3` * Add support for single file rust scripts that use `CARGO_MANIFEST_PATH`. --- CHANGELOG.md | 7 ++++++- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cargo_manifest.rs | 36 ++++++++++++++++++++++++--------- tests/workspace_deps/Cargo.lock | 2 +- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61aacbc..3f83ea4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.3.3] - 2025-01-19 + +* Add support for single file rust scripts that use `CARGO_MANIFEST_PATH`. + ## [0.3.2] - 2025-01-18 * Relax rust edition to 2021 for bevy. @@ -43,7 +47,8 @@ Support reactive compilation using `proc_macro_tracked_env` and `track_path` nig Initial release. -[Unreleased]: https://github.com/ink-feather-org/cargo-manifest-proc-macros-rs/compare/v0.3.2...HEAD +[Unreleased]: https://github.com/ink-feather-org/cargo-manifest-proc-macros-rs/compare/v0.3.3...HEAD +[0.3.3]: https://github.com/ink-feather-org/cargo-manifest-proc-macros-rs/compare/v0.3.2...v0.3.3 [0.3.2]: https://github.com/ink-feather-org/cargo-manifest-proc-macros-rs/compare/v0.3.1...v0.3.2 [0.3.1]: https://github.com/ink-feather-org/cargo-manifest-proc-macros-rs/compare/v0.3.0...v0.3.1 [0.3.0]: https://github.com/ink-feather-org/cargo-manifest-proc-macros-rs/compare/v0.2.2...v0.3.0 diff --git a/Cargo.lock b/Cargo.lock index 466d72c..69f7eed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,7 +25,7 @@ checksum = "1be3f42a67d6d345ecd59f675f3f012d6974981560836e938c22b424b85ce1be" [[package]] name = "cargo-manifest-proc-macros" -version = "0.3.2" +version = "0.3.3" dependencies = [ "serial_test", "syn", diff --git a/Cargo.toml b/Cargo.toml index 10fe3c1..6c8e230 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,7 +79,7 @@ strip = "debuginfo" [package] name = "cargo-manifest-proc-macros" -version = "0.3.2" +version = "0.3.3" edition = "2021" # once we reach 1.85.0 in msrv, we can bump this to 2024 license = "MIT OR Apache-2.0" description = "Find the syn::Path to your own crate from proc-macros reliably." diff --git a/src/cargo_manifest.rs b/src/cargo_manifest.rs index ecde007..12931b6 100644 --- a/src/cargo_manifest.rs +++ b/src/cargo_manifest.rs @@ -13,12 +13,12 @@ use tracing::{info, trace}; use crate::syn_utils::{crate_name_to_syn_path, pretty_format_syn_path}; -fn get_env_var(name: &str) -> String { +fn get_env_var(name: &str) -> Option { #[cfg(not(all(feature = "nightly", feature = "proc-macro")))] let env_var = std::env::var(name); #[cfg(all(feature = "nightly", feature = "proc-macro"))] let env_var = proc_macro::tracked_env::var(name); - env_var.unwrap_or_else(|_| panic!("The environment variable {name} must be set!")) + env_var.ok() } fn track_path(path: impl AsRef) { @@ -249,8 +249,20 @@ impl CargoManifest { // The environment variable `CARGO_MANIFEST_PATH` is not consistently set in all environments. // Access environment variables through the `tracked_env` module to ensure that the proc-macro is re-run when the environment variables change. - let mut cargo_manifest_path = PathBuf::from(get_env_var("CARGO_MANIFEST_DIR")); - cargo_manifest_path.push("Cargo.toml"); + let cargo_manifest_path_env_var = get_env_var("CARGO_MANIFEST_PATH"); + let cargo_manifest_path = cargo_manifest_path_env_var.map_or_else( + || { + // If the `CARGO_MANIFEST_PATH` environment variable is not set, we fall + // back to the `CARGO_MANIFEST_DIR` environment variable. + let cargo_manifest_dir = get_env_var("CARGO_MANIFEST_DIR").unwrap_or_else(|| { + panic!("The environment variable CARGO_MANIFEST_DIR must be set!"); + }); + let mut cargo_manifest_path = PathBuf::from(cargo_manifest_dir); + cargo_manifest_path.push("Cargo.toml"); + cargo_manifest_path + }, + PathBuf::from, + ); assert!( cargo_manifest_path.exists(), "Cargo.toml does not exist at \"{}\"!", @@ -505,12 +517,14 @@ impl CargoManifest { /// https://github.com/bkchr/proc-macro-crate/blob/a5939f3fbf94279b45902119d97f881fefca6a0d/src/lib.rs#L243 #[must_use] fn resolve_workspace_manifest_path(cargo_manifest_path: &Path) -> PathBuf { - let cmd_result = Command::new(get_env_var("CARGO")) - .arg("locate-project") - .args(["--workspace", "--message-format=plain"]) - .arg(format!("--manifest-path={}", cargo_manifest_path.display())) - .output() - .expect("Failed to run `cargo locate-project`!"); + let cmd_result = Command::new( + get_env_var("CARGO").unwrap_or_else(|| panic!("The environment variable CARGO must be set!")), + ) + .arg("locate-project") + .args(["--workspace", "--message-format=plain"]) + .arg(format!("--manifest-path={}", cargo_manifest_path.display())) + .output() + .expect("Failed to run `cargo locate-project`!"); let stdout = cmd_result.stdout; let stderr = cmd_result.stderr; @@ -1096,6 +1110,7 @@ mod fs_tests { #[expect(unsafe_code)] // SAFETY: The test is marked as serial, so it is safe to set the environment variable. unsafe { + std::env::remove_var("CARGO_MANIFEST_PATH"); std::env::set_var("CARGO_MANIFEST_DIR", crate_manifest_path.parent().unwrap()); }; let initial_mtime = CargoManifest::get_cargo_manifest_mtime(&crate_manifest_path).unwrap(); @@ -1214,6 +1229,7 @@ mod fs_tests { #[expect(unsafe_code)] // SAFETY: The test is marked as serial, so it is safe to set the environment variable. unsafe { + std::env::remove_var("CARGO_MANIFEST_PATH"); std::env::set_var("CARGO_MANIFEST_DIR", crate_manifest_path.parent().unwrap()); }; let initial_mtime = CargoManifest::get_cargo_manifest_mtime(&crate_manifest_path).unwrap(); diff --git a/tests/workspace_deps/Cargo.lock b/tests/workspace_deps/Cargo.lock index 13428b1..4cf318f 100644 --- a/tests/workspace_deps/Cargo.lock +++ b/tests/workspace_deps/Cargo.lock @@ -4,7 +4,7 @@ version = 4 [[package]] name = "cargo-manifest-proc-macros" -version = "0.3.1" +version = "0.3.2" dependencies = [ "syn", "thiserror",