From e90513f1d562b58568715398310c9f3b5641a24d Mon Sep 17 00:00:00 2001 From: messense Date: Wed, 23 Jun 2021 12:03:23 +0800 Subject: [PATCH 1/2] Replace `toml_edit` with `toml` crate --- Cargo.lock | 41 -------------------------------------- Cargo.toml | 1 - Changelog.md | 1 + src/source_distribution.rs | 17 +++++++++------- 4 files changed, 11 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42ed312ec..c67e37cb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -267,19 +267,6 @@ dependencies = [ "encoding_rs", ] -[[package]] -name = "chrono" -version = "0.4.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" -dependencies = [ - "libc", - "num-integer", - "num-traits", - "time", - "winapi", -] - [[package]] name = "cipher" version = "0.2.5" @@ -304,16 +291,6 @@ dependencies = [ "vec_map", ] -[[package]] -name = "combine" -version = "4.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2d47c1b11006b87e492b53b313bb699ce60e16613c4dddaa91f8f7c220ab2fa" -dependencies = [ - "bytes", - "memchr", -] - [[package]] name = "configparser" version = "2.1.0" @@ -846,12 +823,6 @@ version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" -[[package]] -name = "linked-hash-map" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" - [[package]] name = "llvm-bitcode" version = "0.1.2" @@ -925,7 +896,6 @@ dependencies = [ "tempfile", "thiserror", "toml", - "toml_edit", "walkdir", "zip", ] @@ -1878,17 +1848,6 @@ dependencies = [ "serde", ] -[[package]] -name = "toml_edit" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbbdcf4f749dd33b1f1ea19b547bf789d87442ec40767d6015e5e2d39158d69a" -dependencies = [ - "chrono", - "combine", - "linked-hash-map", -] - [[package]] name = "tower-service" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 5f66bbbb6..d1d3b2844 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,6 @@ dirs = { version = "3.0.1", optional = true } configparser = { version = "2.0.0", optional = true } fs-err = "2.5.0" fat-macho = "0.4.3" -toml_edit = "0.2.0" once_cell = "1.7.2" scroll = "0.10.2" target-lexicon = "0.12.0" diff --git a/Changelog.md b/Changelog.md index 7a79d4946..bab800b73 100644 --- a/Changelog.md +++ b/Changelog.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Support Aarch64 on OpenBSD in [#570](https://github.com/PyO3/maturin/pull/570) * Support Aarch64 on FreeBSD in [#571](https://github.com/PyO3/maturin/pull/571) * `Cargo.toml`'s `authors` field is now optional per Rust [RFC 3052](https://github.com/rust-lang/rfcs/blob/master/text/3052-optional-authors-field.md) in [#573](https://github.com/PyO3/maturin/pull/573) +* Allow dotted keys in `Cargo.toml` by switch from `toml_edit` to `toml` crate in [#577](https://github.com/PyO3/maturin/pull/577) * Fix source distribution with local path dependencies on Windows in [#580](https://github.com/PyO3/maturin/pull/580) ## 0.10.6 - 2021-05-21 diff --git a/src/source_distribution.rs b/src/source_distribution.rs index 148a554a2..9c2723aab 100644 --- a/src/source_distribution.rs +++ b/src/source_distribution.rs @@ -14,7 +14,7 @@ const LOCAL_DEPENDENCIES_FOLDER: &str = "local_dependencies"; /// distribution. Since there is no cargo-backed way to replace dependencies /// (see https://github.com/rust-lang/cargo/issues/9170), we do a simple /// Cargo.toml rewrite ourselves. -/// A big chunk of that (including toml_edit) comes from cargo edit, and esp. +/// A big chunk of that comes from cargo edit, and esp. /// https://github.com/killercup/cargo-edit/blob/2a08f0311bcb61690d71d39cb9e55e69b256c8e1/src/manifest.rs /// This method is rather frail, but unfortunately I don't know a better solution. fn rewrite_cargo_toml( @@ -26,7 +26,7 @@ fn rewrite_cargo_toml( "Can't read Cargo.toml at {}", manifest_path.as_ref().display(), ))?; - let mut data = text.parse::().context(format!( + let mut data = toml::from_str::(&text).context(format!( "Failed to parse Cargo.toml at {}", manifest_path.as_ref().display() ))?; @@ -36,11 +36,14 @@ fn rewrite_cargo_toml( // ^^^^^^^^^^^^^^^^^^ table[&dep_name]["path"] // ^^^^^^^^^^^^^ dep_name for dep_category in &["dependencies", "dev-dependencies", "build-dependencies"] { - if let Some(table) = data[&dep_category].as_table_mut() { + if let Some(table) = data + .get_mut(&dep_category.to_string()) + .and_then(|x| x.as_table_mut()) + { let dep_names: Vec<_> = table.iter().map(|(key, _)| key.to_string()).collect(); for dep_name in dep_names { // There should either be no value for path, or it should be a string - if table[&dep_name]["path"].is_none() { + if table.get(&dep_name).and_then(|x| x.get("path")).is_none() { continue; } if !table[&dep_name]["path"].is_str() { @@ -53,10 +56,10 @@ fn rewrite_cargo_toml( } // This is the location of the targeted crate in the source distribution table[&dep_name]["path"] = if root_crate { - toml_edit::value(format!("{}/{}", LOCAL_DEPENDENCIES_FOLDER, dep_name)) + format!("{}/{}", LOCAL_DEPENDENCIES_FOLDER, dep_name).into() } else { // Cargo.toml contains relative paths, and we're already in LOCAL_DEPENDENCIES_FOLDER - toml_edit::value(format!("../{}", dep_name)) + format!("../{}", dep_name).into() }; if !known_path_deps.contains_key(&dep_name) { bail!( @@ -70,7 +73,7 @@ fn rewrite_cargo_toml( } } } - Ok(data.to_string_in_original_order()) + Ok(toml::to_string(&data)?) } /// Copies the files of a crate to a source distribution, recursively adding path dependencies From 19d44fb317b432e7a8bf4b0e0638f0beebbe311a Mon Sep 17 00:00:00 2001 From: messense Date: Wed, 23 Jun 2021 13:13:14 +0800 Subject: [PATCH 2/2] Only rewrite `Cargo.toml` when there are path dependencies --- src/source_distribution.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/source_distribution.rs b/src/source_distribution.rs index 9c2723aab..7f232ecfe 100644 --- a/src/source_distribution.rs +++ b/src/source_distribution.rs @@ -30,16 +30,14 @@ fn rewrite_cargo_toml( "Failed to parse Cargo.toml at {}", manifest_path.as_ref().display() ))?; + let mut rewritten = false; // ˇˇˇˇˇˇˇˇˇˇˇˇ dep_category // [dependencies] // some_path_dep = { path = "../some_path_dep" } // ^^^^^^^^^^^^^^^^^^ table[&dep_name]["path"] // ^^^^^^^^^^^^^ dep_name for dep_category in &["dependencies", "dev-dependencies", "build-dependencies"] { - if let Some(table) = data - .get_mut(&dep_category.to_string()) - .and_then(|x| x.as_table_mut()) - { + if let Some(table) = data.get_mut(*dep_category).and_then(|x| x.as_table_mut()) { let dep_names: Vec<_> = table.iter().map(|(key, _)| key.to_string()).collect(); for dep_name in dep_names { // There should either be no value for path, or it should be a string @@ -61,6 +59,7 @@ fn rewrite_cargo_toml( // Cargo.toml contains relative paths, and we're already in LOCAL_DEPENDENCIES_FOLDER format!("../{}", dep_name).into() }; + rewritten = true; if !known_path_deps.contains_key(&dep_name) { bail!( "cargo metadata does not know about the path for {}.{} present in {}, \ @@ -73,7 +72,11 @@ fn rewrite_cargo_toml( } } } - Ok(toml::to_string(&data)?) + if rewritten { + Ok(toml::to_string(&data)?) + } else { + Ok(text) + } } /// Copies the files of a crate to a source distribution, recursively adding path dependencies