Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

envconfig: fix unhandled exception when cross-file lacks required keys #14397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

XueSongTap
Copy link

Problem

When users forget to specify the required endian parameter in a cross-compilation configuration file, Meson currently throws an unhandled KeyError exception. This creates confusion as the error message doesn't clearly indicate what's missing, and instead suggests that this is a Meson bug that should be reported.

Solution

Modified the condition check in the MachineInfo.from_literal method from:

if set(literal) < minimum_literal:

to:

if minimum_literal - set(literal):

This ensures that when mandatory fields are missing, users receive a clear error message explicitly stating which fields are missing, rather than encountering an unhandled exception.

Related Issue

Fixes #14385

@XueSongTap XueSongTap requested a review from jpakkane as a code owner March 23, 2025 16:29
@bonzini
Copy link
Collaborator

bonzini commented Mar 23, 2025

Thanks for the nice commit message! The change is fine but if you want to add a unit test, it would be nice to have.

@bonzini bonzini added this to the 1.7.1 milestone Mar 23, 2025
@bonzini bonzini added the exception Major bug that raises a python-level exception label Mar 23, 2025
@eli-schwartz
Copy link
Member

Can you update the commit message to mention that this regressed in commit b0d2a92

The commit message doesn't really explain why this is a fix (the context from that commit will help, and the comment I left on the linked ticket). This will make it hard to tell from the commit log, why the change was made.

Fix the unhandled KeyError exception that occurs when cross-compilation
configuration files are missing required parameters (such as 'endian').

This issue was introduced in commit b0d2a92 (PR mesonbuild#11692), where code changes
added optional keys but didn't update the key validation logic:
- Previously, minimum_literal contained both all possible keys and all required keys
- Using set(literal) < minimum_literal correctly detected missing required keys
- After adding optional keys, minimum_literal contained non-required optional keys,
  breaking the validation logic
- When required keys were missing, the code continued execution and later threw
  an unhandled KeyError when trying to access the non-existent keys

Changed the condition check from:
  if set(literal) < minimum_literal:
to:
  if minimum_literal - set(literal):

This correctly detects which required keys are missing and provides users with
clear error messages instead of raising unhandled exceptions.

Fixes mesonbuild#14385
@XueSongTap
Copy link
Author

@eli-schwartz Thank you for your feedback. I've amended the commit message to include the information about regression in commit b0d2a92 and provided more context on why this fix works.

I've force-pushed the amended commit with the updated message. Could you please review and confirm if the commit message is now acceptable?

Once we agree on the commit message, I plan to add a separate commit with unit tests as suggested by @bonzini.

@eli-schwartz
Copy link
Member

Nit:

After adding optional keys, minimum_literal contained non-required optional keys,

This isn't really true. The minimum (required) literal doesn't contain non required optional keys, that's why it can still be used in the fix. But the previous check was that the provided keys were a strict subset of the minimum required keys.

The strict subset logic broke down once the provided keys aren't a strict subset anymore, but an overlapping set with disjoint elements on both sides of the comparison.

Rewriting the logic to use "present in required but not present in provided" fixed that, and in effect removed the accidental implicit check "and not any {{ present in provided but not present in required }}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exception Major bug that raises a python-level exception
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unhandled python exception: KeyError with endian
3 participants