diff --git a/src/extensions/score_source_code_linker/needlinks.py b/src/extensions/score_source_code_linker/needlinks.py index 2998240fc..71304cca9 100644 --- a/src/extensions/score_source_code_linker/needlinks.py +++ b/src/extensions/score_source_code_linker/needlinks.py @@ -13,7 +13,6 @@ # req-Id: tool_req__docs_dd_link_source_code_link import json -import os from dataclasses import asdict, dataclass from pathlib import Path from typing import Any, TypedDict, TypeGuard @@ -176,13 +175,11 @@ def load_source_code_links_with_metadata_json(file: Path) -> list[NeedLink]: [ meta_dict, needlink1, needlink2, ... ] Returns: [NeedLink, NeedLink, ...] - - This normally should be the one called 'locally' => :docs target """ - if not file.is_absolute(): - ws_root = os.environ.get("BUILD_WORKSPACE_DIRECTORY") - if ws_root: - file = Path(ws_root) / file + + # Bazel passes an absolute path (``$(location :sourcelinks_json)`` under + # ``bazel run``) or an execroot-relative path (sandbox, where CWD *is* the + # execroot); both are usable as ``Path(file)`` directly. data: list[object] = json.loads( file.read_text(encoding="utf-8"), @@ -219,12 +216,6 @@ def load_source_code_links_json(file: Path) -> list[NeedLink]: This is used when mounted external documentation contributes source links. """ - if not file.is_absolute(): - # use env variable set by Bazel - ws_root = os.environ.get("BUILD_WORKSPACE_DIRECTORY") - if ws_root: - file = Path(ws_root) / file - links: list[NeedLink] = json.loads( file.read_text(encoding="utf-8"), object_hook=needlink_decoder, diff --git a/src/extensions/score_source_code_linker/tests/test_codelink.py b/src/extensions/score_source_code_linker/tests/test_codelink.py index 5f2e9fed2..56785e042 100644 --- a/src/extensions/score_source_code_linker/tests/test_codelink.py +++ b/src/extensions/score_source_code_linker/tests/test_codelink.py @@ -759,10 +759,8 @@ def test_load_with_metadata_invalid_items_after_metadata(tmp_path: Path): # ────────────────[ File Path Resolution Tests ]──────────────── -def test_load_resolves_relative_path_with_env_var( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -): - """Test if relative path is resolved using BUILD_WORKSPACE_DIRECTORY""" +def test_load_resolves_absolute_path(tmp_path: Path): + """Absolute paths (as emitted by Bazel via ``$(location)``) load directly.""" workspace = tmp_path / "workspace" workspace.mkdir() @@ -776,22 +774,18 @@ def test_load_resolves_relative_path_with_env_var( ) ] - # Store in workspace + # Store in workspace and load via its absolute path. cache_file = workspace / "cache.json" store_source_code_links_json(cache_file, needlinks) - # Set env var and load with relative path - monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(workspace)) - loaded = load_source_code_links_json(Path("cache.json")) + loaded = load_source_code_links_json(cache_file) assert len(loaded) == 1 assert loaded[0].need == "REQ_1" -def test_load_with_metadata_resolves_relative_path( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -): - """Edge case: load_with_metadata resolves relative paths using env var""" +def test_load_with_metadata_absolute_path(tmp_path: Path): + """Absolute path: load_with_metadata reads the file directly.""" workspace = tmp_path / "workspace" workspace.mkdir() @@ -813,8 +807,7 @@ def test_load_with_metadata_resolves_relative_path( cache_file = workspace / "metadata_cache.json" store_source_code_links_with_metadata_json(cache_file, metadata, needlinks) - monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(workspace)) - loaded = load_source_code_links_with_metadata_json(Path("metadata_cache.json")) + loaded = load_source_code_links_with_metadata_json(cache_file) assert len(loaded) == 1 assert loaded[0].repo_name == "mod"