Skip to content

feat(render): diagnose added patch application - #433

Open
Nan Liu (liunan-ms) wants to merge 1 commit into
mainfrom
liunan/patch-diagnose
Open

Nan Liu (liunan-ms) wants to merge 1 commit into
mainfrom
liunan/patch-diagnose

Conversation

@liunan-ms

@liunan-ms Nan Liu (liunan-ms) commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Why this is needed

patch-add copies a patch and registers a PatchN: tag or %patchlist entry, but registration alone does not establish that preparation applies it. A package can build without the intended downstream fix, or fail later because the patch
paths and strip level do not agree.

Related open issues:

  • #408:
    added patches can be silently skipped in specs with hand-written %patch
    sequences. This PR makes missing recognized application visible during rendering.
  • #409:
    bare %autosetup can use an incompatible strip level for subdirectory patches.
    This PR reports likely path/strip mismatches before the build.
  • #423:
    proposed strip-aware application and final-render diagnostics. This PR implements
    the diagnostic portion using a deliberately narrower static-analysis contract.

These are related/partially addressed issues, not automatic-closing references:
this PR does not implement a strip configuration field, insert application commands, or rewrite existing preparation commands.

Changes

  • Inspect the final spec and final added-patch contents after all overlays, including overlays that repair application or remove a declaration.
  • Share read-only lexical analysis between the legacy and experimental structural editors.
  • Recognize %patch forms, %autopatch, %autosetup, and supported %forgeautosetup / %goprep behavior, including disabled patching and ranges.
  • Check relevant unconditional macro-helper redefinitions, execution hazards, backend behavior, and likely header-path/strip mismatches.
  • Expose per-component patchWarnings in render JSON and add --patch-warnings=warn|error.
  • Update user reference, generated CLI documentation, both emitted agent-guidance variants, unit/integration tests, and CLI snapshots.

Decision flow

For each supplied overlay-added patch:

  1. Find its declaration in the final spec. If a later overlay removed it, skip it.
  2. Warn if its final file is missing, its declaration is conditional/repeated, or another declaration uses its known number.
  3. Keep explicit/implicit numbering information. An unknown number can match an unrestricted bulk command, but cannot establish a numbered/range selection.
  4. Require one ordinary unconditional %prep. Repeated, conditional, augmented (%prep -a / %prep -p), or malformed preparation remains unverified.
  5. Collect supported unconditional application candidates:
    • Respect -N, wrapper defaults, explicit selections, inclusive ranges, backend, and strip options.
    • Accept braced directory-name macros in -n without evaluating them.
    • Capture relevant preceding unconditional directory/control-flow hazards and invoked helper redefinitions; do not invalidate earlier candidates because of unrelated later commands.
  6. If any selecting candidate has no detected relevant problem, emit no diagnostic. Otherwise report a candidate's problem or the absence of recognized unconditional selection.

Ordinary preamble macros, unrelated generated definitions, and opaque surrounding commands do not veto an otherwise understood selection. Conditional commands and their side effects are skipped. Duplicate application is deliberately not detected. Loaded macro files and macro expressions are not evaluated.

Rendering policy

Finding Default --patch-warnings=warn --patch-warnings=error
Missing unconditional selection, including a simple missing/excluding command Warning; component can render Component error
Unsupported/uncertain preparation or likely strip mismatch Warning; component can render Component error

--fail-on-error controls whether component failures cause a nonzero invocation exit. Successful components retain their output when another component fails.
--check-only retains its existing freshness/drift semantics.

Validation

Automated checks

The following commands passed during implementation/review:

  • mage fix all - passed.
  • mage unit - passed, including both-editor selection/numbering/helper regressions
    and render JSON/default/strict-policy tests.
  • mage check all - passed.
  • mage docs - passed; CLI reference regenerated.
  • TESTING_DISABLE_NETWORK_TESTS=1 mage scenarioUpdate - passed; snapshots updated.
  • TESTING_DISABLE_NETWORK_TESTS=1 mage all - passed: build, unit tests,
    formatting/lint, static analysis, licenses, offline scenarios, and docs/schema.

Rendered 56 components with patch-add overlays

