Blob Blame History Raw
From 55998b3bd2d4b62e8f09d3e9c7661937c96ddb69 Mon Sep 17 00:00:00 2001
From: Fabio Valentini <decathorpe@gmail.com>
Date: Jul 26 2022 10:13:47 +0000
Subject: Ensure both generated and manual patches affect spec generation


Previous to this change, if there were no manual changes, the automatically
generated patch would not affect the calculated metadata, and result in
broken .spec files.

---

diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py
index c103643..9b44f02 100644
--- a/rust2rpm/__main__.py
+++ b/rust2rpm/__main__.py
@@ -272,21 +272,32 @@ def make_patches(args, version, toml):
     diff1 = diff2 = None
     toml_path = f"{args.crate}-{version}/Cargo.toml"
 
-    # We always do the automatic part before asking the user for more edits.
-    if toml_after := args.patch_foreign and drop_foreign_dependencies(toml_before):
-        diff1 = make_diff(toml_path, toml_before, mtime_before, toml_after, mtime_before)
-    else:
-        toml_after = toml_before
+    # patching Cargo.toml involves multiple steps:
+    #
+    # 1) attempt to automatically drop "foreign" dependencies
+    #    If this succeeded, remove the original Cargo.toml file, write the
+    #    changed contents back to disk, and generate a diff.
+    # 2) if requested, open Cargo.toml in an editor for further, manual changes
+    #
+    # The changes from *both* steps must be reflected in the Cargo.toml file
+    # that ends up on disk after this function returns. Otherwise, the
+    # calculated metadata and generated spec file will not reflect the patches
+    # to Cargo.toml that were generated here.
 
-    if args.patch:
+    if toml_after := args.patch_foreign and drop_foreign_dependencies(toml_before):
         # remove original Cargo.toml file
         os.remove(toml)
 
         # write auto-patched Cargo.toml to disk
         with open(toml, "w") as file:
             file.writelines(toml_after)
-            file.flush()
 
+        diff1 = make_diff(toml_path, toml_before, mtime_before, toml_after, mtime_before)
+    else:
+        toml_after = toml_before
+
+    if args.patch:
+        # open editor for Cargo.toml
         editor = util.detect_editor()
         subprocess.check_call([editor, toml])
 
diff --git a/rust2rpm/cfg.py b/rust2rpm/cfg.py
index 51e5ff9..cd164db 100644
--- a/rust2rpm/cfg.py
+++ b/rust2rpm/cfg.py
@@ -116,8 +116,9 @@ def evaluate_atom(name: str) -> bool:
             return False
         case _:
             log.warn(
-                f"Ignoring invalid atom {name!r} in cfg-expression. "
-                + 'Only "unix" and "windows" are valid names for atoms.'
+                f"Ignoring unknown identifier {name!r} in cfg-expression. "
+                + 'Only "unix" and "windows" are standard identifiers, '
+                + 'any non-standard "--cfg" flags are not supported.'
             )
             return False