From 98c1c2e934995a2c6ce740d4ff43ce0daa19b79f Mon Sep 17 00:00:00 2001 From: dusk Date: Fri, 20 Dec 2024 21:13:37 +0000 Subject: [PATCH] fix(rust-cargo-vendor): patch manifest attributes that use workspace.package aswell (#1082) --- ...h-workspace-deps.jq => patch-workspace.jq} | 28 ++++++++++++++++--- .../dream2nix/rust-cargo-vendor/vendor.nix | 10 +++---- 2 files changed, 29 insertions(+), 9 deletions(-) rename modules/dream2nix/rust-cargo-vendor/{patch-workspace-deps.jq => patch-workspace.jq} (58%) diff --git a/modules/dream2nix/rust-cargo-vendor/patch-workspace-deps.jq b/modules/dream2nix/rust-cargo-vendor/patch-workspace.jq similarity index 58% rename from modules/dream2nix/rust-cargo-vendor/patch-workspace-deps.jq rename to modules/dream2nix/rust-cargo-vendor/patch-workspace.jq index 0eb57d74..97ed4950 100644 --- a/modules/dream2nix/rust-cargo-vendor/patch-workspace-deps.jq +++ b/modules/dream2nix/rust-cargo-vendor/patch-workspace.jq @@ -1,7 +1,7 @@ def normalizeWorkspaceDep: - if ($workspaceDependencies."\(.key)" | type) == "object" - then [.value, $workspaceDependencies."\(.key)"] | add - else [.value, {"version":$workspaceDependencies."\(.key)"}] | add + if ($workspaceAttrs."dependencies"."\(.key)" | type) == "object" + then [.value, $workspaceAttrs."dependencies"."\(.key)"] | add + else [.value, {"version":$workspaceAttrs."dependencies"."\(.key)"}] | add end # remove workspace option from the dependency | del(.workspace) @@ -33,8 +33,28 @@ def mapWorkspaceDeps: | mapWorkspaceDepsFor("build-dependencies") ; -# normalize workspace inherited deps +# normalizes workpsace inherited package attributes +def mapWorkspaceAttrs(name): + if has(name) + then + ."\(name)" = ( + ."\(name)" + | to_entries + | map( + if (.value | type) == "object" and .value.workspace == true + then .value = $workspaceAttrs."\(name)"."\(.key)" + else . + end + ) + | from_entries + ) + else . + end +; + +# normalize workspace inherited deps and attributes mapWorkspaceDeps +| mapWorkspaceAttrs("package") | if has("target") then # normalize workspace inherited deps in target specific deps diff --git a/modules/dream2nix/rust-cargo-vendor/vendor.nix b/modules/dream2nix/rust-cargo-vendor/vendor.nix index e7bfff87..897585fd 100644 --- a/modules/dream2nix/rust-cargo-vendor/vendor.nix +++ b/modules/dream2nix/rust-cargo-vendor/vendor.nix @@ -94,16 +94,16 @@ in rec { exit 1 fi else - # we need to patch dependencies with `workspace = true` (workspace inheritance) - workspaceDependencies="$(cat "$tree/Cargo.toml" | ${tomlToJson} | ${jq} -cr '.workspace.dependencies')" - if [[ "$workspaceDependencies" != "null" ]]; then + # we need to patch manifest attributes with `workspace = true` (workspace inheritance) + workspaceAttrs="$(cat "$tree/Cargo.toml" | ${tomlToJson} | ${jq} -cr '.workspace')" + if [[ "$workspaceAttrs" != "null" ]]; then tree="$(pwd)/${pkg.name}-${pkg.version}" cp -prd --no-preserve=mode,ownership "$(dirname $crateCargoTOML)" "$tree" crateCargoTOML="$tree/Cargo.toml" cat "$crateCargoTOML" \ | ${tomlToJson} \ - | ${jq} -cr --argjson workspaceDependencies "$workspaceDependencies" \ - --from-file ${./patch-workspace-deps.jq} \ + | ${jq} -cr --argjson workspaceAttrs "$workspaceAttrs" \ + --from-file ${./patch-workspace.jq} \ | ${jsonToToml} \ | ${sponge} "$crateCargoTOML" fi