Repository navigation
fix(config): keep the comments of config.yaml when a command writes it - #526
Conversation
Every writer re-encoded the in-memory struct, so a command such as `target --mode copy` dropped every comment, including marker comments users build scripts around. marshalConfig now takes the file being replaced and copies its comments onto the keys that are still present. Fixes #525
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: de28e853c3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| m.HeadComment = withoutSchemaDirective(m.HeadComment) | ||
| if len(m.Content) > 0 { | ||
| m.Content[0].HeadComment = withoutSchemaDirective(m.Content[0].HeadComment) | ||
| } |
There was a problem hiding this comment.
Strip the schema comment from the originally matched key
When an existing file starts in a noncanonical section order, yaml.v3 associates the schema directive with that file's first key, but sortSections has already reordered the new tree before comments are copied. Stripping only m.Content[0] therefore misses the copied directive on the old first key; for example, a file ordered as mode then sources is saved with the generated schema header at the top and another schema directive before mode, contradicting the intended single-header behavior.
Useful? React with 👍 / 👎.
| case old.Kind == yaml.SequenceNode && cur.Kind == yaml.SequenceNode && len(old.Content) == len(cur.Content): | ||
| for i := range old.Content { | ||
| copyComments(old.Content[i], cur.Content[i]) | ||
| } |
There was a problem hiding this comment.
Preserve comments when sequence lengths change
When a command adds or removes an item from a sequence, this length check prevents all recursive comment copying for that sequence, including comments on entries that survived unchanged. This occurs in common paths such as project target add/remove and global or project extra removal, so saving after one of those operations still discards comments from every remaining item and its nested keys; surviving entries need to be matched independently rather than requiring equal sequence lengths.
Useful? React with 👍 / 👎.
…d lists Codex review: the schema directive stuck to the old first key when the file was in a noncanonical order, and a list edit dropped the comments of every remaining item.
|
Both fixed in the follow-up commit: the schema directive is stripped from the old document before any copying, and sequence items are matched by value (or by first key and value for |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e479db5103
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return len(a.Content) >= 2 && len(b.Content) >= 2 && | ||
| a.Content[0].Value == b.Content[0].Value && a.Content[1].Value == b.Content[1].Value |
There was a problem hiding this comment.
Match sequence mappings by their identity key
For mapping items whose first value is a collection, this comparison is not an identity check because collection nodes have an empty Value. Project targets with files, agents, or instructions are marshaled from a map that can place one of those keys before name, so multiple targets can all match the first current item; comments on later targets are then lost or copied to the wrong target during any subsequent save. Locate the name pair regardless of key order and make sequence matching one-to-one.
Useful? React with 👍 / 👎.
…registry.yaml comments too Review follow-up: a swallowed read error would have written the config without its comments. The three savers share one helper; registry.yaml is the third YAML file we rewrite from a struct.
|
Follow-up from an internal review: a read error on the existing file now fails the save instead of silently dropping its comments; the three savers (global, project, registry.yaml) share |
Codex review: a target whose first value is a list has an empty Value, so every such item matched the first one and took its comments.
|
Fixed: sequence items are now identified by their |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5488e0a5f7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| func itemIdentity(n *yaml.Node) string { | ||
| switch n.Kind { | ||
| case yaml.ScalarNode: | ||
| return n.Value |
There was a problem hiding this comment.
Use one identity for scalar and object-form targets
When a project target changes representation, the same target cannot match because a scalar target produces claude while an object target produces name=claude. This occurs when a settings command converts - claude # comment into an object, and also when a name-only object is canonicalized back to a scalar by ProjectTargetEntry.MarshalYAML; copyComments then treats the surviving target as a new item and drops its comments. Normalize both forms to the same name-based identity.
Useful? React with 👍 / 👎.
…and object form Codex review: `- claude` and `- name: claude` are the same target, so they share an identity and the line comment moves onto the name.
|
Fixed: a scalar target and an object target with the same name now share one identity, and the line comment moves onto |
Why
Every command that writes
config.yaml(global or project) re-encoded the in-memory struct with yaml.v3, so comments were lost. A user keeps a generated block between# BEGIN …/# END …markers and atarget --mode copy --target-naming prefixedsilently removed them (#525).What
marshalConfigtakes the bytes of the file being replaced and copies head, line and foot comments onto every key that is still present. Comments the new document already carries (rawhooks/pluginsnodes) win, so nothing is duplicated. The schema directive is stripped from the copied comments because the header is written separately.Config.Save,ProjectConfig.SaveIn, the two legacy-target migrations on load, and the registry split.mode, save, assert the comments and the single schema header survive.Fixes #525
Verification
make checkin the devcontainer: docs-check, fmt-check, lint, unit + integration tests all pass.