taiki-e / find-crate

Find the crate name from the current Cargo.toml.

Home Page:https://docs.rs/find-crate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot find workspace version in crate_package

jonathanbourdier opened this issue · comments

When I use cargo workspace, version is pulled from the crate's cargo.toml instead of workspace cargo.toml, resulting in a bug.

It should work with this cargo.toml :

[package]
name = "my_package"
version.workspace = true
authors.workspace = true
edition.workspace = true

lib.rs line 359: pub fn crate_package(&self) -> Result

This lib is used by i18n_embed which causes the impossibility to use it in a workspace.

This issue can be fixed with the following patch.
However, it is debatable whether it is correct to return a string like "*".

diff --git a/src/lib.rs b/src/lib.rs
index 8f28131..2f1735d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -371,9 +371,15 @@ impl Manifest {
             Error::InvalidManifest("[package] section is missing `version`".to_string())
         })?;
 
-        let package_version = package_version_value.as_str().ok_or_else(|| {
-            Error::InvalidManifest("`version` in [package] section is not a string".to_string())
-        })?;
+        let package_version = match package_version_value {
+            Value::String(s) => s.clone(),
+            Value::Table(t) => "*".to_string(),
+            _ => {
+                return Err(Error::InvalidManifest(
+                    "`version` in [package] section is not a string".to_string(),
+                ))
+            }
+        };
 
         let package = Package {
             key: package_key.to_string(),

I think the correct approach is to return the version defined in the [workspace.dependencies].

I think the correct approach is to return the version defined in the [workspace.dependencies].

Yeah, we need a stable way to get the workspace root.
rust-lang/cargo#3946

Could std::env::var_os("CARGO_PKG_VERSION") be used instead?

Could std::env::var_os("CARGO_PKG_VERSION") be used instead?

CARGO_PKG_VERSION is the version of the current package, not dependency.

Yeah, we need a stable way to get the workspace root.
rust-lang/cargo#3946

Given this limitation, it may actually be a reasonable compromise to return * for now, with documentation that the exact version requirements are not returned for workspace dependencies.

CARGO_PKG_VERSION is the version of the current package, not dependency.

I tested it and found that it only helps with this issue when using the proc macro like cargo-i18n ...

CARGO_PKG_VERSION

If it's about the current package version and not the dependency version, that could indeed still work well in some cases.
(I thought this issue was about workspace dependencies.)

Wait, why did we have crate_package in the first place? Did env!("CARGO_PKG_VERSION") not work?

cc @kellpossible (because you opened #11)

I'm proposing at cargo-i18n to fallback to std::env::var("CARGO_PKG_NAME") .

kellpossible/cargo-i18n#97 (comment)

Wait, why did we have crate_package in the first place? Did env!("CARGO_PKG_VERSION") not work?

In the case of env!, the value from the crate (i18n-embed-fl) is used instead of the package being compiled.