Skip to content

spec-remove-section / spec-remove-subpackage produce invalid specs when sections are inside %if/%endif #144

@reubeno

Description

@reubeno

Drafted by GitHub Copilot CLI while reviewing PR #132.

Spec.RemoveSection and Spec.RemoveSubpackage (internal/rpm/spec/edit.go) operate on whole-section line ranges and have no awareness of %if/%endif conditionals. When a section is wrapped in a conditional block, the %endif is typically consumed as part of the trailing removed section, while the matching %if remains in place. The result is a syntactically invalid spec written to disk; the build then fails far away with a confusing error.

This limitation is honestly documented for both overlays in docs/user/reference/config/overlays.md and in the Go doc-comment on RemoveSubpackage, but the API still returns nil and silently writes a broken spec.

Reproduction

Name: test
Version: 1.0

%description
Main.

%if 0%{?with_devel}
%package devel
Summary: Devel files

%description devel
Devel description.

%files devel
/usr/include/test.h
%endif

%files
/usr/bin/test

Applying:

[[components.test.overlays]]
type = "spec-remove-subpackage"
package = "devel"

…produces:

Name: test
Version: 1.0

%description
Main.

%if 0%{?with_devel}
%files
/usr/bin/test

The %endif was consumed as part of the trailing %files devel section (it sits between that section header and the next section header), and the %if is now unbalanced.

Proposed fix

Spec.collectSectionRanges already runs inside Visit, whose context tracks conditionalDepthChange. Detect ranges that:

  1. Start at non-zero conditional depth, or
  2. Have a non-zero net conditional balance change across the range.

In either case, return a clear, actionable error (e.g. "section %q (package=%q) is wrapped in %if/%endif; remove the conditional first or use spec-search-replace") instead of writing invalid output.

Apply uniformly to RemoveSection and RemoveSubpackage. After the fix, update the limitation prose in docs/user/reference/config/overlays.md to describe the new failure mode (clear error at overlay-application time) rather than the silent-corruption mode.

Workaround until fixed

Use a spec-search-replace overlay to remove the conditional block, or remove the %if/%endif first via additional overlays before applying the section/sub-package removal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: overlaysOverlay system: spec and file modifications driven by TOML declarationsbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions