replace-workspace-values.py: fix missing key exception (#439487)

This commit is contained in:
Winter
2025-09-16 21:09:26 -04:00
committed by GitHub
4 changed files with 52 additions and 8 deletions
@@ -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):
@@ -39,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:
@@ -63,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":
@@ -104,10 +111,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)
@@ -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"
@@ -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}
''
@@ -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"