You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(connect): keep comments when connect init and clean edit the block - #443
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
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.
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.
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
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
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.
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.
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.
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
avocado connect initrewrites the wholeconnect:block ofavocado.yaml. The skip loop inset_connect_fields_in_yamltreats every blank line and every comment afterconnect:as part of the block, at any indent. The function then writes back onlyorg,projectandserver_keyat a 2-space indent. As a result, it deletes:## Runtimessection headerIt also changes a 4-space block to 2 spaces.
avocado connect cleanuses the same loop inremove_connect_fields_in_yaml, so it also deletes the header comment after the block.Example with 1.2.0 and
connect clean:Change
All changes are in
src/utils/config_edit.rs.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_yamlkeeps every line and changes only the value of a direct childorg,projectorserver_key. The child indent comes from the first real child line.replace_yaml_valuekeeps 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 notorg: o# fill in.remove_connect_fields_in_yamlusestop_level_block_end. Indented comments after the last child go with the block, soconnect cleanleaves no orphan comment. Ifconnect:is the first key, the file does not start with a blank line.If the values do not change,
connect initnow gives a byte-identical file.Testing
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 thecleancases.config_edit.rsnow use placeholder Connect IDs and a placeholder server key. One older test used real-looking values.connect cleanwith the built binary on a sample file removes only theconnect:block. The header and the blank line before it stay.connect initwas not run end to end, because it needs a Connect server. The unit tests cover the file edit thatinitdoes.cargo fmt --all -- --checkandRUST_MIN_STACK=8388608 cargo testpass.cargo clippy --all-targets --all-features -- -D warningson macOS gives 5dead_codeerrors insrc/utils/host_mounts.rs. They are onmaintoo (the callers are Linux-only). This change adds no warnings.