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.
This commit is contained in:
beeb
2025-09-02 11:56:49 +02:00
committed by Winter
parent 737ce9d193
commit 30a786c9a1
4 changed files with 44 additions and 2 deletions

View File

@@ -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)

View File

@@ -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"

View File

@@ -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}
''

View File

@@ -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"