Skip to content

Commit

Permalink
Auto merge of rust-lang#11645 - chansuke:issue-11597, r=weihanglo
Browse files Browse the repository at this point in the history
Add `CARGO_PKG_README`

Fixes rust-lang#11597

This environment variable shows the path to the README file of your package. From rust-lang#11597:

> Cargo may rewrite the package’s `Cargo.toml` and move the README file around, relative to the manifest. I would like to `include_str!()` this README in my `lib.rs`, but am unable to do so right now, because if I specify `include_str!("../../README")` it works for development, but I can’t package my crate. Conversely if I specify `include_str!("../README")` it works when packaged, but not during development.
  • Loading branch information
bors committed Mar 7, 2023
2 parents f72f8a8 + 34a17ea commit c1334b0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ impl<'cfg> Compilation<'cfg> {
"CARGO_PKG_RUST_VERSION",
&pkg.rust_version().unwrap_or(&String::new()),
)
.env(
"CARGO_PKG_README",
metadata.readme.as_ref().unwrap_or(&String::new()),
)
.cwd(pkg.root());

// Apply any environment variables from the config
Expand Down
1 change: 1 addition & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ corresponding environment variable is set to the empty string, `""`.
* `CARGO_PKG_RUST_VERSION` --- The Rust version from the manifest of your package.
Note that this is the minimum Rust version supported by the package, not the
current Rust version.
* `CARGO_PKG_README` --- Path to the README file of your package.
* `CARGO_CRATE_NAME` --- The name of the crate that is currently being compiled. It is the name of the [Cargo target] with `-` converted to `_`, such as the name of the library, binary, example, integration test, or benchmark.
* `CARGO_BIN_NAME` --- The name of the binary that is currently being compiled.
Only set for [binaries] or binary [examples]. This name does not include any
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ fn crate_env_vars() {
license = "MIT OR Apache-2.0"
license-file = "license.txt"
rust-version = "1.61.0"
readme = "../../README.md"
[[bin]]
name = "foo-bar"
Expand All @@ -1397,6 +1398,7 @@ fn crate_env_vars() {
static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE");
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION");
static README: &'static str = env!("CARGO_PKG_README");
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
Expand All @@ -1416,6 +1418,7 @@ fn crate_env_vars() {
assert_eq!("license.txt", LICENSE_FILE);
assert_eq!("This is foo", DESCRIPTION);
assert_eq!("1.61.0", RUST_VERSION);
assert_eq!("../../README.md", README);
let s = format!("{}.{}.{}-{}", VERSION_MAJOR,
VERSION_MINOR, VERSION_PATCH, VERSION_PRE);
assert_eq!(s, VERSION);
Expand Down

0 comments on commit c1334b0

Please sign in to comment.