3 patch warnings:

Component Added patch Reason Warning
fido-device-onboard pin-libcryptsetup-rs-0.13.2.patch (Patch1001) Application commands are conditional; no unconditional selection. The RHEL range ends at 999. Conditional patch application cannot be verified for every build configuration
python-blivet bb696202-fix-cpe-2.3-version-parsing.patch (Patch104) Applying %autosetup uses additional archive extraction (-b1), outside the modeled setup flow. Custom or multi-source setup cannot be resolved
uv 0001-use-python314-tests.patch (Patch4) %prep -a is intentionally unsupported; implicit build-system preparation is not modeled. Multiple, conditional, or nonstandard preparation sections cannot be resolved

A separate negative check intentionally removed the application overlay for golang-github-gorilla-sessions: the patch remained declared, while bare %goprep and cp supplied no recognized application. Rendering correctly returned
status: "ok" with a missing-unconditional-selection warning under default policy.

Copilot AI balanced review requested due to automatic review settings November 11, 2026 00:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Active analyzer defects can silently accept unapplied patches and falsely reject valid components in strict mode because function declarations, macro state, helper overrides, and diff hunk boundaries are mishandled.

4 open issues
What changed in this PR

Adds static diagnostics for overlay-added patches during source preparation and rendering, partially addressing #408, #409, and #423 without inserting application commands or changing strip levels.

Changes:

  • Shares final-spec patch analysis across both editors to identify missing selection, uncertain application, and likely strip mismatches.
  • Adds render JSON patchWarnings and the --patch-warnings=warn|error policy.
  • Updates regression coverage, CLI snapshots, user documentation, and both agent-guidance variants.
File Description
scenario/​component_render_test.go Tests warning JSON and strict rendering behavior.
scenario/​clismoke_test.go Adds render-help snapshot coverage.
scenario/​__snapshots__/​TestSnapshots_component_render_help_stdout_1.snap Records render help and warning policy.
scenario/​__snapshots__/​TestSnapshots_component_render_help_stderr_1.snap Records help startup logging.
scenario/​__snapshots__/​TestSnapshots_component_render_help_1.snap.json Records successful help exit status.
internal/​rpm/​spec/​patch_application.go Implements shared lexical patch-application analysis.
internal/​rpm/​spec/​patch_application_test.go Covers selection, macros, execution hazards, and paths.
internal/​app/​azldev/​core/​sources/​sourceprep.go Invokes diagnostics after overlays and adds a reporting callback.
internal/​app/​azldev/​core/​sources/​patch_application.go Reads final patch contents and reports diagnostics.
internal/​app/​azldev/​core/​sources/​patch_application_internal_test.go Tests final-overlay analysis and reporting.
internal/​app/​azldev/​cmds/​component/​render.go Adds warning policy and preserves diagnostics in results.
internal/​app/​azldev/​cmds/​component/​render_test.go Tests flag registration and policy validation.
internal/​app/​azldev/​cmds/​component/​render_patch_warnings_internal_test.go Tests JSON, strict rejection, freshness checks, and failures.
internal/​app/​azldev/​agentskill/​content/​withoutlockfile/​overlays.md.tmpl Documents diagnostics for lockfile-free workflows.
internal/​app/​azldev/​agentskill/​content/​overlays.md.tmpl Documents diagnostics for default workflows.
internal/​app/​azldev/​agentskill/​agentskill_test.go Checks diagnostic guidance in both modes.
docs/​user/​reference/​config/​overlays.md Explains analysis limits and strict rendering policy.
docs/​user/​reference/​cli/​azldev_component_render.md Updates generated render command reference.

Review effort: Balanced

Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Copilot AI balanced review requested due to automatic review settings November 11, 2026 01:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved parsing issues falsely reject valid patch applications and allow patches running in a subshell’s changed directory to pass strict checks without a diagnostic.

3 open issues
4 resolved since last review

Review effort: Balanced

Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Copilot AI balanced review requested due to automatic review settings November 11, 2026 01:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The scanner misses assignment- and builtin-prefixed commands that can skip or misdirect patch application even under strict rendering, while misclassified backslashes can also cause valid components to be rejected.

