1212# *******************************************************************************
1313
1414import json
15- import os
1615import subprocess
17- import sys
1816from dataclasses import dataclass
1917from pathlib import Path
2018
2321from sphinx .util import logging
2422from sphinx_needs .needsfile import NeedsList
2523
24+ from src .helper_lib import get_runfiles_dir
25+
2626logger = logging .getLogger (__name__ )
2727
2828
@@ -61,6 +61,7 @@ def parse_external_needs_sources_from_DATA(v: str) -> list[ExternalNeedsSource]:
6161 return []
6262
6363 logger .debug (f"Parsing external needs sources: { v } " )
64+
6465 data = json .loads (v )
6566
6667 res = [res for el in data if (res := _parse_bazel_external_need (el ))]
@@ -138,37 +139,21 @@ def temp(self: NeedsList):
138139
139140
140141def get_external_needs_source (external_needs_source : str ) -> list [ExternalNeedsSource ]:
141- bazel = external_needs_source or os .getenv ("RUNFILES_DIR" )
142-
143- if bazel :
142+ if external_needs_source :
143+ # Path taken for all invocations via `bazel`
144144 external_needs = parse_external_needs_sources_from_DATA (external_needs_source )
145145 else :
146+ # This is the path taken for anything that doesn't
147+ # run via `bazel` e.g. esbonio or other direct executions
146148 external_needs = parse_external_needs_sources_from_bazel_query () # pyright: ignore[reportAny]
147-
148149 return external_needs
149150
150151
151152def add_external_needs_json (e : ExternalNeedsSource , config : Config ):
152- json_file = f"{ e .bazel_module } +/{ e .target } /_build/needs/needs.json"
153- if r := os .getenv ("RUNFILES_DIR" ):
154- logger .debug ("Using runfiles to determine external needs JSON file." )
155- fixed_json_file = Path (r ) / json_file
156- else :
157- logger .debug (
158- "Running outside bazel. "
159- + "Determining git root for external needs JSON file."
160- )
161- git_root = Path .cwd ().resolve ()
162- while not (git_root / ".git" ).exists ():
163- git_root = git_root .parent
164- if git_root == Path ("/" ):
165- sys .exit ("Could not find git root." )
166- logger .debug (f"Git root found: { git_root } " )
167- fixed_json_file = git_root / "bazel-bin" / "ide_support.runfiles" / json_file
168-
169- logger .debug (f"Fixed JSON file path: { json_file } -> { fixed_json_file } " )
170- json_file = fixed_json_file
171-
153+ json_file_raw = f"{ e .bazel_module } +/{ e .target } /_build/needs/needs.json"
154+ r = get_runfiles_dir ()
155+ json_file = r / json_file_raw
156+ logger .debug (f"External needs.json: { json_file } " )
172157 try :
173158 needs_json_data = json .loads (Path (json_file ).read_text (encoding = "utf-8" )) # pyright: ignore[reportAny]
174159 except FileNotFoundError :
@@ -192,11 +177,11 @@ def add_external_needs_json(e: ExternalNeedsSource, config: Config):
192177def add_external_docs_sources (e : ExternalNeedsSource , config : Config ):
193178 # Note that bazel does NOT write the files under e.target!
194179 # {e.bazel_module}+ matches the original git layout!
195- if r := os .getenv ("RUNFILES_DIR" ):
196- docs_source_path = Path (r ) / f"{ e .bazel_module } +"
197- else :
180+ r = get_runfiles_dir ()
181+ if "ide_support.runfiles" in str (r ):
198182 logger .error ("Combo builds are currently only supported with Bazel." )
199183 return
184+ docs_source_path = Path (r ) / f"{ e .bazel_module } +"
200185
201186 if "collections" not in config :
202187 config .collections = {}
0 commit comments