Skip to content

Commit c1334b0

Browse files
committed
Auto merge of #11645 - chansuke:issue-11597, r=weihanglo
Add `CARGO_PKG_README` Fixes #11597 This environment variable shows the path to the README file of your package. From #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.
2 parents f72f8a8 + 34a17ea commit c1334b0

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

src/cargo/core/compiler/compilation.rs

+4
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ impl<'cfg> Compilation<'cfg> {
343343
"CARGO_PKG_RUST_VERSION",
344344
&pkg.rust_version().unwrap_or(&String::new()),
345345
)
346+
.env(
347+
"CARGO_PKG_README",
348+
metadata.readme.as_ref().unwrap_or(&String::new()),
349+
)
346350
.cwd(pkg.root());
347351

348352
// Apply any environment variables from the config

src/doc/src/reference/environment-variables.md

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ corresponding environment variable is set to the empty string, `""`.
231231
* `CARGO_PKG_RUST_VERSION` --- The Rust version from the manifest of your package.
232232
Note that this is the minimum Rust version supported by the package, not the
233233
current Rust version.
234+
* `CARGO_PKG_README` --- Path to the README file of your package.
234235
* `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.
235236
* `CARGO_BIN_NAME` --- The name of the binary that is currently being compiled.
236237
Only set for [binaries] or binary [examples]. This name does not include any

tests/testsuite/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,7 @@ fn crate_env_vars() {
13721372
license = "MIT OR Apache-2.0"
13731373
license-file = "license.txt"
13741374
rust-version = "1.61.0"
1375+
readme = "../../README.md"
13751376
13761377
[[bin]]
13771378
name = "foo-bar"
@@ -1397,6 +1398,7 @@ fn crate_env_vars() {
13971398
static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE");
13981399
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
13991400
static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION");
1401+
static README: &'static str = env!("CARGO_PKG_README");
14001402
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
14011403
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
14021404
@@ -1416,6 +1418,7 @@ fn crate_env_vars() {
14161418
assert_eq!("license.txt", LICENSE_FILE);
14171419
assert_eq!("This is foo", DESCRIPTION);
14181420
assert_eq!("1.61.0", RUST_VERSION);
1421+
assert_eq!("../../README.md", README);
14191422
let s = format!("{}.{}.{}-{}", VERSION_MAJOR,
14201423
VERSION_MINOR, VERSION_PATCH, VERSION_PRE);
14211424
assert_eq!(s, VERSION);

0 commit comments

Comments
 (0)