From 0f7ed19525c422a76ecb9f0419a932198ea4c20e Mon Sep 17 00:00:00 2001 From: robby Date: Fri, 23 Oct 2026 11:32:12 -0400 Subject: [PATCH] d2-bin: find the binary from the package's installed files find_d2_bin looked only in the running Python's script folders, so it missed the binary in overlay environments such as uv run --with. It now reads the d2-bin distribution's file list, which works for any install layout. The wheel version becomes 0.9.0.post1; the binary is still D2 v0.9.0. Co-Authored-By: Claude Opus 5.5 --- packages/d2-bin/build_wheels.py | 9 +++++---- packages/d2-bin/src/d2_bin/__init__.py | 27 +++++++------------------- tests/test_d2/test_d2_bin.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 tests/test_d2/test_d2_bin.py diff --git a/packages/d2-bin/build_wheels.py b/packages/d2-bin/build_wheels.py index 4b319f7..1d175e5 100644 --- a/packages/d2-bin/build_wheels.py +++ b/packages/d2-bin/build_wheels.py @@ -14,6 +14,7 @@ from typing import Literal, NamedTuple D2_VERSION = "0.9.0" +WHEEL_VERSION = "0.9.0.post1" D2_TAG = f"v{D2_VERSION}" RELEASE_URL = f"https://github.com/terrastruct/d2/releases/download/{D2_TAG}" @@ -36,9 +37,9 @@ } PACKAGE_DIR = Path(__file__).resolve().parent -DIST_INFO = f"d2_bin-{D2_VERSION}.dist-info" +DIST_INFO = f"d2_bin-{WHEEL_VERSION}.dist-info" ENTRY_POINTS = b"[console_scripts]\nd2-bin = d2_bin.__main__:main\n" -DATA_DIR = f"d2_bin-{D2_VERSION}.data" +DATA_DIR = f"d2_bin-{WHEEL_VERSION}.data" LICENSE_FILES = ("LICENSE.txt", "THIRD_PARTY_NOTICES.txt") SUMMARY = ( "The unmodified official D2 diagram scripting language binary," @@ -93,7 +94,7 @@ def metadata() -> bytes: headers = [ "Metadata-Version: 2.4", "Name: d2-bin", - f"Version: {D2_VERSION}", + f"Version: {WHEEL_VERSION}", f"Summary: {SUMMARY}", "Description-Content-Type: text/markdown", "License-Expression: MPL-2.0", @@ -159,7 +160,7 @@ def build_wheel(platform: Platform, checksums: dict[str, str], out_dir: Path) -> files.append(WheelFile(f"{DIST_INFO}/RECORD", record(files), 0o644)) out_dir.mkdir(parents=True, exist_ok=True) - wheel_path = out_dir / f"d2_bin-{D2_VERSION}-py3-none-{tag}.whl" + wheel_path = out_dir / f"d2_bin-{WHEEL_VERSION}-py3-none-{tag}.whl" with zipfile.ZipFile(wheel_path, "w", zipfile.ZIP_DEFLATED) as wheel: for file in files: info = zipfile.ZipInfo(file.path, date_time=(2020, 1, 1, 0, 0, 0)) diff --git a/packages/d2-bin/src/d2_bin/__init__.py b/packages/d2-bin/src/d2_bin/__init__.py index 1ae4bde..2d0cc4f 100644 --- a/packages/d2-bin/src/d2_bin/__init__.py +++ b/packages/d2-bin/src/d2_bin/__init__.py @@ -1,26 +1,13 @@ import sys -import sysconfig +from importlib.metadata import distribution from pathlib import Path -def _user_scheme() -> str: - if sys.platform == "win32": - return "nt_user" - framework = sysconfig.get_config_var("PYTHONFRAMEWORK") - if sys.platform == "darwin" and isinstance(framework, str) and framework != "": - return "osx_framework_user" - return "posix_user" - - def find_d2_bin() -> str: exe = "d2.exe" if sys.platform == "win32" else "d2" - scripts_dirs = [ - sysconfig.get_path("scripts"), - sysconfig.get_path("scripts", scheme=_user_scheme()), - str(Path(sys.prefix) / ("Scripts" if sys.platform == "win32" else "bin")), - ] - for directory in scripts_dirs: - path = Path(directory) / exe - if path.is_file(): - return str(path) - raise FileNotFoundError(f"{exe} not found in: {', '.join(scripts_dirs)}") + for file in distribution("d2-bin").files or (): + if file.name == exe and file.parent.name in {"bin", "Scripts"}: + path = Path(str(file.locate())).resolve() + if path.is_file(): + return str(path) + raise FileNotFoundError(f"{exe} is missing from the d2-bin installation") diff --git a/tests/test_d2/test_d2_bin.py b/tests/test_d2/test_d2_bin.py new file mode 100644 index 0000000..51d16e3 --- /dev/null +++ b/tests/test_d2/test_d2_bin.py @@ -0,0 +1,12 @@ +from pathlib import Path + +import pytest + +d2_bin = pytest.importorskip("d2_bin") + + +def test_find_d2_bin__returns_the_installed_binary() -> None: + path = Path(d2_bin.find_d2_bin()) + + assert path.is_file() + assert path.name in {"d2", "d2.exe"}