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
Fix YAML error message when error is at the end of the file #73241
Fix YAML error message when error is at the end of the file #73241
Conversation
If a YAML file fails to load due to a syntax error in a file, or there is an error in the last line of a file, PyYAML reports the last line number of the file as the index where the error occurred. When reading the file lines, we use that index to the get the relevant line. If the index value is out of range, the relevant line is lost for error reporting. Subtract one from the index value to avoid the IndexError in this specific scenario. It is possible to still get an IndexError, which will be handled as it is currently.
Hrm, looks like the message does change, but it still references the wrong line number when I use the example from the original reported issue (with an invalid, two-line role Without your change:
With your change:
|
@Shrews Unfortunately, that's expected. That's the position we get from YAML. Since it's an "end of stream" error, the error address is effectively the last line and column of the file. I'm not sure how we would go about getting the "correct" position in this situtaion. ansible/lib/ansible/parsing/utils/yaml.py Line 37 in 30d9399
|
…file (ansible#73241) * Fix YAML error message when error is at the end of the file If a YAML file fails to load due to a syntax error in a file, or there is an error in the last line of a file, PyYAML reports the last line number of the file as the index where the error occurred. When reading the file lines, we use that index to the get the relevant line. If the index value is out of range, the relevant line is lost for error reporting. Subtract one from the index value to avoid the IndexError in this specific scenario. It is possible to still get an IndexError, which will be handled as it is currently. * Update existing tests and add new tests (cherry picked from commit e8d4b62) Co-authored-by: Sam Doran <sdoran@redhat.com>
…ile (ansible#73241) * Fix YAML error message when error is at the end of the file If a YAML file fails to load due to a syntax error in a file, or there is an error in the last line of a file, PyYAML reports the last line number of the file as the index where the error occurred. When reading the file lines, we use that index to the get the relevant line. If the index value is out of range, the relevant line is lost for error reporting. Subtract one from the index value to avoid the IndexError in this specific scenario. It is possible to still get an IndexError, which will be handled as it is currently. * Update existing tests and add new tests (cherry picked from commit e8d4b62) Co-authored-by: Sam Doran <sdoran@redhat.com>
…ile (#73241) (#73297) If a YAML file fails to load due to a syntax error in a file, or there is an error in the last line of a file, PyYAML reports the last line number of the file as the index where the error occurred. When reading the file lines, we use that index to the get the relevant line. If the index value is out of range, the relevant line is lost for error reporting. Subtract one from the index value to avoid the IndexError in this specific scenario. It is possible to still get an IndexError, which will be handled as it is currently. * Update existing tests and add new tests (cherry picked from commit e8d4b62) Co-authored-by: Sam Doran <sdoran@redhat.com>
…file (#73241) (#73296) If a YAML file fails to load due to a syntax error in a file, or there is an error in the last line of a file, PyYAML reports the last line number of the file as the index where the error occurred. When reading the file lines, we use that index to the get the relevant line. If the index value is out of range, the relevant line is lost for error reporting. Subtract one from the index value to avoid the IndexError in this specific scenario. It is possible to still get an IndexError, which will be handled as it is currently. * Update existing tests and add new tests (cherry picked from commit e8d4b62) Co-authored-by: Sam Doran <sdoran@redhat.com>
SUMMARY
If a YAML file fails to load due to a syntax error in a file, or there is an error in the last line of a file, PyYAML reports the last line number of the file as the index where the error occurred.
When reading the file lines, we use that index to the get the relevant line. If the index value is out of range, the relevant line is lost for error reporting.
Subtract one from the index value to avoid the IndexError in this specific scenario. It is possible to still get an IndexError, which will be handled as it is currently.
Fixes #16456
ISSUE TYPE
COMPONENT NAME
lib/ansible/errors/__init__.py
ADDITIONAL INFORMATION
The additional code that tries to avoid empty lines in the error message is not necessary for this fix. I can omit that if others see problems with it.