Skip to content
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

tox.ini syntax error messages #1692

Closed
jayvdb opened this issue Oct 17, 2020 · 3 comments
Closed

tox.ini syntax error messages #1692

jayvdb opened this issue Oct 17, 2020 · 3 comments
Labels
feature:new something does not exist yet, but should

Comments

@jayvdb
Copy link

jayvdb commented Oct 17, 2020

These two ini files cause rather unhelpful error backtraces. The first exception has a line number which could be used to provide a useful error message. The second doesnt have a line number (that might be fixable upstream in iniconfig), and the testenv name is hidden in the py3 backtrace("python" in ...ConfigError: python failed..) so that could be made more prominent.

Obviously these are contrived ini files; they are intentionally minimal test cases to show what can happen if a \ is accidentally misplaced. Reading iniconfig source, it looks like similar errors can be triggered with misplaced : , and semantic errors like duplicate or missing section names or item names.

\
[tox]
foo = 1
[testenv]
setenv = \

Backtraces respectively:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/py/_vendored_packages/iniconfig.py", line 126, in _parseline
    name, value = line.split('=', 1)
ValueError: not enough values to unpack (expected 2, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/py/_vendored_packages/iniconfig.py", line 131, in _parseline
    name, value = line.split(":", 1)
ValueError: not enough values to unpack (expected 2, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/tox", line 8, in <module>
    sys.exit(cmdline())
  File "/usr/lib/python3.8/site-packages/tox/session/__init__.py", line 44, in cmdline
    main(args)
  File "/usr/lib/python3.8/site-packages/tox/session/__init__.py", line 64, in main
    config = load_config(args)
  File "/usr/lib/python3.8/site-packages/tox/session/__init__.py", line 80, in load_config
    config = parseconfig(args)
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 269, in parseconfig
    ParseIni(config, config_file, content)
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 1038, in __init__
    self._cfg = py.iniconfig.IniConfig(config.toxinipath, ini_data)
  File "/usr/lib/python3.8/site-packages/py/_vendored_packages/iniconfig.py", line 54, in __init__
    tokens = self._parse(iter(f))
  File "/usr/lib/python3.8/site-packages/py/_vendored_packages/iniconfig.py", line 83, in _parse
    name, data = self._parseline(line, lineno)
  File "/usr/lib/python3.8/site-packages/py/_vendored_packages/iniconfig.py", line 133, in _parseline
    self._raise(lineno, 'unexpected line: %r' % line)
  File "/usr/lib/python3.8/site-packages/py/_vendored_packages/iniconfig.py", line 77, in _raise
    raise ParseError(self.path, lineno, msg)
py._vendored_packages.iniconfig.ParseError: /home/jayvdb/tmp/invalid-tox-ini/tox.ini:1: unexpected line: '\\'
Traceback (most recent call last):
  File "/usr/bin/tox", line 8, in <module>
    sys.exit(cmdline())
  File "/usr/lib/python3.8/site-packages/tox/session/__init__.py", line 44, in cmdline
    main(args)
  File "/usr/lib/python3.8/site-packages/tox/session/__init__.py", line 64, in main
    config = load_config(args)
  File "/usr/lib/python3.8/site-packages/tox/session/__init__.py", line 80, in load_config
    config = parseconfig(args)
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 269, in parseconfig
    ParseIni(config, config_file, content)
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 1181, in __init__
    raise tox.exception.ConfigError(
tox.exception.ConfigError: ConfigError: python failed with not enough values to unpack (expected 2, got 1) at Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 1157, in run
    results[name] = cur_self.make_envconfig(name, section, subs, config)
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 1305, in make_envconfig
    res = meth(env_attr.name, env_attr.default, replace=replace)
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 1524, in getdict_setenv
    definitions = self._getdict(value, default=default, sep=sep, replace=replace)
  File "/usr/lib/python3.8/site-packages/tox/config/__init__.py", line 1535, in _getdict
    name, rest = line.split("=", 1)
ValueError: not enough values to unpack (expected 2, got 1)
@jayvdb jayvdb added the feature:new something does not exist yet, but should label Oct 17, 2020
@gaborbernat
Copy link
Member

Agreed we should have a more user-friendly error for these.

@jayvdb
Copy link
Author

jayvdb commented Jan 11, 2021

I suspect a non-empty file without a section header resulting in py._vendored_packages.iniconfig.ParseError tox.ini:2: no section header defined is not nicely handled.

@gaborbernat gaborbernat added this to the 4.1 milestone Mar 1, 2021
@gaborbernat
Copy link
Member

Traceback (most recent call last):
  File "/home/bernat/.local/bin/tox4", line 33, in <module>
    sys.exit(load_entry_point('tox', 'console_scripts', 'tox4')())
  File "/home/bernat/git/tox/src/tox/run.py", line 18, in run
    result = main(sys.argv[1:] if args is None else args)
  File "/home/bernat/git/tox/src/tox/run.py", line 37, in main
    state = setup_state(args)
  File "/home/bernat/git/tox/src/tox/run.py", line 52, in setup_state
    options = get_options(*args)
  File "/home/bernat/git/tox/src/tox/config/cli/parse.py", line 36, in get_options
    guess_verbosity, log_handler, source = _get_base(args)
  File "/home/bernat/git/tox/src/tox/config/cli/parse.py", line 51, in _get_base
    source = discover_source(parsed.config_file, parsed.root_dir)
  File "/home/bernat/git/tox/src/tox/config/source/discover.py", line 26, in discover_source
    src = _locate_source()
  File "/home/bernat/git/tox/src/tox/config/source/discover.py", line 40, in _locate_source
    return src_type(candidate)
  File "/home/bernat/git/tox/src/tox/config/source/ini.py", line 32, in __init__
    self._parser.read_string(content, str(path))
  File "/usr/lib/python3.10/configparser.py", line 724, in read_string
    self.read_file(sfile, source)
  File "/usr/lib/python3.10/configparser.py", line 719, in read_file
    self._read(f, source)
  File "/usr/lib/python3.10/configparser.py", line 1086, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
configparser.MissingSectionHeaderError: File contains no section headers.
file: '/tmp/magic/tox.ini', line: 1
'\\\n'
❯ tox4
py: internal error
Traceback (most recent call last):
  File "/home/bernat/git/tox/src/tox/session/cmd/run/single.py", line 45, in _evaluate
    tox_env.setup()
  File "/home/bernat/git/tox/src/tox/tox_env/api.py", line 226, in setup
    self._setup_env()
  File "/home/bernat/git/tox/src/tox/tox_env/python/runner.py", line 90, in _setup_env
    super()._setup_env()
  File "/home/bernat/git/tox/src/tox/tox_env/python/api.py", line 158, in _setup_env
    self.ensure_python_env()
  File "/home/bernat/git/tox/src/tox/tox_env/python/api.py", line 162, in ensure_python_env
    conf = self.python_cache()
  File "/home/bernat/git/tox/src/tox/tox_env/python/virtual_env/api.py", line 78, in python_cache
    base = super().python_cache()
  File "/home/bernat/git/tox/src/tox/tox_env/python/api.py", line 196, in python_cache
    "version_info": list(self.base_python.version_info),
  File "/home/bernat/git/tox/src/tox/tox_env/python/api.py", line 205, in base_python
    self._base_python = self._get_python(base_pythons)
  File "/home/bernat/git/tox/src/tox/tox_env/python/virtual_env/api.py", line 135, in _get_python
    interpreter = self.creator.interpreter
  File "/home/bernat/git/tox/src/tox/tox_env/python/virtual_env/api.py", line 127, in creator
    return self.session.creator
  File "/home/bernat/git/tox/src/tox/tox_env/python/virtual_env/api.py", line 107, in session
    env = self.virtualenv_env_vars()
  File "/home/bernat/git/tox/src/tox/tox_env/python/virtual_env/api.py", line 112, in virtualenv_env_vars
    env = self.environment_variables.copy()
  File "/home/bernat/git/tox/src/tox/tox_env/python/virtual_env/api.py", line 167, in environment_variables
    environment_variables = super().environment_variables
  File "/home/bernat/git/tox/src/tox/tox_env/runner.py", line 196, in environment_variables
    environment_variables = super().environment_variables
  File "/home/bernat/git/tox/src/tox/tox_env/api.py", line 301, in environment_variables
    set_env: SetEnv = self.conf["set_env"]
  File "/home/bernat/git/tox/src/tox/config/sets.py", line 112, in __getitem__
    return self.load(item)
  File "/home/bernat/git/tox/src/tox/config/sets.py", line 123, in load
    return config_definition.__call__(self._conf, self.loaders, ConfigLoadArgs(chain, self.name, self.env_name))
  File "/home/bernat/git/tox/src/tox/config/of_type.py", line 102, in __call__
    value = loader.load(key, self.of_type, self.factory, conf, args)
  File "/home/bernat/git/tox/src/tox/config/loader/api.py", line 127, in load
    converted = self.to(prepared, of_type, factory)
  File "/home/bernat/git/tox/src/tox/config/loader/convert.py", line 53, in to
    return factory(raw)
  File "/home/bernat/git/tox/src/tox/config/sets.py", line 225, in set_env_factory
    return SetEnv(raw, self.name, self.env_name, root)
  File "/home/bernat/git/tox/src/tox/config/set_env.py", line 29, in __init__
    key, value = self._extract_key_value(line)
  File "/home/bernat/git/tox/src/tox/config/set_env.py", line 67, in _extract_key_value
    raise ValueError(f"invalid line {line!r} in set_env")
ValueError: invalid line '\\' in set_env

Seems fixed in version 4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:new something does not exist yet, but should
Projects
None yet
Development

No branches or pull requests

2 participants