changeset 45777:0883413e09bc

config: move message about leading spaces in config to config.py When the config parser raises a ParseError, it uses the line that failed to parse as the error message. It doesn't currently tell the user anything about why it failed to parse. b13b99d39a46 (config: highlight parse error caused by leading spaces (issue3214), 2014-03-16) added a checked based on the error *message* having leading spaces. That has worked fine because only the config parser uses the line itself as error message (I think the revset and fileset parsers use more user-friendly proper messages). It still feels like a hack. Let's make the config parser give a useful message about leading whitespace instead. We should ideally follow up with more useful messages for other parse errors in config files. Differential Revision: https://phab.mercurial-scm.org/D9241
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 22 Oct 2020 10:57:11 -0700
parents 0fc8b066928a
children 6620caf427ec
files mercurial/config.py mercurial/dispatch.py tests/test-config.t tests/test-hgrc.t
diffstat 4 files changed, 7 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/config.py	Thu Oct 22 09:58:05 2020 -0700
+++ b/mercurial/config.py	Thu Oct 22 10:57:11 2020 -0700
@@ -200,7 +200,10 @@
                 self._unset.append((section, name))
                 continue
 
-            raise error.ParseError(l.rstrip(), (b"%s:%d" % (src, line)))
+            message = l.rstrip()
+            if l.startswith(b' '):
+                message = b"unexpected leading whitespace: %s" % message
+            raise error.ParseError(message, (b"%s:%d" % (src, line)))
 
     def read(self, path, fp=None, sections=None, remap=None):
         if not fp:
--- a/mercurial/dispatch.py	Thu Oct 22 09:58:05 2020 -0700
+++ b/mercurial/dispatch.py	Thu Oct 22 10:57:11 2020 -0700
@@ -248,8 +248,6 @@
             _(b"hg: parse error at %s: %s\n")
             % (pycompat.bytestr(inst.location), inst.message)
         )
-        if inst.message.startswith(b' '):
-            write(_(b"unexpected leading whitespace\n"))
     else:
         write(_(b"hg: parse error: %s\n") % inst.message)
         _reportsimilar(write, similar)
--- a/tests/test-config.t	Thu Oct 22 09:58:05 2020 -0700
+++ b/tests/test-config.t	Thu Oct 22 10:57:11 2020 -0700
@@ -25,8 +25,7 @@
   >  key=value
   > EOF
   $ hg showconfig
-  hg: parse error at $TESTTMP/.hg/hgrc:1:  key=value
-  unexpected leading whitespace
+  hg: parse error at $TESTTMP/.hg/hgrc:1: unexpected leading whitespace:  key=value
   [255]
 
   $ cat > .hg/hgrc << EOF
@@ -34,8 +33,7 @@
   > key=value
   > EOF
   $ hg showconfig
-  hg: parse error at $TESTTMP/.hg/hgrc:1:  [section]
-  unexpected leading whitespace
+  hg: parse error at $TESTTMP/.hg/hgrc:1: unexpected leading whitespace:  [section]
   [255]
 
 Reset hgrc
--- a/tests/test-hgrc.t	Thu Oct 22 09:58:05 2020 -0700
+++ b/tests/test-hgrc.t	Thu Oct 22 10:57:11 2020 -0700
@@ -68,8 +68,7 @@
   $ echo '[foo]' > $HGRC
   $ echo '  x = y' >> $HGRC
   $ hg version
-  hg: parse error at $TESTTMP/hgrc:2:   x = y
-  unexpected leading whitespace
+  hg: parse error at $TESTTMP/hgrc:2: unexpected leading whitespace:   x = y
   [255]
 
   $ "$PYTHON" -c "from __future__ import print_function; print('[foo]\nbar = a\n b\n c \n  de\n fg \nbaz = bif cb \n')" \