Skip to content

Commit

Permalink
BUG: lunr.js: Fix Popen error when node isn't available
Browse files Browse the repository at this point in the history
Fixes #446
  • Loading branch information
kernc committed Jun 25, 2024
1 parent 80697eb commit 4220d22
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pdoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,22 +419,24 @@ def to_url_id(module):

json_values = [dict(obj, url=urls[obj['url']]) for obj in index]
cmd = ['node', str(Path(__file__).with_name('build-index.js'))]
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
main_path = args.output_dir
if proc.poll() is None:
proc = None
try:
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate(json.dumps(json_values))
assert proc.poll() == 0, proc.poll()
if proc.returncode == 0:
stdout = 'INDEX=' + stdout
else:
warn(f'Prebuilding Lunr index with command `{" ".join(cmd)}` failed: '
f'{proc.stderr and proc.stderr.read() or ""!r}. '
f'The search feature will still work, '
except Exception as ex:
stderr = proc and proc.stderr and proc.stderr.read()
warn(f'Prebuilding Lunr index with command `{" ".join(cmd)}` failed with '
f'"{ex.__class__.__name__}: {ex}". Stderr: {stderr or ""!r}. '
f'Note, the search feature will still work, '
f'but may be slower (with the index rebuilt just before use). '
f'To prebuild an index in advance, ensure `node` is executable in the '
f'pdoc environment.', category=RuntimeWarning)
stdout = ('URLS=' + json.dumps(urls, indent=0, separators=(',', ':')) +
';\nINDEX=' + json.dumps(index, indent=0, separators=(',', ':')))
else:
stdout = 'INDEX=' + stdout
main_path = args.output_dir
index_path = Path(main_path).joinpath('index.js')
index_path.write_text(stdout)
print(str(index_path))
Expand Down

0 comments on commit 4220d22

Please sign in to comment.