mirror of
https://github.com/rust-lang/cargo
synced 2024-10-05 23:39:47 +00:00
Auto merge of #12268 - epage:direct, r=weihanglo
fix(embedded): Don't create an intermediate manifest ### What does this PR try to resolve? More immediately, this is to unblock rust-lang/rust#112601 More generally, this gets us away from hackily writing out an out-of-line manifest from an embedded manifest. To parse the manifest, we have to write it out so our regular manifest loading code could handle it. This updates the manifest parsing code to handle it. This doesn't mean this will work everywhere in all cases though. For example, ephemeral workspaces parses a manifest from the SourceId and these won't have valid SourceIds. As a consequence, `Cargo.lock` and `CARGO_TARGET_DIR` are changing from being next to the temp manifest to being next to the script. This still isn't the desired behavior but stepping stones. ### How should we test and review this PR? A Commit at a time ### Additional information In production code, this does not conflict with #12255 (due to #12262) but in test code, it does.
This commit is contained in:
commit
0d5370a9ae
35
Cargo.lock
generated
35
Cargo.lock
generated
|
@ -99,24 +99,12 @@ version = "1.6.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6"
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8868f09ff8cea88b079da74ae569d9b8c62a23c68c746240b704ee6f7525c89c"
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
|
@ -199,20 +187,6 @@ dependencies = [
|
|||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "blake3"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "729b71f35bd3fa1a4c86b85d32c8b9069ea7fe14f7a53cfabb65f62d4265b888"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"arrayvec 0.7.3",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"constant_time_eq",
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.4"
|
||||
|
@ -292,7 +266,6 @@ version = "0.73.0"
|
|||
dependencies = [
|
||||
"anyhow",
|
||||
"base64",
|
||||
"blake3",
|
||||
"bytesize",
|
||||
"cargo-platform 0.1.3",
|
||||
"cargo-test-macro",
|
||||
|
@ -554,12 +527,6 @@ version = "0.9.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "520fbf3c07483f94e3e3ca9d0cfd913d7718ef2483d2cfd91c0d9e91474ab913"
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21a53c0a4d288377e7415b53dcfc3c04da5cdc2cc95c8d5ac178b58f0b861ad6"
|
||||
|
||||
[[package]]
|
||||
name = "content_inspector"
|
||||
version = "0.2.4"
|
||||
|
@ -3512,7 +3479,7 @@ version = "0.10.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6cbce692ab4ca2f1f3047fcf732430249c0e971bfdd2b234cf2c47ad93af5983"
|
||||
dependencies = [
|
||||
"arrayvec 0.5.2",
|
||||
"arrayvec",
|
||||
"utf8parse",
|
||||
"vte_generate_state_changes",
|
||||
]
|
||||
|
|
|
@ -13,7 +13,6 @@ exclude = [
|
|||
[workspace.dependencies]
|
||||
anyhow = "1.0.47"
|
||||
base64 = "0.21.0"
|
||||
blake3 = "1.3.3"
|
||||
bytesize = "1.0"
|
||||
cargo = { path = "" }
|
||||
cargo-credential = { version = "0.2.0", path = "credential/cargo-credential" }
|
||||
|
@ -114,7 +113,6 @@ path = "src/cargo/lib.rs"
|
|||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
base64.workspace = true
|
||||
blake3.workspace = true
|
||||
bytesize.workspace = true
|
||||
cargo-platform.workspace = true
|
||||
cargo-util.workspace = true
|
||||
|
|
|
@ -106,7 +106,6 @@ allow = [
|
|||
"MIT-0",
|
||||
"Apache-2.0",
|
||||
"BSD-3-Clause",
|
||||
"BSD-2-Clause",
|
||||
"MPL-2.0",
|
||||
"Unicode-DFS-2016",
|
||||
"CC0-1.0",
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::path::Path;
|
|||
use crate::command_prelude::*;
|
||||
use crate::util::restricted_names::is_glob_pattern;
|
||||
use cargo::core::Verbosity;
|
||||
use cargo::core::Workspace;
|
||||
use cargo::ops::{self, CompileFilter, Packages};
|
||||
use cargo_util::ProcessError;
|
||||
|
||||
|
@ -95,14 +96,19 @@ pub fn exec_manifest_command(config: &Config, cmd: &str, args: &[OsString]) -> C
|
|||
}
|
||||
|
||||
let manifest_path = Path::new(cmd);
|
||||
let manifest_path = config.cwd().join(manifest_path);
|
||||
let manifest_path = cargo_util::paths::normalize_path(&manifest_path);
|
||||
if !manifest_path.exists() {
|
||||
return Err(
|
||||
anyhow::anyhow!("manifest `{}` does not exist", manifest_path.display()).into(),
|
||||
);
|
||||
return Err(anyhow::format_err!(
|
||||
"manifest path `{}` does not exist",
|
||||
manifest_path.display()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let mut ws = Workspace::new(&manifest_path, config)?;
|
||||
if config.cli_unstable().avoid_dev_deps {
|
||||
ws.set_require_optional_deps(false);
|
||||
}
|
||||
let manifest_path = crate::util::try_canonicalize(manifest_path)?;
|
||||
let script = cargo::util::toml::embedded::parse_from(&manifest_path)?;
|
||||
let ws = cargo::util::toml::embedded::to_workspace(&script, config)?;
|
||||
|
||||
let mut compile_opts =
|
||||
cargo::ops::CompileOptions::new(config, cargo::core::compiler::CompileMode::Build)?;
|
||||
|
|
|
@ -64,6 +64,7 @@ pub struct Manifest {
|
|||
metabuild: Option<Vec<String>>,
|
||||
resolve_behavior: Option<ResolveBehavior>,
|
||||
lint_rustflags: Vec<String>,
|
||||
embedded: bool,
|
||||
}
|
||||
|
||||
/// When parsing `Cargo.toml`, some warnings should silenced
|
||||
|
@ -407,6 +408,7 @@ impl Manifest {
|
|||
metabuild: Option<Vec<String>>,
|
||||
resolve_behavior: Option<ResolveBehavior>,
|
||||
lint_rustflags: Vec<String>,
|
||||
embedded: bool,
|
||||
) -> Manifest {
|
||||
Manifest {
|
||||
summary,
|
||||
|
@ -433,6 +435,7 @@ impl Manifest {
|
|||
metabuild,
|
||||
resolve_behavior,
|
||||
lint_rustflags,
|
||||
embedded,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -500,6 +503,9 @@ impl Manifest {
|
|||
pub fn links(&self) -> Option<&str> {
|
||||
self.links.as_deref()
|
||||
}
|
||||
pub fn is_embedded(&self) -> bool {
|
||||
self.embedded
|
||||
}
|
||||
|
||||
pub fn workspace_config(&self) -> &WorkspaceConfig {
|
||||
&self.workspace
|
||||
|
|
|
@ -726,6 +726,10 @@ impl<'cfg> Workspace<'cfg> {
|
|||
if self.members.contains(&manifest_path) {
|
||||
return Ok(());
|
||||
}
|
||||
if is_path_dep && self.root_maybe().is_embedded() {
|
||||
// Embedded manifests cannot have workspace members
|
||||
return Ok(());
|
||||
}
|
||||
if is_path_dep
|
||||
&& !manifest_path.parent().unwrap().starts_with(self.root())
|
||||
&& self.find_root(&manifest_path)? != self.root_manifest
|
||||
|
@ -1580,6 +1584,13 @@ impl MaybePackage {
|
|||
MaybePackage::Virtual(ref vm) => vm.workspace_config(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_embedded(&self) -> bool {
|
||||
match self {
|
||||
MaybePackage::Package(p) => p.manifest().is_embedded(),
|
||||
MaybePackage::Virtual(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WorkspaceRootConfig {
|
||||
|
|
|
@ -393,7 +393,7 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
|
|||
let package_root = orig_pkg.root();
|
||||
let source_id = orig_pkg.package_id().source_id();
|
||||
let (manifest, _nested_paths) =
|
||||
TomlManifest::to_real_manifest(&toml_manifest, source_id, package_root, config)?;
|
||||
TomlManifest::to_real_manifest(&toml_manifest, false, source_id, package_root, config)?;
|
||||
let new_pkg = Package::new(manifest, orig_pkg.manifest_path());
|
||||
|
||||
// Regenerate Cargo.lock using the old one as a guide.
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use anyhow::Context as _;
|
||||
|
||||
use crate::core::Workspace;
|
||||
use crate::util::restricted_names;
|
||||
use crate::CargoResult;
|
||||
use crate::Config;
|
||||
|
@ -9,22 +8,15 @@ const DEFAULT_EDITION: crate::core::features::Edition =
|
|||
crate::core::features::Edition::LATEST_STABLE;
|
||||
const DEFAULT_VERSION: &str = "0.0.0";
|
||||
const DEFAULT_PUBLISH: bool = false;
|
||||
const AUTO_FIELDS: &[&str] = &["autobins", "autoexamples", "autotests", "autobenches"];
|
||||
|
||||
pub struct RawScript {
|
||||
manifest: String,
|
||||
body: String,
|
||||
path: std::path::PathBuf,
|
||||
}
|
||||
|
||||
pub fn parse_from(path: &std::path::Path) -> CargoResult<RawScript> {
|
||||
let body = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("failed to script at {}", path.display()))?;
|
||||
parse(&body, path)
|
||||
}
|
||||
|
||||
fn parse(body: &str, path: &std::path::Path) -> CargoResult<RawScript> {
|
||||
let comment = match extract_comment(body) {
|
||||
Ok(manifest) => Some(manifest),
|
||||
pub fn expand_manifest(
|
||||
content: &str,
|
||||
path: &std::path::Path,
|
||||
config: &Config,
|
||||
) -> CargoResult<String> {
|
||||
let comment = match extract_comment(content) {
|
||||
Ok(comment) => Some(comment),
|
||||
Err(err) => {
|
||||
log::trace!("failed to extract doc comment: {err}");
|
||||
None
|
||||
|
@ -39,82 +31,18 @@ fn parse(body: &str, path: &std::path::Path) -> CargoResult<RawScript> {
|
|||
}
|
||||
}
|
||||
.unwrap_or_default();
|
||||
let body = body.to_owned();
|
||||
let path = path.to_owned();
|
||||
Ok(RawScript {
|
||||
manifest,
|
||||
body,
|
||||
path,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_workspace<'cfg>(
|
||||
script: &RawScript,
|
||||
config: &'cfg Config,
|
||||
) -> CargoResult<Workspace<'cfg>> {
|
||||
let target_dir = config
|
||||
.target_dir()
|
||||
.transpose()
|
||||
.unwrap_or_else(|| default_target_dir().map(crate::util::Filesystem::new))?;
|
||||
// HACK: without cargo knowing about embedded manifests, the only way to create a
|
||||
// `Workspace` is either
|
||||
// - Create a temporary one on disk
|
||||
// - Create an "ephemeral" workspace **but** compilation re-loads ephemeral workspaces
|
||||
// from the registry rather than what we already have on memory, causing it to fail
|
||||
// because the registry doesn't know about embedded manifests.
|
||||
let manifest_path = write(script, config, target_dir.as_path_unlocked())?;
|
||||
let workspace = Workspace::new(&manifest_path, config)?;
|
||||
Ok(workspace)
|
||||
}
|
||||
|
||||
fn write(
|
||||
script: &RawScript,
|
||||
config: &Config,
|
||||
target_dir: &std::path::Path,
|
||||
) -> CargoResult<std::path::PathBuf> {
|
||||
let hash = hash(script).to_string();
|
||||
assert_eq!(hash.len(), 64);
|
||||
|
||||
let file_name = script
|
||||
.path
|
||||
.file_stem()
|
||||
.ok_or_else(|| anyhow::format_err!("no file name"))?
|
||||
.to_string_lossy();
|
||||
let name = sanitize_name(file_name.as_ref());
|
||||
|
||||
let mut workspace_root = target_dir.to_owned();
|
||||
workspace_root.push("eval");
|
||||
workspace_root.push(&hash[0..2]);
|
||||
workspace_root.push(&hash[2..4]);
|
||||
workspace_root.push(&hash[4..]);
|
||||
workspace_root.push(name);
|
||||
std::fs::create_dir_all(&workspace_root).with_context(|| {
|
||||
format!(
|
||||
"failed to create temporary workspace at {}",
|
||||
workspace_root.display()
|
||||
)
|
||||
})?;
|
||||
let manifest_path = workspace_root.join("Cargo.toml");
|
||||
let manifest = expand_manifest(script, config)?;
|
||||
write_if_changed(&manifest_path, &manifest)?;
|
||||
Ok(manifest_path)
|
||||
}
|
||||
|
||||
fn expand_manifest(script: &RawScript, config: &Config) -> CargoResult<String> {
|
||||
let manifest = expand_manifest_(script, config)
|
||||
.with_context(|| format!("failed to parse manifest at {}", script.path.display()))?;
|
||||
let manifest = remap_paths(
|
||||
manifest,
|
||||
script.path.parent().ok_or_else(|| {
|
||||
anyhow::format_err!("no parent directory for {}", script.path.display())
|
||||
})?,
|
||||
)?;
|
||||
let manifest = expand_manifest_(&manifest, path, config)
|
||||
.with_context(|| format!("failed to parse manifest at {}", path.display()))?;
|
||||
let manifest = toml::to_string_pretty(&manifest)?;
|
||||
Ok(manifest)
|
||||
}
|
||||
|
||||
fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Table> {
|
||||
let mut manifest: toml::Table = toml::from_str(&script.manifest)?;
|
||||
fn expand_manifest_(
|
||||
manifest: &str,
|
||||
path: &std::path::Path,
|
||||
config: &Config,
|
||||
) -> CargoResult<toml::Table> {
|
||||
let mut manifest: toml::Table = toml::from_str(&manifest)?;
|
||||
|
||||
for key in ["workspace", "lib", "bin", "example", "test", "bench"] {
|
||||
if manifest.contains_key(key) {
|
||||
|
@ -130,17 +58,23 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
|
|||
.or_insert_with(|| toml::Table::new().into())
|
||||
.as_table_mut()
|
||||
.ok_or_else(|| anyhow::format_err!("`package` must be a table"))?;
|
||||
for key in ["workspace", "build", "links"] {
|
||||
if package.contains_key(key) {
|
||||
for key in ["workspace", "build", "links"]
|
||||
.iter()
|
||||
.chain(AUTO_FIELDS.iter())
|
||||
{
|
||||
if package.contains_key(*key) {
|
||||
anyhow::bail!("`package.{key}` is not allowed in embedded manifests")
|
||||
}
|
||||
}
|
||||
let file_name = script
|
||||
.path
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.ok_or_else(|| anyhow::format_err!("no file name"))?
|
||||
.to_string_lossy();
|
||||
let file_stem = path
|
||||
.file_stem()
|
||||
.ok_or_else(|| anyhow::format_err!("no file name"))?
|
||||
.to_string_lossy();
|
||||
let name = sanitize_name(file_name.as_ref());
|
||||
let name = sanitize_name(file_stem.as_ref());
|
||||
let bin_name = name.clone();
|
||||
package
|
||||
.entry("name".to_owned())
|
||||
|
@ -158,18 +92,17 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
|
|||
package
|
||||
.entry("publish".to_owned())
|
||||
.or_insert_with(|| toml::Value::Boolean(DEFAULT_PUBLISH));
|
||||
for field in AUTO_FIELDS {
|
||||
package
|
||||
.entry(field.to_owned())
|
||||
.or_insert_with(|| toml::Value::Boolean(false));
|
||||
}
|
||||
|
||||
let mut bin = toml::Table::new();
|
||||
bin.insert("name".to_owned(), toml::Value::String(bin_name));
|
||||
bin.insert(
|
||||
"path".to_owned(),
|
||||
toml::Value::String(
|
||||
script
|
||||
.path
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow::format_err!("path is not valid UTF-8"))?
|
||||
.into(),
|
||||
),
|
||||
toml::Value::String(file_name.into_owned()),
|
||||
);
|
||||
manifest.insert(
|
||||
"bin".to_owned(),
|
||||
|
@ -223,28 +156,6 @@ fn sanitize_name(name: &str) -> String {
|
|||
name
|
||||
}
|
||||
|
||||
fn hash(script: &RawScript) -> blake3::Hash {
|
||||
blake3::hash(script.body.as_bytes())
|
||||
}
|
||||
|
||||
fn default_target_dir() -> CargoResult<std::path::PathBuf> {
|
||||
let mut cargo_home = home::cargo_home()?;
|
||||
cargo_home.push("eval");
|
||||
cargo_home.push("target");
|
||||
Ok(cargo_home)
|
||||
}
|
||||
|
||||
fn write_if_changed(path: &std::path::Path, new: &str) -> CargoResult<()> {
|
||||
let write_needed = match std::fs::read_to_string(path) {
|
||||
Ok(current) => current != new,
|
||||
Err(_) => true,
|
||||
};
|
||||
if write_needed {
|
||||
std::fs::write(path, new).with_context(|| format!("failed to write {}", path.display()))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Locates a "code block manifest" in Rust source.
|
||||
fn extract_comment(input: &str) -> CargoResult<String> {
|
||||
let mut doc_fragments = Vec::new();
|
||||
|
@ -559,10 +470,12 @@ mod test_expand {
|
|||
|
||||
macro_rules! si {
|
||||
($i:expr) => {{
|
||||
let script = parse($i, std::path::Path::new("/home/me/test.rs"))
|
||||
.unwrap_or_else(|err| panic!("{}", err));
|
||||
expand_manifest(&script, &Config::default().unwrap())
|
||||
.unwrap_or_else(|err| panic!("{}", err))
|
||||
expand_manifest(
|
||||
$i,
|
||||
std::path::Path::new("/home/me/test.rs"),
|
||||
&Config::default().unwrap(),
|
||||
)
|
||||
.unwrap_or_else(|err| panic!("{}", err))
|
||||
}};
|
||||
}
|
||||
|
||||
|
@ -571,9 +484,13 @@ mod test_expand {
|
|||
snapbox::assert_eq(
|
||||
r#"[[bin]]
|
||||
name = "test-"
|
||||
path = "/home/me/test.rs"
|
||||
path = "test.rs"
|
||||
|
||||
[package]
|
||||
autobenches = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
edition = "2021"
|
||||
name = "test-"
|
||||
publish = false
|
||||
|
@ -593,12 +510,16 @@ strip = true
|
|||
snapbox::assert_eq(
|
||||
r#"[[bin]]
|
||||
name = "test-"
|
||||
path = "/home/me/test.rs"
|
||||
path = "test.rs"
|
||||
|
||||
[dependencies]
|
||||
time = "0.1.25"
|
||||
|
||||
[package]
|
||||
autobenches = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
edition = "2021"
|
||||
name = "test-"
|
||||
publish = false
|
||||
|
@ -858,73 +779,6 @@ fn main () {
|
|||
}
|
||||
}
|
||||
|
||||
/// Given a Cargo manifest, attempts to rewrite relative file paths to absolute ones, allowing the manifest to be relocated.
|
||||
fn remap_paths(
|
||||
mani: toml::Table,
|
||||
package_root: &std::path::Path,
|
||||
) -> anyhow::Result<toml::value::Table> {
|
||||
// Values that need to be rewritten:
|
||||
let paths: &[&[&str]] = &[
|
||||
&["build-dependencies", "*", "path"],
|
||||
&["dependencies", "*", "path"],
|
||||
&["dev-dependencies", "*", "path"],
|
||||
&["package", "build"],
|
||||
&["target", "*", "dependencies", "*", "path"],
|
||||
];
|
||||
|
||||
let mut mani = toml::Value::Table(mani);
|
||||
|
||||
for path in paths {
|
||||
iterate_toml_mut_path(&mut mani, path, &mut |v| {
|
||||
if let toml::Value::String(s) = v {
|
||||
if std::path::Path::new(s).is_relative() {
|
||||
let p = package_root.join(&*s);
|
||||
if let Some(p) = p.to_str() {
|
||||
*s = p.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?
|
||||
}
|
||||
|
||||
match mani {
|
||||
toml::Value::Table(mani) => Ok(mani),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterates over the specified TOML values via a path specification.
|
||||
fn iterate_toml_mut_path<F>(
|
||||
base: &mut toml::Value,
|
||||
path: &[&str],
|
||||
on_each: &mut F,
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
F: FnMut(&mut toml::Value) -> anyhow::Result<()>,
|
||||
{
|
||||
if path.is_empty() {
|
||||
return on_each(base);
|
||||
}
|
||||
|
||||
let cur = path[0];
|
||||
let tail = &path[1..];
|
||||
|
||||
if cur == "*" {
|
||||
if let toml::Value::Table(tab) = base {
|
||||
for (_, v) in tab {
|
||||
iterate_toml_mut_path(v, tail, on_each)?;
|
||||
}
|
||||
}
|
||||
} else if let toml::Value::Table(tab) = base {
|
||||
if let Some(v) = tab.get_mut(cur) {
|
||||
iterate_toml_mut_path(v, tail, on_each)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_manifest {
|
||||
use super::*;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||||
use std::ffi::OsStr;
|
||||
use std::fmt::{self, Display, Write};
|
||||
use std::marker::PhantomData;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
@ -55,13 +56,29 @@ pub fn read_manifest(
|
|||
path.display(),
|
||||
source_id
|
||||
);
|
||||
let contents = paths::read(path).map_err(|err| ManifestError::new(err, path.into()))?;
|
||||
let mut contents = paths::read(path).map_err(|err| ManifestError::new(err, path.into()))?;
|
||||
let embedded = is_embedded(path);
|
||||
if embedded {
|
||||
if !config.cli_unstable().script {
|
||||
return Err(ManifestError::new(
|
||||
anyhow::anyhow!("parsing `{}` requires `-Zscript`", path.display()),
|
||||
path.into(),
|
||||
));
|
||||
}
|
||||
contents = embedded::expand_manifest(&contents, path, config)
|
||||
.map_err(|err| ManifestError::new(err, path.into()))?;
|
||||
}
|
||||
|
||||
read_manifest_from_str(&contents, path, source_id, config)
|
||||
read_manifest_from_str(&contents, path, embedded, source_id, config)
|
||||
.with_context(|| format!("failed to parse manifest at `{}`", path.display()))
|
||||
.map_err(|err| ManifestError::new(err, path.into()))
|
||||
}
|
||||
|
||||
fn is_embedded(path: &Path) -> bool {
|
||||
let ext = path.extension();
|
||||
ext.is_none() || ext == Some(OsStr::new("rs"))
|
||||
}
|
||||
|
||||
/// Parse an already-loaded `Cargo.toml` as a Cargo manifest.
|
||||
///
|
||||
/// This could result in a real or virtual manifest being returned.
|
||||
|
@ -70,9 +87,10 @@ pub fn read_manifest(
|
|||
/// within the manifest. For virtual manifests, these paths can only
|
||||
/// come from patched or replaced dependencies. These paths are not
|
||||
/// canonicalized.
|
||||
pub fn read_manifest_from_str(
|
||||
fn read_manifest_from_str(
|
||||
contents: &str,
|
||||
manifest_file: &Path,
|
||||
embedded: bool,
|
||||
source_id: SourceId,
|
||||
config: &Config,
|
||||
) -> CargoResult<(EitherManifest, Vec<PathBuf>)> {
|
||||
|
@ -128,7 +146,7 @@ pub fn read_manifest_from_str(
|
|||
}
|
||||
return if manifest.project.is_some() || manifest.package.is_some() {
|
||||
let (mut manifest, paths) =
|
||||
TomlManifest::to_real_manifest(&manifest, source_id, package_root, config)?;
|
||||
TomlManifest::to_real_manifest(&manifest, embedded, source_id, package_root, config)?;
|
||||
add_unused(manifest.warnings_mut());
|
||||
if manifest.targets().iter().all(|t| t.is_custom_build()) {
|
||||
bail!(
|
||||
|
@ -1976,6 +1994,7 @@ impl TomlManifest {
|
|||
|
||||
pub fn to_real_manifest(
|
||||
me: &Rc<TomlManifest>,
|
||||
embedded: bool,
|
||||
source_id: SourceId,
|
||||
package_root: &Path,
|
||||
config: &Config,
|
||||
|
@ -2644,6 +2663,7 @@ impl TomlManifest {
|
|||
package.metabuild.clone().map(|sov| sov.0),
|
||||
resolve_behavior,
|
||||
rustflags,
|
||||
embedded,
|
||||
);
|
||||
if package.license_file.is_some() && package.license.is_some() {
|
||||
manifest.warnings_mut().add_warning(
|
||||
|
|
|
@ -26,16 +26,16 @@ fn basic_rs() {
|
|||
p.cargo("-Zscript echo.rs")
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"bin: [ROOT]/home/.cargo/eval/target/eval/[..]
|
||||
r#"bin: [..]/debug/echo[EXE]
|
||||
args: []
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] echo v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/echo)
|
||||
[COMPILING] echo v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/echo/target/debug/echo[EXE]`
|
||||
[RUNNING] `[..]debug/echo[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -50,16 +50,16 @@ fn basic_path() {
|
|||
p.cargo("-Zscript ./echo")
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"bin: [ROOT]/home/.cargo/eval/target/eval/[..]
|
||||
r#"bin: [..]/debug/echo[EXE]
|
||||
args: []
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] echo v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/echo)
|
||||
[COMPILING] echo v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/echo/target/debug/echo[EXE]`
|
||||
[RUNNING] `[..]/debug/echo[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -104,16 +104,16 @@ fn manifest_precedence_over_plugins() {
|
|||
.env("PATH", &path)
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"bin: [ROOT]/home/.cargo/eval/target/eval/[..]
|
||||
r#"bin: [..]/debug/echo[EXE]
|
||||
args: []
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] echo v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/echo)
|
||||
[COMPILING] echo v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/echo/target/debug/echo[EXE]`
|
||||
[RUNNING] `[..]/debug/echo[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -203,9 +203,9 @@ fn main() {
|
|||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE]`
|
||||
[RUNNING] `[..]/debug/script[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -235,9 +235,9 @@ fn main() {
|
|||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE]`
|
||||
[RUNNING] `[..]/debug/script[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -264,9 +264,9 @@ fn main() {
|
|||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE]`
|
||||
[RUNNING] `[..]/debug/script[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -282,7 +282,7 @@ fn main() {
|
|||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE]`
|
||||
[RUNNING] `[..]/debug/script[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -298,9 +298,9 @@ fn main() {
|
|||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE]`
|
||||
[RUNNING] `[..]/debug/script[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -327,9 +327,9 @@ fn main() {
|
|||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE]`
|
||||
[RUNNING] `[..]/debug/script[EXE]`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -345,16 +345,16 @@ fn test_escaped_hyphen_arg() {
|
|||
p.cargo("-Zscript -- script.rs -NotAnArg")
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"bin: [ROOT]/home/.cargo/eval/target/eval/[..]
|
||||
r#"bin: [..]/debug/script[EXE]
|
||||
args: ["-NotAnArg"]
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE] -NotAnArg`
|
||||
[RUNNING] `[..]/debug/script[EXE] -NotAnArg`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -370,16 +370,16 @@ fn test_unescaped_hyphen_arg() {
|
|||
p.cargo("-Zscript script.rs -NotAnArg")
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"bin: [ROOT]/home/.cargo/eval/target/eval/[..]
|
||||
r#"bin: [..]/debug/script[EXE]
|
||||
args: ["-NotAnArg"]
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE] -NotAnArg`
|
||||
[RUNNING] `[..]/debug/script[EXE] -NotAnArg`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -395,16 +395,16 @@ fn test_same_flags() {
|
|||
p.cargo("-Zscript script.rs --help")
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"bin: [ROOT]/home/.cargo/eval/target/eval/[..]
|
||||
r#"bin: [..]/debug/script[EXE]
|
||||
args: ["--help"]
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE] --help`
|
||||
[RUNNING] `[..]/debug/script[EXE] --help`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -420,15 +420,15 @@ fn test_name_has_weird_chars() {
|
|||
p.cargo("-Zscript s-h.w§c!.rs")
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"bin: [ROOT]/home/.cargo/eval/target/eval/[..]
|
||||
r#"bin: [..]/debug/s-h-w-c-[EXE]
|
||||
args: []
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] s-h-w-c- v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/s-h-w-c-)
|
||||
[COMPILING] s-h-w-c- v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/s-h-w-c-/target/debug/s-h-w-c-[EXE]`
|
||||
[RUNNING] `[..]/debug/s-h-w-c-[EXE]`
|
||||
"#,
|
||||
)
|
||||
.run();
|
||||
|
@ -464,9 +464,9 @@ fn main() {
|
|||
[DOWNLOADING] crates ...
|
||||
[DOWNLOADED] script v1.0.0 (registry `dummy-registry`)
|
||||
[COMPILING] script v1.0.0
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE] --help`
|
||||
[RUNNING] `[..]/debug/script[EXE] --help`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
@ -501,9 +501,38 @@ fn main() {
|
|||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] bar v0.0.1 ([ROOT]/foo/bar)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/home/.cargo/eval/target/eval/[..]/script)
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[ROOT]/home/.cargo/eval/target/eval/[..]/script/target/debug/script[EXE] --help`
|
||||
[RUNNING] `[..]/debug/script[EXE] --help`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
#[cargo_test]
|
||||
fn test_no_autobins() {
|
||||
let script = r#"#!/usr/bin/env cargo
|
||||
|
||||
fn main() {
|
||||
println!("Hello world!");
|
||||
}"#;
|
||||
let p = cargo_test_support::project()
|
||||
.file("script.rs", script)
|
||||
.file("src/bin/not-script/main.rs", "fn main() {}")
|
||||
.build();
|
||||
|
||||
p.cargo("-Zscript script.rs --help")
|
||||
.masquerade_as_nightly_cargo(&["script"])
|
||||
.with_stdout(
|
||||
r#"Hello world!
|
||||
"#,
|
||||
)
|
||||
.with_stderr(
|
||||
"\
|
||||
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
|
||||
[COMPILING] script v0.0.0 ([ROOT]/foo)
|
||||
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
|
||||
[RUNNING] `[..]/debug/script[EXE] --help`
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
|
Loading…
Reference in a new issue