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"""