From 30a786c9a1afcf300a9d8fc87b4f363556f9878c Mon Sep 17 00:00:00 2001 From: beeb <703631+beeb@users.noreply.github.com> Date: Tue, 2 Sep 2025 11:56:49 +0200 Subject: [PATCH 1/2] replace-workspace-values.py: fix missing key exception If a cargo workspace member defines a key with `.workpace = true` but this key does not exist in the workspace manifest, the script was raising an exception. The case is now handled properly. --- .../rust/replace-workspace-values.py | 17 +++++++++++++++-- .../crate_missing_field.toml | 13 +++++++++++++ .../default.nix | 4 ++++ .../want_missing_field.toml | 12 ++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/crate_missing_field.toml create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/want_missing_field.toml diff --git a/pkgs/build-support/rust/replace-workspace-values.py b/pkgs/build-support/rust/replace-workspace-values.py index 708ce14a7e8f..1130c67ced75 100644 --- a/pkgs/build-support/rust/replace-workspace-values.py +++ b/pkgs/build-support/rust/replace-workspace-values.py @@ -31,7 +31,12 @@ def replace_key( local_dep = table[key] del local_dep["workspace"] - workspace_dep = workspace_manifest[section][key] + try: + workspace_dep = workspace_manifest[section][key] + except KeyError: + # Key is not present in workspace manifest, we can't inherit the value, so we mark it for deletion + table[key] = {} + return True if section == "dependencies": if isinstance(workspace_dep, str): @@ -104,10 +109,18 @@ def main() -> None: changed = False + to_remove = [] for key in crate_manifest["package"].keys(): - changed |= replace_key( + changed_key = replace_key( workspace_manifest, crate_manifest["package"], "package", key ) + if changed_key and crate_manifest["package"][key] == {}: + # Key is missing from workspace manifest, mark for deletion + to_remove.append(key) + changed |= changed_key + # Remove keys which have no value + for key in to_remove: + del crate_manifest["package"][key] changed |= replace_dependencies(workspace_manifest, crate_manifest) diff --git a/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/crate_missing_field.toml b/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/crate_missing_field.toml new file mode 100644 index 000000000000..6474a3893285 --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/crate_missing_field.toml @@ -0,0 +1,13 @@ +[package] +name = "im_using_workspaces" +version = { workspace = true } +publish = false +readme.workspace = true +keywords = [ + "workspace", + "other_thing", + "third_thing", +] + +[dependencies] +bar = "1.0.0" diff --git a/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/default.nix b/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/default.nix index b52026190e29..e0f64510d61a 100644 --- a/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/default.nix +++ b/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/default.nix @@ -8,4 +8,8 @@ runCommand "git-dependency-workspace-inheritance-test" { } '' cp --no-preserve=mode ${./crate_lints.toml} "$out" ${replaceWorkspaceValues} "$out" ${./workspace.toml} diff -u "$out" ${./want_lints.toml} + + cp --no-preserve=mode ${./crate_missing_field.toml} "$out" + ${replaceWorkspaceValues} "$out" ${./workspace.toml} + diff -u "$out" ${./want_missing_field.toml} '' diff --git a/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/want_missing_field.toml b/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/want_missing_field.toml new file mode 100644 index 000000000000..fc4b44bf7e50 --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/want_missing_field.toml @@ -0,0 +1,12 @@ +[package] +name = "im_using_workspaces" +version = "1.0.0" +publish = false +keywords = [ + "workspace", + "other_thing", + "third_thing", +] + +[dependencies] +bar = "1.0.0" From cb280b66dbf74ff6641fa5930f4dde28e7255071 Mon Sep 17 00:00:00 2001 From: beeb <703631+beeb@users.noreply.github.com> Date: Wed, 10 Sep 2025 07:35:18 +0200 Subject: [PATCH 2/2] replace-workspace-values.py: format --- .../build-support/rust/replace-workspace-values.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkgs/build-support/rust/replace-workspace-values.py b/pkgs/build-support/rust/replace-workspace-values.py index 1130c67ced75..64fd7151d46c 100644 --- a/pkgs/build-support/rust/replace-workspace-values.py +++ b/pkgs/build-support/rust/replace-workspace-values.py @@ -44,17 +44,17 @@ def replace_key( final: dict[str, Any] = workspace_dep.copy() - merged_features = local_dep.pop("features", []) + workspace_dep.get("features", []) + merged_features = local_dep.pop("features", []) + workspace_dep.get( + "features", [] + ) if merged_features: final["features"] = merged_features local_default_features = local_dep.pop( - "default-features", - local_dep.pop("default_features", None) + "default-features", local_dep.pop("default_features", None) ) workspace_default_features = workspace_dep.get( - "default-features", - workspace_dep.get("default_features") + "default-features", workspace_dep.get("default_features") ) if not workspace_default_features and local_default_features: @@ -68,7 +68,9 @@ def replace_key( final["package"] = local_dep.pop("package") if local_dep: - raise Exception(f"Unhandled keys in inherited dependency {key}: {local_dep}") + raise Exception( + f"Unhandled keys in inherited dependency {key}: {local_dep}" + ) table[key] = final elif section == "package":