From 04758b56e6acb63bd5772de0355330116236fec2 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:05:30 +0900 Subject: [PATCH 1/2] build(release): stop adding blank lines to the changelog on every backport When the news of a backport were merged into an existing version block, a newline was added after the block, whilst the blank lines that separate it from the next version were kept. A release with N backports ended up with N extra blank lines. Normalize the separator to the two blank lines that a new version block gets, which also fixes the blocks that already have too many. The block is no longer passed through a regex replacement string, so a backslash in the changelog or in a news entry does not fail the merge. --- dev/release/changelog_news.py | 25 ++-- .../private/release/changelog_news_test.py | 128 ++++++++++++++++++ 2 files changed, 145 insertions(+), 8 deletions(-) diff --git a/dev/release/changelog_news.py b/dev/release/changelog_news.py index 6381aacb34..d0e53b7c95 100644 --- a/dev/release/changelog_news.py +++ b/dev/release/changelog_news.py @@ -3,6 +3,10 @@ import pathlib import re +# The release blocks end with a newline; this adds the two blank lines that +# separate them from the next version. +_VERSION_SEPARATOR = "\n\n" + def _get_sub_category(content): """Extracts the sub-category in parentheses from the entry content.""" @@ -286,14 +290,19 @@ def _add_news_to_changelog(input_path, output_path, version, entries, release_da header_str.rstrip() + "\n\nNo notable unreleased changes.\n" ) - # Replace in changelog_content - new_content = re.sub( - pattern, - r"\g\n" + new_release_block.strip() + "\n", - changelog_content, - count=1, - flags=re.DOTALL, + # Replace in changelog_content. The match stops before the blank + # lines that separate the block from the next version, so normalize + # them instead of adding a newline on every merge. + new_content = ( + changelog_content[: match.start()] + + version_anchor + + "\n" + + new_release_block.strip() + + "\n" ) + rest = changelog_content[match.end() :].lstrip() + if rest: + new_content += _VERSION_SEPARATOR + rest output_path.write_text(new_content, encoding="utf-8") else: @@ -309,7 +318,7 @@ def _add_news_to_changelog(input_path, output_path, version, entries, release_da new_content = ( changelog_content[:insertion_point] + new_release_block - + "\n\n" + + _VERSION_SEPARATOR + changelog_content[insertion_point:] ) output_path.write_text(new_content, encoding="utf-8") diff --git a/tests/tools/private/release/changelog_news_test.py b/tests/tools/private/release/changelog_news_test.py index c251efcaa1..a99d0acd87 100644 --- a/tests/tools/private/release/changelog_news_test.py +++ b/tests/tools/private/release/changelog_news_test.py @@ -284,6 +284,134 @@ def test_update_changelog_merge_existing(tmp_path): assert "Unreleased changes are tracked as individual files" in new_content +def test_update_changelog_repeated_merges_keep_blank_lines(tmp_path): + # Arrange + changelog = """# Changelog + +{#unreleased} +## Unreleased + +[unreleased]: https://github.com/bazel-contrib/rules_python/releases/tag/unreleased + +{#v2-0-2} +## [2.0.2] - 2026-06-01 + +[2.0.2]: https://github.com/bazel-contrib/rules_python/releases/tag/2.0.2 + +{#v2-0-2-fixed} +### Fixed +* (pypi) Old fix +""" + changelog_path = tmp_path / "CHANGELOG.md" + changelog_path.write_text(changelog) + + news_dir = tmp_path / "news" + news_dir.mkdir() + + # Act: every backport merges its news into the same version. + for i in range(3): + (news_dir / f"{i}.fixed.md").write_text(f"(pypi) Fix {i}") + changelog_news.update_changelog( + "2.0.3", + "2026-06-15", + changelog_path=changelog_path, + news_dir=news_dir, + ) + + # Assert + new_content = changelog_path.read_text() + expected = """{#v2-0-3-fixed} +### Fixed +* (pypi) Fix 0 +* (pypi) Fix 1 +* (pypi) Fix 2 + + +{#v2-0-2} +""" + assert expected in new_content + assert new_content.endswith("* (pypi) Old fix\n") + + +def test_update_changelog_merge_last_block_with_backslashes(tmp_path): + # Arrange + changelog = """# Changelog + +{#v2-0-3} +## [2.0.3] - 2026-06-15 + +[2.0.3]: https://github.com/bazel-contrib/rules_python/releases/tag/2.0.3 + +{#v2-0-3-fixed} +### Fixed +* (pypi) Paths like `C:\\dir` work +""" + changelog_path = tmp_path / "CHANGELOG.md" + changelog_path.write_text(changelog) + + news_dir = tmp_path / "news" + news_dir.mkdir() + + # Act + for i in range(2): + (news_dir / f"{i}.fixed.md").write_text(f"(pypi) Regex `\\d{i}` works") + changelog_news.update_changelog( + "2.0.3", + "2026-06-15", + changelog_path=changelog_path, + news_dir=news_dir, + ) + + # Assert + new_content = changelog_path.read_text() + expected = """### Fixed +* (pypi) Paths like `C:\\dir` work +* (pypi) Regex `\\d0` works +* (pypi) Regex `\\d1` works +""" + assert new_content.endswith(expected) + + +def test_update_changelog_merge_normalizes_blank_lines(tmp_path): + # Arrange + changelog = """# Changelog + +{#v2-0-3} +## [2.0.3] - 2026-06-15 + +[2.0.3]: https://github.com/bazel-contrib/rules_python/releases/tag/2.0.3 + +{#v2-0-3-fixed} +### Fixed +* (pypi) Fix 0 + + + + + +{#v2-0-2} +## [2.0.2] - 2026-06-01 +""" + changelog_path = tmp_path / "CHANGELOG.md" + changelog_path.write_text(changelog) + + news_dir = tmp_path / "news" + news_dir.mkdir() + (news_dir / "1.fixed.md").write_text("(pypi) Fix 1") + + # Act + changelog_news.update_changelog( + "2.0.3", + "2026-06-15", + changelog_path=changelog_path, + news_dir=news_dir, + ) + + # Assert + new_content = changelog_path.read_text() + assert "* (pypi) Fix 1\n\n\n{#v2-0-2}\n" in new_content + + def test_update_changelog_does_not_leak(tmp_path): # Arrange changelog = """# Changelog From 2b6bdc06bd5a65869c0c94650a1f22a4818f1b48 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:08:25 +0900 Subject: [PATCH 2/2] docs(changelog): remove the extra blank lines and trailing whitespace The release tooling used to add a blank line for every backport, which left up to five blank lines between the 2.4.6, 2.3.4 and 2.1.0 blocks and the versions before them. --- CHANGELOG.md | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07601d0f5e..5047657e7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -189,9 +189,6 @@ changelog](https://rules-python.readthedocs.io/en/latest/changelog.html). ([#1975](https://github.com/bazel-contrib/rules_python/issues/1975)) - - - {#v2-4-6} ## [2.4.6] - 2026-10-29 @@ -345,8 +342,6 @@ changelog](https://rules-python.readthedocs.io/en/latest/changelog.html). ([#4029](https://github.com/bazel-contrib/rules_python/issues/4029)). - - {#v2-3-4} ## [2.3.4] - 2026-09-21 @@ -539,7 +534,6 @@ Work towards [#2787](https://github.com/bazel-contrib/rules_python/issues/2787) and [#1975](https://github.com/bazel-contrib/rules_python/issues/1975). - {#v2-1-0} ## [2.1.0] - 2026-06-17 @@ -571,9 +565,9 @@ and [#1975](https://github.com/bazel-contrib/rules_python/issues/1975). {#v2-1-0-fixed} ### Fixed * (gazelle) `py_library` and `py_test` targets with missing source files can now be - removed by Gazelle ([#3375](https://github.com/bazel-contrib/rules_python/issues/3375)). - However `map_kind` and `alias_kind` will not be removed unless people are running a - gazelle version that includes + removed by Gazelle ([#3375](https://github.com/bazel-contrib/rules_python/issues/3375)). + However `map_kind` and `alias_kind` will not be removed unless people are running a + gazelle version that includes [bazel-gazelle#2362](https://github.com/bazel-contrib/bazel-gazelle/pull/2362) * (bootstrap) Fixed a potential race condition with symlink creation during startup. @@ -627,7 +621,7 @@ and [#1975](https://github.com/bazel-contrib/rules_python/issues/1975). Fixes [#3296](https://github.com/bazel-contrib/rules_python/issues/3296). * (gazelle) Support alias_kind directive. Fixes [#3183](https://github.com/bazel-contrib/rules_python/issues/3183). -* (pypi) `package_metadata` support, fixes +* (pypi) `package_metadata` support, fixes [#2054](https://github.com/bazel-contrib/rules_python/issues/2054). * (coverage) Add support for python 3.14 and bump `coverage.py` to 7.10.7.