feat(spec): preserve referenced macros during removal - #334
Open
Thien Trung Vuong (trungams) wants to merge 1 commit into
Conversation
Thien Trung Vuong (trungams)
force-pushed
the
tvuong/structural-spec-editor-macro-preservation
branch
from
September 2, 2026 23:50
4a298a0 to
740f791
Compare
Thien Trung Vuong (trungams)
force-pushed
the
tvuong/structural-spec-editor-macro-preservation
branch
from
September 10, 2026 03:54
740f791 to
1771691
Compare
Copilot started reviewing on behalf of
Thien Trung Vuong (trungams)
September 10, 2026 03:56
View session
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved integration and macro-analysis correctness issues remain.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds structural RPM macro hoisting when sections or subpackages are removed, preserving referenced declarations and rejecting unsafe relocations.
Changes:
- Tracks transitive macro dependencies and preserves declaration order.
- Rejects ambiguous or unsafe hoists without modifying the spec.
- Adds focused tests and regression fixtures.
File summaries
| File | Summary |
|---|---|
internal/rpm/spec/tree_hoist.go |
Implements macro analysis, validation, and hoisting. |
internal/rpm/spec/tree_hoist_internal_test.go |
Adds focused hoisting and rejection tests. |
internal/rpm/spec/testdata/specs/subpackage-define-transitive.spec |
Covers transitive macro preservation. |
internal/rpm/spec/testdata/specs/subpackage-define-shadowed.spec |
Covers shadowed definitions. |
internal/rpm/spec/testdata/specs/subpackage-define-referenced.spec |
Covers direct macro references. |
internal/rpm/spec/testdata_test.go |
Adds fixture-based integration coverage. |
internal/rpm/spec/structural_tree_api.go |
Integrates hoisting with section removal. |
Review details
Suppressed comments (7)
internal/rpm/spec/structural_tree_api.go:188
- This changes the user-facing behavior of both
spec-remove-sectionandspec-remove-subpackage, butdocs/user/reference/config/overlays.mdstill only documents section deletion. Please document that referenced macro declarations may be hoisted and that conservativeErrUnsafeMacroHoistcases abort without changing the spec.
if err := t.hoistReferencedMacros(sections); err != nil {
return err
}
internal/rpm/spec/structural_tree_api.go:188
- An empty handle slice now reaches
hoistReferencedMacros, where no removed root ancestor exists andErrUnsafeMacroHoistis returned. Before this hook,validateSectionRemovalfollowed byremoveSectionsmadeRemoveSections(nil)a no-op; retain that behavior for callers that build a selection dynamically and can have no matches.
if err := t.hoistReferencedMacros(sections); err != nil {
return err
}
internal/rpm/spec/tree_hoist.go:466
- Dynamic invocations are only checked against removed declaration names here; this never resolves a dynamic target to a surviving lazy definition and traverses that definition's dependencies. For example, with surviving
%define suffix nameand%define rootname %{dep}, a removed%define dep value, andecho %{root%{suffix}}, the dynamic target isrootnamesodepis never selected and removal leaves a dangling%{dep}whenrootnameexpands. Resolve or conservatively reject matching surviving definitions at the invocation order.
for _, reference := range facts.references {
visit(effectiveMacroDefinition(facts.all[reference.name], reference.order), reference.order)
}
internal/rpm/spec/tree_hoist.go:616
- This treats macro-looking text in comments as a live reference. RPM does not expand a
#comment, so a removed parameterized or conditional declaration plus a surviving line such as# %{helper}will select that declaration and then fail withErrUnsafeMacroHoist, even though no surviving content needs it. Skip comment-only lines before collecting references.
if !removed {
addReferences(line, order)
}
internal/rpm/spec/tree_hoist.go:361
- A selected
%globalcan depend on a selected lazy%define, but this early return prevents validation from traversing that dependency. If%define base %{dep}is removed,%global root %{base}is later in the removed sections, and surviving%define dep newlies between the first removed section androot, the original eagerrootseesnewwhile the hoistedrootexpands before it and sees a different binding; this shape is currently accepted and silently changes the spec. Validate the complete eager dependency closure at both the original and insertion orders, or reject this case.
if definition == nil || definition.global {
return nil
internal/rpm/spec/tree_hoist.go:532
- This rejects every surviving dependency of a selected
%global, even when that dependency is already defined before the hoist insertion point. For example, a preamble%define dep /usr/libfollowed by a removed%global root %{dep}and a survivingecho %{root}is safe—the hoisted global still followsdep—but this returnsErrUnsafeMacroHoist. Compare the candidate's order withinsertionOrderinstead of treating all surviving candidates as unsafe.
if !candidate.removed || candidate.order >= definition.order {
return fmt.Errorf("cannot safely hoist eager '%%global' macro %#q before dependency %#q:\n%w",
definition.block.Name, dependency, ErrUnsafeMacroHoist)
}
internal/rpm/spec/tree_hoist.go:225
facts.undefinedrecords only whether a name was ever undefined, so any%undefinecauses a selected declaration to be rejected. A preamble%undefine foofollowed by a removed%define foo valueis safe: the hoisted definition remains after the preamble and still follows the undefine, but this returnsErrUnsafeMacroHoist. Track directive order/context and reject only when the relocation crosses a relevant%undefine.
if facts.undefined[definition.block.Name] {
return fmt.Errorf("cannot safely hoist macro %#q across '%%undefine':\n%w",
definition.block.Name, ErrUnsafeMacroHoist)
}
- Files reviewed: 7/7 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+186
to
+188
| if err := t.hoistReferencedMacros(sections); err != nil { | ||
| return err | ||
| } |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Removing a section or subpackage can also remove
%defineor%globaldeclarations that happen to live inside it. When surviving content still references one of those macros, the rendered spec is left with a dangling reference and often fails much later during the build.This PR teaches the structural editor to preserve the complete declarations that surviving content still needs. It follows transitive dependencies and keeps declaration ordering intact. When a relocation is ambiguous or clearly unsafe, the removal fails without modifying the spec.
Motivation
Issue #203 documents this with QEMU:
%define testsdirlives in the tests subpackage, but%installcontinues to use it after that subpackage is removed. The macro declaration must survive even though the package sections do not.Changes
ErrUnsafeMacroHoistwithout modifying the spec, including:%globaldependencies or redefinitions;%globaldeclarations;spec-remove-subpackagedrops%definemacros inside the removed subpackage that are referenced elsewhere #203 coverage and focused rejection tests.Validation
mage buildmage unitThe completed structural editor also rendered the full Azure Linux evaluation corpus without parser or overlay errors, including automatic preservation of QEMU's
testsdirmacro.Known limitations
This is conservative structural and lexical analysis, not RPM macro evaluation. If azldev cannot establish a safe binding, it rejects the removal instead of guessing. Conditional same-name bindings that would require evaluating the condition remain best-effort.