Skip to content

fix(connect): keep comments when connect init and clean edit the block - #443

Open
nicksinas wants to merge 5 commits into
mainfrom
fix/connect-init-keeps-comments
Open

nicksinas wants to merge 5 commits into
mainfrom
fix/connect-init-keeps-comments

Conversation

@nicksinas

Copy link
Copy Markdown
Contributor

Problem

avocado connect init rewrites the whole connect: block of avocado.yaml. The skip loop in set_connect_fields_in_yaml treats every blank line and every comment after connect: as part of the block, at any indent. The function then writes back only org, project and server_key at a 2-space indent. As a result, it deletes:

  • comments inside the block
  • column-0 comments and blank lines between the block and the next top-level key, such as a ## Runtimes section header
  • unknown keys in the block

It also changes a 4-space block to 2 spaces. avocado connect clean uses the same loop in remove_connect_fields_in_yaml, so it also deletes the header comment after the block.

Example with 1.2.0 and connect clean:

 sdk:
   image: docker.io/avocadolinux/sdk:latest
-
-connect:
-  org: 0000...
-  # The project and server key come from Avocado Connect.
-  project: 0000...
-  server_key: abc...
-
-##
-## Runtimes
-##
 runtimes:

Change

All changes are in src/utils/config_edit.rs.

  • New top_level_block_end: the block ends at its last real child line, which is a line that is not blank and not a comment. Blank lines and comments after it stay with the next key. A column-0 comment between two children does not end the block.
  • set_connect_fields_in_yaml keeps every line and changes only the value of a direct child org, project or server_key. The child indent comes from the first real child line.
  • New replace_yaml_value keeps the indent, the quote style and an inline comment (org: x # note). If the old value is empty (org: # fill in), it puts a space before the comment, so the result is not org: o# fill in.
  • A replaced value that continues on deeper lines (value on the next line, or a block scalar) has those lines removed. Deeper lines under other keys do not change.
  • A missing field is added after the last real child line, at the child indent.
  • remove_connect_fields_in_yaml uses top_level_block_end. Indented comments after the last child go with the block, so connect clean leaves no orphan comment. If connect: is the first key, the file does not start with a blank line.

If the values do not change, connect init now gives a byte-identical file.

Testing

  • 13 new unit tests in config_edit.rs: same values give the same file, new values keep comments, indent and unknown keys, missing keys, empty block, inline comments and quotes, nested keys, no block, empty value with a comment, values on the next lines, and the clean cases.
  • All tests in config_edit.rs now use placeholder Connect IDs and a placeholder server key. One older test used real-looking values.
  • Before the fix, 7 of the first 9 tests fail. The 4 later tests fail on the first commit and pass on the second.
  • connect clean with the built binary on a sample file removes only the connect: block. The header and the blank line before it stay.
  • connect init was not run end to end, because it needs a Connect server. The unit tests cover the file edit that init does.
  • cargo fmt --all -- --check and RUST_MIN_STACK=8388608 cargo test pass.
  • cargo clippy --all-targets --all-features -- -D warnings on macOS gives 5 dead_code errors in src/utils/host_mounts.rs. They are on main too (the callers are Linux-only). This change adds no warnings.

`avocado connect init` rewrote the whole `connect:` block. It treated
every blank line and every comment after `connect:` as part of the
block, at any indent, and wrote back only org, project and server_key
at a 2-space indent. This deleted comments inside the block, unknown
keys, and column-0 section headers before the next top-level key.
`avocado connect clean` used the same loop, so it also deleted the
header comment after the block.

- Add top_level_block_end: the block ends at its last real child line,
  so trailing blank lines and comments stay with the next key
- Update org, project and server_key values in place, keep the indent,
  the quote style and any inline comment
- Add a missing field after the last real child line of the block
- Use top_level_block_end in remove_connect_fields_in_yaml
- Add tests for set_connect_fields_in_yaml and for the clean header case

Refs: (bug)avocado-cli-S3-connect-init-deletes-comments-after-the-connect-block.md
QA found more cases where the connect block edit gave wrong YAML or left
lines behind.

- Put a space before an inline comment when the old value is empty, so
  `org: # x` does not become `org: o# x`
- Remove the deeper lines of a replaced value, for a value on the next
  line or a block scalar
- In connect clean, remove indented comments after the last child
- In connect clean, do not leave a blank first line when connect is
  the first key
- Add tests for each case

Refs: (bug)avocado-cli-S3-connect-init-deletes-comments-after-the-connect-block.md
The connect field tests used real Connect org and project IDs and a
real server key as sample values. Use placeholder values instead.

- Replace the org and project UUIDs with 00000000-0000-7000-8000-00000000000N
- Replace the server key with a placeholder hex string
Copilot AI balanced review requested due to automatic review settings November 28, 2026 18:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The insertion logic can mistake hash-prefixed multiline scalar content for comments and place missing fields inside unknown values, altering preserved configuration data.

1 open issue
What changed in this PR

This PR makes the YAML editing used by avocado connect init and connect clean more selective, aiming to preserve surrounding configuration and comments.

Changes:

  • Updates existing Connect fields in place, preserving indentation, unknown keys, quotes, and inline comments.
  • Refines block removal to retain following section headers and avoid leading blank lines.
  • Adds 13 regression tests and replaces real-looking test credentials with placeholders.
File Description
src/​utils/​config_edit.rs Refines Connect YAML editing and block boundaries, adds preservation tests, and updates test fixtures.

Review effort: Balanced

Comment thread src/utils/config_edit.rs Resolved
A line that starts with # inside a block scalar or a quoted value that
spans lines is part of the value. The connect block edit read it as a
comment, so connect init could put the missing fields inside the value
and change it.

- Mark the lines that continue a block scalar or a multi-line quoted
  value, and treat them as content when finding the block end, the child
  indent and the insert point
- Drop such lines with the value when a connect field is replaced
- Add tests for a block scalar, a quoted value and the line marking

Refs: #443

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The config editor can corrupt multiline scalar values, remove scalar-owned blank lines, and leave undefined YAML aliases when replacing anchored fields.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Preserve multiline quoted scalar continuations

src/​utils/​config_edit.rs:1171

Multiline quoted scalars can continue at the direct-child indentation: with extra: "first\n org: data\n last", this loop rewrites text inside the unknown value and marks org as found, so no actual org field is added. Replacing org: "first\n last" also leaves a dangling continuation and invalid YAML. Treat lines marked by in_scalar as value continuations regardless of indentation, preserving them unless their owning field is being replaced.

Medium severity Retain blank lines belonging to block scalars

src/​utils/​config_edit.rs:1256

Blank lines can also be scalar data. With extra: |+, a payload line and a trailing blank line before runtimes: {}, this mask excludes the blank line, so missing Connect fields are inserted before it. That changes the retained unknown value from payload\n\n to payload\n. Track scalar-owned blank lines in the block-end and insertion calculations, taking care not to interpret an empty scalar line's zero indentation as a top-level boundary.

Medium severity Retain YAML anchors when replacing scalar values

src/​utils/​config_edit.rs:1372

A valid block containing org: &org_id old-org and an unknown child extra: *org_id becomes invalid after this replacement: the new line is org: o, but extra: *org_id is now preserved and references an undefined anchor. Retain YAML node properties such as anchors when replacing the scalar content, rather than discarding them with the old value, so preserving unknown keys does not leave dangling aliases.

1 resolved since last review

Review effort: Balanced

The connect field edit still changed some values it should leave alone.
A line inside a quoted multi-line value that sat at the child indent,
such as "  org: data", was read as a key and replaced. Blank lines at the
end of a keep (|+) block scalar were not counted as data, so new fields
went in before them and the value lost its trailing newlines. Replacing
"org: &id old" gave "org: o", which dropped the anchor and left any
"*id" alias with nothing to refer to.

- Skip lines inside a multi-line scalar in the key loop, and drop them
  with the value when their field is replaced
- Mark blank lines in a keep block scalar as data, and keep such lines in
  the connect block when the block end is found
- Keep anchors and tags in front of the old value when the value is
  replaced
- Add tests for each case that also parse the result as YAML

Refs: #443
@nicksinas

Copy link
Copy Markdown
Contributor Author

Reply to the Copilot review of 53aa630:

  • Quoted continuation line at the child indent read as a key: good catch. The key loop now skips lines that are inside a multi-line scalar, so org: data" inside a quoted value stays data. Those lines go with the value when their field is replaced. The test parses the result as YAML. Fixed in 6d2089b.
  • Blank lines in a |+ block: agreed for keep blocks. Blank lines in a |+ or >+ block now count as data, and the block end keeps them. New fields go after them, so the value keeps its trailing newlines. Clip and strip drop trailing blank lines, so the insert point does not change those values. Test added. Fixed in 6d2089b.
  • Anchor dropped on replace: agreed. The replace now keeps an anchor (&id) and a tag (!tag) in front of the old value, so org: &id old becomes org: &id o and *id still resolves. Test added. Fixed in 6d2089b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants