Skip to content

Commit 78e5c3c

Browse files
committed
Fix too greedy match for replacing workspace value
1 parent 5b1a5ff commit 78e5c3c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

easybuild/easyblocks/generic/cargo.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,13 @@ def do_replacement(section, workspace_section):
253253
for key, value in section.items():
254254
if (key.endswith(SUFFIX) and value == 'true') or value == '{ workspace = true }':
255255
real_key = key[:-len(SUFFIX)] if key.endswith(SUFFIX) else key
256-
value = workspace_section[real_key]
257-
idx = next(idx for idx, line in enumerate(lines) if key in line)
258-
lines[idx] = f'{real_key} = {value}'
256+
new_value = workspace_section[real_key]
257+
try:
258+
idx = next(idx for idx, line in enumerate(lines)
259+
if line.lstrip().startswith(f'{key} =') and value in line)
260+
except StopIteration:
261+
raise ValueError(f"Failed to find line for key '{key}' while merging {cargo_toml_path}")
262+
lines[idx] = f'{real_key} = {new_value}'
259263

260264
do_replacement(cargo_toml.get('package'), workspace_toml.get('workspace.package'))
261265
do_replacement(cargo_toml.get('dependencies'), workspace_toml.get('workspace.dependencies'))

test/easyblocks/easyblock_specific.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ def test_cargo_merge_sub_crate(self):
449449
description.workspace = true
450450
documentation.workspace = true
451451
452+
# Unrelated line that looks like a workspace key
453+
description = "Uses regex=123 and regex = 456 and not foo.workspace = true"
454+
452455
[dependencies]
453456
regex.workspace = true
454457
@@ -467,6 +470,9 @@ def test_cargo_merge_sub_crate(self):
467470
description = "A short description of my package"
468471
documentation = "https://example.com/bar"
469472
473+
# Unrelated line that looks like a workspace key
474+
description = "Uses regex=123 and regex = 456 and not foo.workspace = true"
475+
470476
[dependencies]
471477
regex = { version = "1.6.0", default-features = false, features = ["std"] }
472478

0 commit comments

Comments
 (0)