2 open issues
3 resolved since last review

Review effort: Balanced

Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Copilot AI balanced review requested due to automatic review settings November 11, 2026 01:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical control-flow gap ignores ! exit 0, allowing an unreachable %patch0 -p1 to pass without diagnostics even in strict mode; both editors need a fix and regression coverage.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread internal/rpm/spec/patch_application.go Resolved
Copilot AI balanced review requested due to automatic review settings November 11, 2026 18:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical parsing and backend-state gaps suppress diagnostics for missing or unreachable patch applications and incorrect strip levels, including under strict policy.

4 open issues
1 resolved since last review

Review effort: Balanced

Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Comment thread internal/rpm/spec/patch_application.go Resolved
Copilot AI balanced review requested due to automatic review settings November 11, 2026 23:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Patch parsing mistakes commit-message prose for a file header, causing false path warnings and strict rendering to reject valid patches.

1 open issue
4 resolved since last review

Review effort: Balanced

Comment thread internal/rpm/spec/patch_application.go Resolved
Check final specs for supported unconditional application of overlay-added patches and report selection uncertainty or likely strip mismatches.

Expose patchWarnings in render JSON and add a strict warning policy, with regression tests and updated user and agent documentation.
Copilot AI balanced review requested due to automatic review settings November 12, 2026 22:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Strict rejection relies on substantial RPM/shell modeling, so a maintainer should validate %forgeautosetup/%goprep wrapper behavior and strip defaults against supported distro macro versions.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@liunan-ms
Nan Liu (liunan-ms) marked this pull request as ready for review November 13, 2026 00:48
@Kanishk-Bansal

Copy link
Copy Markdown

Hi Nan Liu (@liunan-ms), thanks for picking this up! I built fd727c7 and rendered 13 real 4.0 components with a CVE-style patch added. I ran it twice: once with just patch-add, and once with the extra overlays our security pipeline writes today.

What worked well

  • Every warning I got was correct. Specs with hand-written %patch lines (opensc, vim, alsa-lib) were flagged as "nothing applies this". Bare %autosetup/%autopatch with a subdirectory patch (expat, crash-trace-command, biosdevname) was flagged for the missing -p.
  • Once a later overlay added the apply line, the warning went away, so checking the final spec works as described.
  • %goprep and %forgeautosetup are understood, and there were no false alarms on curl, fscrypt, augeas or containerd.
  • patchWarnings in -O json is exactly what we need for automation. We can drop or flag a patch without scraping logs.
  • --patch-warnings=error with --fail-on-error behaves as expected: the component gets status: error and the run exits 1.

A few things I noticed

  1. Double application isn't caught. I know the description calls this out, but it's the one case where something broken rendered clean. A patch-add plus an extra %patch line in a %forgeautosetup -p1 spec (augeas) or a %goprep -p1 spec (containerd) passes, but %prep would apply the patch twice. Even a simple "selected by more than one unconditional command" warning would help, maybe as a follow-up.
  2. The strip check only looks at the a//b/ prefix. A patch with --- lib/url.c in a -p1 spec (curl) passes, but -p1 leaves url.c, which won't be found. I get that you can't tell pkg-1.0/lib/x.c from lib/x.c without the source tree, so even a softer hint would be useful.
  3. With --patch-warnings=error, the rejected component's specs/<x>/<pkg>/ folder is replaced by a RENDER_FAILED marker. That makes sense for a failed render, but it surprised me for what feels like a policy check. A line in the docs would save people some confusion.
  4. Small wording thing. For containerd with a vendor/ patch, the reason says "goprep removes vendor after applying patches unless '-k' is used". The verdict is right, but in containerd the vendor tarball is unpacked after %goprep, so the message sends the reader the wrong way. Maybe something like "patch targets vendor/, which isn't present when this command applies patches"?
  5. Some warnings are conservative. findutils (a here-document before a Git-backend %autopatch) and nss (pushd around %autopatch -M 99) get warnings even though they're fine. Being conservative is totally fine for a first version. I'm only mentioning it in case allowing a here-document that doesn't change directory is cheap.

Looking forward to the strip part, and happy to test it the same way when it's up.

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.

3 participants