Skip to content

Commit

Permalink
v0.3.3
Browse files Browse the repository at this point in the history
* Add support for single file rust scripts that use `CARGO_MANIFEST_PATH`.
  • Loading branch information
raldone01 committed Jan 19, 2025
1 parent e60d6bb commit 29c0d98
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
36 changes: 26 additions & 10 deletions src/cargo_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
#[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<str>) {
Expand Down Expand Up @@ -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 \"{}\"!",
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/workspace_deps/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 29c0d98

Please sign in to comment.