🐛 fix(config): ignore a config file that fails to parse instead of crashing - #3230
Open
darrenhuai wants to merge 2 commits into
Open
🐛 fix(config): ignore a config file that fails to parse instead of crashing#3230darrenhuai wants to merge 2 commits into
darrenhuai wants to merge 2 commits into
Conversation
…ashing
IniConfig only sets has_virtualenv_section after a successful parse, but
__bool__ reads it unconditionally, so a virtualenv.ini that configparser
rejects took every invocation down with
AttributeError: 'IniConfig' object has no attribute 'has_virtualenv_section'
right after logging that the file would be ignored. The "failed to parse"
state in the help epilog was unreachable for the same reason.
The easiest way to hit this on Windows is a UTF-8 BOM: PowerShell 5's
Out-File and older Notepad both write one, and configparser then sees
"\ufeff[virtualenv]" as a line outside any section. Read the file with
utf-8-sig so a BOM is tolerated, and when parsing does fail, mark the
config as failed to parse so it is skipped and the epilog says why.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Thanks for contributing, make sure you address all the checklists (for details on how see development documentation)
tox -e fix)docs/changelogfolderIniConfig.__init__is written to survive a config file it cannot parse: it catches the exception, logsfailed to read config file ... because ..., and there is a"failed to parse"entry inSTATEfor the help epilog. Buthas_virtualenv_sectionis only assigned after a successful_load(), and__bool__reads it unconditionally:VirtualEnvConfigParser._fix_defaultsevaluatesif outcome is None and self.file_config:for every option, so the first one without an env var override blows up. Anyvirtualenv.inithatconfigparserrejects takes every invocation down, right after the log line saying the file would be ignored:The
"failed to parse"epilog state was unreachable for the same reason; a file that failed to parse still reported itself asactive.The BOM above is the way I expect people actually hit this. On Windows,
Out-File -Encoding utf8in PowerShell 5 and older versions of Notepad both write a UTF-8 BOM, andconfigparserthen sees\ufeff[virtualenv]as a line outside any section. A stray line inside the section or a missing header fail the same way.Three changes in
IniConfig:has_virtualenv_section = Falsebefore trying to load, so__bool__is always answerablehas_config_file = None, which is the stateSTATEalready reserves for"failed to parse"; the config is then skipped and--helpsays why instead ofactiveutf-8-sigso a BOM is stripped rather than fatal; it is a no-op for files without oneWith the fix the three files above all produce a working
--help. The BOM one is parsed normally (clear = Truetakes effect), and the other two are ignored with the error logged and the epilog readingconfig file ... failed to parse.Three tests: a bad file is ignored and reported (
bool(config)isFalse, the epilog saysfailed to parse, the error is logged), a bad file does not breaksession_via_cli, and a BOM-prefixed file is read. All three fail onmain, and I checked that reverting each of the three source lines individually makes exactly one of them fail.tests/unitis 314 passed / 40 skipped on Windows 3.14;ruffandty check src/virtualenvare clean.