From 5e82077b26f5dbe654f0c6536a8d5ce27b9cfd51 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Tue, 14 Apr 2026 23:43:33 +0200 Subject: [PATCH] Fix duplicated comments when re-saving YAML files This was due to nested comments associated with list items. We now completely remove these. Fixes #1285 --- CHANGELOG.md | 1 + src/serena/util/yaml.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48beba24..cc432a74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Status of the `main` branch. Changes prior to the next official version change w * General: - Support `serena --version` CLI command for displaying the current version #1347 - Fix: Check for ignored path ignored `.git` folder only at the top level, not in every subdirectory (`Project._is_ignored_relative_path`) #1350 + - Fix: Duplicate comments in re-saved YAML configuration files #1285 # v1.1.2 (2026-04-14) diff --git a/src/serena/util/yaml.py b/src/serena/util/yaml.py index 89c68027..ca63eee7 100644 --- a/src/serena/util/yaml.py +++ b/src/serena/util/yaml.py @@ -4,7 +4,7 @@ from collections.abc import Sequence from enum import Enum from typing import Any -from ruamel.yaml import YAML, CommentToken, StreamMark +from ruamel.yaml import YAML, CommentedSeq, CommentToken, StreamMark from ruamel.yaml.comments import CommentedMap from serena.constants import SERENA_FILE_ENCODING @@ -122,6 +122,20 @@ def normalise_yaml_comments(commented_map: CommentedMap, comment_normalisation: first_token.value = first_token.value[1:] return token_list + def remove_nested_comments() -> None: + """ + Removes nested comments, particularly of sequences, which incorrectly capture comments + that are actually intended for top-level keys. + """ + for key in commented_map.keys(): + entry = commented_map[key] + if isinstance(entry, CommentedSeq): + items = entry.ca.items + if isinstance(items, dict): + items_keys = list(items.keys()) + for i in items_keys: + items[i] = [None] * 4 + match comment_normalisation: case YamlCommentNormalisation.NONE: pass @@ -176,6 +190,9 @@ def normalise_yaml_comments(commented_map: CommentedMap, comment_normalisation: preceding_comment[ITEM_COMMENT_INDEX_BEFORE] = token_list current_comment[ITEM_COMMENT_INDEX_BEFORE] = None preceding_comment = current_comment + + # remove nested comments, as we assume that only top-level keys are supposed to be commented + remove_nested_comments() case _: raise ValueError(f"Unhandled comment normalisation: {comment_normalisation}")