Skip to content

Commit 456869b

Browse files
committed
BUG: lunr.js: Fix Popen error when node isn't available
Fixes #446
1 parent 80697eb commit 456869b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

pdoc/cli.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from functools import lru_cache
1717
from http.server import BaseHTTPRequestHandler, HTTPServer
1818
from pathlib import Path
19-
from typing import Dict, List, Sequence
19+
from typing import Dict, List, Optional, Sequence
2020
from warnings import warn
2121

2222
import pdoc
@@ -419,22 +419,24 @@ def to_url_id(module):
419419

420420
json_values = [dict(obj, url=urls[obj['url']]) for obj in index]
421421
cmd = ['node', str(Path(__file__).with_name('build-index.js'))]
422-
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
423-
main_path = args.output_dir
424-
if proc.poll() is None:
422+
proc = None
423+
try:
424+
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
425425
stdout, stderr = proc.communicate(json.dumps(json_values))
426426
assert proc.poll() == 0, proc.poll()
427-
if proc.returncode == 0:
428-
stdout = 'INDEX=' + stdout
429-
else:
430-
warn(f'Prebuilding Lunr index with command `{" ".join(cmd)}` failed: '
431-
f'{proc.stderr and proc.stderr.read() or ""!r}. '
432-
f'The search feature will still work, '
427+
except Exception as ex:
428+
stderr = proc and proc.stderr and proc.stderr.read() # type: ignore
429+
warn(f'Prebuilding Lunr index with command `{" ".join(cmd)}` failed with '
430+
f'"{ex.__class__.__name__}: {ex}". Stderr: {stderr or ""!r}. '
431+
f'Note, the search feature will still work, '
433432
f'but may be slower (with the index rebuilt just before use). '
434433
f'To prebuild an index in advance, ensure `node` is executable in the '
435434
f'pdoc environment.', category=RuntimeWarning)
436435
stdout = ('URLS=' + json.dumps(urls, indent=0, separators=(',', ':')) +
437436
';\nINDEX=' + json.dumps(index, indent=0, separators=(',', ':')))
437+
else:
438+
stdout = 'INDEX=' + stdout
439+
main_path = args.output_dir
438440
index_path = Path(main_path).joinpath('index.js')
439441
index_path.write_text(stdout)
440442
print(str(index_path))

0 commit comments

Comments
 (0)