swyddfa / esbonio

A language server for working with Sphinx projects.
https://docs.esbon.io/
122 stars 21 forks source link

Add ability to set envrionment variables for the language server process #469

Open abergmeier opened 1 year ago

abergmeier commented 1 year ago

Inside Visual Studio Code I see Sphinx failing. Output is:

WARNING: kernel-doc '../scripts/kernel-doc -rst -enable-lineno -sphinx-version 5.3.0 -function pci_driver ../include/linux/pci.h' processing failed with: [Errno 2] No such file or directory: '../scripts/kernel-doc'
WARNING: kernel-doc '../scripts/kernel-doc -rst -enable-lineno -sphinx-version 5.3.0 -function pci_device_id ../include/linux/mod_devicetable.h' processing failed with: [Errno 2] No such file or directory: '../scripts/kernel-doc'
[esbonio.lsp] Traceback (most recent call last):
  File "/home/user/.local/lib/python3.10/site-packages/esbonio/lsp/sphinx/__init__.py", line 233, in build
    self.app.build()
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/application.py", line 347, in build
    self.builder.build_update()
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 310, in build_update
    self.build(to_build,
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 326, in build
    updated_docnames = set(self.read())
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 433, in read
    self._read_serial(docnames)
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 454, in _read_serial
    self.read_doc(docname)
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 510, in read_doc
    publisher.publish()
  File "/home/user/.local/lib/python3.10/site-packages/docutils/core.py", line 224, in publish
    self.document = self.reader.read(self.source, self.parser,
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/io.py", line 104, in read
    self.parse()
  File "/home/user/.local/lib/python3.10/site-packages/docutils/readers/__init__.py", line 76, in parse
    self.parser.parse(self.input, document)
  File "/home/user/.local/lib/python3.10/site-packages/sphinx/parsers.py", line 78, in parse
    self.statemachine.run(inputlines, document, inliner=self.inliner)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 169, in run
    results = StateMachineWS.run(self, input_lines, input_offset,
  File "/home/user/.local/lib/python3.10/site-packages/docutils/statemachine.py", line 233, in run
    context, next_state, result = self.check_line(
  File "/home/user/.local/lib/python3.10/site-packages/docutils/statemachine.py", line 445, in check_line
    return method(match, context, next_state)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 2785, in underline
    self.section(title, source, style, lineno - 1, messages)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 325, in section
    self.new_subsection(title, lineno, messages)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 391, in new_subsection
    newabsoffset = self.nested_parse(
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 279, in nested_parse
    state_machine.run(block, input_offset, memo=self.memo,
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 195, in run
    results = StateMachineWS.run(self, input_lines, input_offset)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/statemachine.py", line 233, in run
    context, next_state, result = self.check_line(
  File "/home/user/.local/lib/python3.10/site-packages/docutils/statemachine.py", line 445, in check_line
    return method(match, context, next_state)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 2355, in explicit_markup
    nodelist, blank_finish = self.explicit_construct(match)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 2367, in explicit_construct
    return method(self, expmatch)
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 2104, in directive
    return self.run_directive(
  File "/home/user/.local/lib/python3.10/site-packages/docutils/parsers/rst/states.py", line 2154, in run_directive
    result = directive_instance.run()
  File "/home/user/Projects/linux/Documentation/sphinx/kernel_abi.py", line 89, in run
    srctree = path.abspath(os.environ["srctree"])
  File "/usr/lib/python3.10/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'srctree'
alcarney commented 1 year ago

Looks the error is coming from a custom directive defined in kernel_api.py, and looking at the last few lines of the stack trace

  File "/home/user/Projects/linux/Documentation/sphinx/kernel_abi.py", line 89, in run
    srctree = path.abspath(os.environ["srctree"])   <---
  File "/usr/lib/python3.10/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'srctree'

it's trying to read from the srctree environment variable which, judging from the error is undefined.

Unfortunately, the Esbonio extension doesn't allow you to set environment variables directly, but I can think of a few workarounds