From 2da39317b20c1f5e1ff5873c56ace6b5f5f28e2d Mon Sep 17 00:00:00 2001 From: ohemorange Date: Fri, 25 Apr 2025 13:11:28 -0700 Subject: [PATCH] Replace pyparsing error that usually misdirects people with a more helpful message (#10265) Addresses #10264, though I could not actually find a way to fix that particular issue. So, fixes #10264 is not actually accurate, but I would like github to link them. --- .../src/certbot_nginx/_internal/parser.py | 7 ++- .../_internal/tests/nginxparser_test.py | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/certbot-nginx/src/certbot_nginx/_internal/parser.py b/certbot-nginx/src/certbot_nginx/_internal/parser.py index a7bd7c8ff..4da91b0f6 100644 --- a/certbot-nginx/src/certbot_nginx/_internal/parser.py +++ b/certbot-nginx/src/certbot_nginx/_internal/parser.py @@ -233,8 +233,11 @@ class NginxParser: logger.warning("Could not read file: %s due to invalid " "character. Only UTF-8 encoding is " "supported.", filename) - except pyparsing.ParseException as err: - logger.warning("Could not parse file: %s due to %s", filename, err) + except pyparsing.ParseException: + logger.warning("Could not parse file: %s. This is usually due to a comment that " + "certbot cannot parse, such as between a block's name and definition or " + "within a string literal. Moving the comment to another location in the file " + "or deleting it may resolve the issue.", filename) return trees def _find_config_root(self) -> str: diff --git a/certbot-nginx/src/certbot_nginx/_internal/tests/nginxparser_test.py b/certbot-nginx/src/certbot_nginx/_internal/tests/nginxparser_test.py index bc6dc260f..f53f6a40f 100644 --- a/certbot-nginx/src/certbot_nginx/_internal/tests/nginxparser_test.py +++ b/certbot-nginx/src/certbot_nginx/_internal/tests/nginxparser_test.py @@ -374,6 +374,49 @@ class TestRawNginxParser(unittest.TestCase): """ loads(test) + def test_location_comment_issue(self): + # See discussion at https://github.com/certbot/certbot/issues/10264 + already_good = ''' + location = /resume + # x + { rewrite .* /Files/Adam_Lein_resume.pdf redirect; } + ''' + loads(already_good) + already_good = ''' + location = /resume + { rewrite .* /Files/Adam_Lein_resume.pdf redirect; } + # { + ''' + loads(already_good) + needs_fixing = ''' + location = /resume + # { + { rewrite .* /Files/Adam_Lein_resume.pdf redirect; } + ''' + with pytest.raises(ParseException): + loads(needs_fixing) # fails + needs_fixing = ''' + location = /resume + # x{ + { rewrite .* /Files/Adam_Lein_resume.pdf redirect; } + ''' + with pytest.raises(ParseException): + loads(needs_fixing) # fails + needs_fixing = ''' + location = /resume + #{ + { rewrite .* /Files/Adam_Lein_resume.pdf redirect; } + ''' + with pytest.raises(ParseException): + loads(needs_fixing) # fails + needs_fixing = ''' + location = /resume + # {x + { rewrite .* /Files/Adam_Lein_resume.pdf redirect; } + ''' + with pytest.raises(ParseException): + loads(needs_fixing) # fails + class TestUnspacedList(unittest.TestCase): """Test the UnspacedList data structure"""