Skip to content

Conversation

@nforro
Copy link
Member

@nforro nforro commented Dec 17, 2025

Macro expansion can have sideeffects and modify other macros, which becomes a problem when the spec file is processed line by line to determine line validity. Even lines in false condition branches are expanded, and if the expansion has sideeffects it can affect further processing and condition evaluation.

Try to prevent that by temporarily overriding macros that can affect other macros, making them noop during expansion.

Unfortunately overriding doesn't work on EL9 and EL8 where RPM behaves differently.

Fixes #498.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @nforro, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue where RPM macro expansion could inadvertently introduce side effects by modifying other macros, particularly problematic when processing spec files line by line where even unexecuted conditional branches are expanded. To mitigate this, a new "safe" expansion mode has been implemented in the Macros.expand method. This mode temporarily neutralizes macro-altering directives like global, define, and undefine during expansion, ensuring that the process is purely evaluative and does not alter the global macro state, thereby preventing unexpected behavior and incorrect condition evaluations.

Highlights

  • Side-effect Prevention: Implemented a mechanism to prevent macro expansion from causing unintended side effects by modifying other macros.
  • Safe Expansion Mode: Introduced a safe parameter to the Macros.expand method which, when enabled, temporarily overrides macro-manipulating directives (global, define, undefine) to be no-ops during expansion.
  • Contextual Macro Handling: Ensures that macro expansions, especially within conditional branches, do not inadvertently alter the global macro state, resolving potential issues with line validity processing.
  • New Test Case: Added test_macros_sideeffects to validate the new safe expansion behavior, confirming that macros are not modified when safe mode is active.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a 'safe' mode for macro expansion to prevent side effects, which is a good approach to solve the described issue. The implementation correctly overrides and restores macros that could modify other macros. The accompanying test case effectively validates the new functionality. I've provided a couple of suggestions in specfile/macros.py to improve code readability and efficiency, mainly by simplifying the try-except-finally structure and using a more idiomatic loop for cleanup operations.

@centosinfra-prod-github-app
Copy link
Contributor

@centosinfra-prod-github-app
Copy link
Contributor

@nforro
Copy link
Member Author

nforro commented Dec 17, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a 'safe' mode for macro expansion to prevent side effects, which is a solid approach to address the problem of unintended macro modifications during spec file processing. The implementation correctly uses a try...finally block to ensure the state of macros is restored after expansion. The accompanying test case effectively verifies the new functionality. I have one suggestion to make the cleanup logic more idiomatic and efficient.

@centosinfra-prod-github-app
Copy link
Contributor

Macro expansion can have sideeffects and modify other macros,
which becomes a problem when the spec file is processed line by line
to determine line validity. Even lines in false condition branches
are expanded, and if the expansion has sideeffects it can affect
further processing and condition evaluation.

Try to prevent that by temporarily overriding macros that can affect
other macros, making them noop during expansion.

Signed-off-by: Nikola Forró <[email protected]>
@centosinfra-prod-github-app
Copy link
Contributor

@nforro nforro added the mergeit Merge via Zuul label Dec 17, 2025
@centosinfra-prod-github-app
Copy link
Contributor

@centosinfra-prod-github-app centosinfra-prod-github-app bot merged commit a7d535e into main Dec 17, 2025
47 of 49 checks passed
@github-project-automation github-project-automation bot moved this from new to done in Packit Kanban Board Dec 17, 2025
@nforro nforro deleted the macros branch December 17, 2025 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mergeit Merge via Zuul

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Macros with lua function calls fail to expand

3 participants