From 3f39f5bad825e1b5bf2aab95cd26f54162d287ec Mon Sep 17 00:00:00 2001 From: zackees Date: Sat, 11 Apr 2026 19:32:53 -0700 Subject: [PATCH 1/4] feat(rust): scaffold Cargo workspace and rust-toolchain Add a minimal Cargo workspace (crates/fastled-cli, crates/fastled-py) with rust-toolchain.toml pinned to 1.94.1, ci/env.py for MSVC environment setup, trampoline scripts (_cargo, _rustc, _rustfmt), and update install/lint/test scripts to bootstrap and exercise the Rust toolchain alongside the existing Python steps. --- .gitignore | 5 + Cargo.lock | 322 ++++++++++++++++++ Cargo.toml | 24 ++ _cargo | 16 + _rustc | 16 + _rustfmt | 16 + ci/env.py | 193 +++++++++++ ci/lint_python/keyboard_interrupt_checker.py | 5 +- crates/fastled-cli/Cargo.toml | 16 + crates/fastled-cli/src/main.rs | 14 + crates/fastled-py/Cargo.toml | 16 + crates/fastled-py/src/lib.rs | 14 + install | 338 +++++++++++++++++-- lint | 20 ++ rust-toolchain.toml | 4 + test | 13 + 16 files changed, 995 insertions(+), 37 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 _cargo create mode 100644 _rustc create mode 100644 _rustfmt create mode 100644 ci/env.py create mode 100644 crates/fastled-cli/Cargo.toml create mode 100644 crates/fastled-cli/src/main.rs create mode 100644 crates/fastled-py/Cargo.toml create mode 100644 crates/fastled-py/src/lib.rs create mode 100644 rust-toolchain.toml diff --git a/.gitignore b/.gitignore index 5537b3c0..ab07b511 100644 --- a/.gitignore +++ b/.gitignore @@ -153,3 +153,8 @@ dist/ logs vscode-plugin .loop + +# Rust build artifacts +/target/ +/.cargo/ +/.rustup/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 00000000..ad26e3a9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,322 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "clap" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1110bd8a634a1ab8cb04345d8d878267d57c3cf1b38d91b71af6686408bbca6a" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "fastled-cli" +version = "2.0.6" +dependencies = [ + "clap", +] + +[[package]] +name = "fastled-py" +version = "2.0.6" +dependencies = [ + "pyo3", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indoc" +version = "2.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" +dependencies = [ + "rustversion", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "libc" +version = "0.2.184" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48f5d2a454e16a5ea0f4ced81bd44e4cfc7bd3a507b61887c99fd3538b28e4af" + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pyo3" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7778bffd85cf38175ac1f545509665d0b9b92a198ca7941f131f85f7a4f9a872" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "once_cell", + "portable-atomic", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94f6cbe86ef3bf18998d9df6e0f3fc1050a8c5efa409bf712e661a4366e010fb" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9f1b4c431c0bb1c8fb0a338709859eed0d030ff6daa34368d3b152a63dfdd8d" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbc2201328f63c4710f68abdf653c89d8dbc2858b88c5d88b0ff38a75288a9da" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fca6726ad0f3da9c9de093d6f116a93c1a38e417ed73bf138472cf4064f72028" +dependencies = [ + "heck", + "proc-macro2", + "pyo3-build-config", + "quote", + "syn", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unindent" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..8131fd90 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,24 @@ +[workspace] +resolver = "2" +members = [ + "crates/fastled-cli", + "crates/fastled-py", +] + +[workspace.package] +version = "2.0.6" +edition = "2021" +rust-version = "1.85" +license = "MIT" +repository = "https://github.com/zackees/fastled-wasm" +homepage = "https://github.com/zackees/fastled-wasm" + +[workspace.dependencies] +pyo3 = { version = "0.23", features = ["abi3-py310"] } +clap = { version = "4", features = ["derive"] } +thiserror = "2" + +[profile.release] +debug = "line-tables-only" +split-debuginfo = "packed" +strip = "none" diff --git a/_cargo b/_cargo new file mode 100644 index 00000000..a2bd8353 --- /dev/null +++ b/_cargo @@ -0,0 +1,16 @@ +#!/bin/bash +# Trampoline: prefers the shared rustup proxy in ~/.cargo/bin when present. +if [ -n "${CARGO_HOME:-}" ]; then + CARGO_BIN="$CARGO_HOME/bin" +elif [ -n "${HOME:-}" ]; then + CARGO_BIN="$HOME/.cargo/bin" +elif [ -n "${USERPROFILE:-}" ]; then + CARGO_BIN="$USERPROFILE/.cargo/bin" +else + CARGO_BIN="" +fi + +if [ -n "$CARGO_BIN" ] && [ -d "$CARGO_BIN" ]; then + export PATH="$CARGO_BIN:$PATH" +fi +exec cargo "$@" diff --git a/_rustc b/_rustc new file mode 100644 index 00000000..cebfea3d --- /dev/null +++ b/_rustc @@ -0,0 +1,16 @@ +#!/bin/bash +# Trampoline: prefers the shared rustup proxy in ~/.cargo/bin when present. +if [ -n "${CARGO_HOME:-}" ]; then + CARGO_BIN="$CARGO_HOME/bin" +elif [ -n "${HOME:-}" ]; then + CARGO_BIN="$HOME/.cargo/bin" +elif [ -n "${USERPROFILE:-}" ]; then + CARGO_BIN="$USERPROFILE/.cargo/bin" +else + CARGO_BIN="" +fi + +if [ -n "$CARGO_BIN" ] && [ -d "$CARGO_BIN" ]; then + export PATH="$CARGO_BIN:$PATH" +fi +exec rustc "$@" diff --git a/_rustfmt b/_rustfmt new file mode 100644 index 00000000..1d9efda3 --- /dev/null +++ b/_rustfmt @@ -0,0 +1,16 @@ +#!/bin/bash +# Trampoline: prefers the shared rustup proxy in ~/.cargo/bin when present. +if [ -n "${CARGO_HOME:-}" ]; then + CARGO_BIN="$CARGO_HOME/bin" +elif [ -n "${HOME:-}" ]; then + CARGO_BIN="$HOME/.cargo/bin" +elif [ -n "${USERPROFILE:-}" ]; then + CARGO_BIN="$USERPROFILE/.cargo/bin" +else + CARGO_BIN="" +fi + +if [ -n "$CARGO_BIN" ] && [ -d "$CARGO_BIN" ]; then + export PATH="$CARGO_BIN:$PATH" +fi +exec rustfmt "$@" diff --git a/ci/env.py b/ci/env.py new file mode 100644 index 00000000..dfa0d105 --- /dev/null +++ b/ci/env.py @@ -0,0 +1,193 @@ +from __future__ import annotations + +import os +import platform +import shutil +import subprocess +import tomllib +from pathlib import Path + + +def cargo_home() -> Path: + if os.environ.get("CARGO_HOME"): + return Path(os.environ["CARGO_HOME"]).expanduser() + return Path.home() / ".cargo" + + +def cargo_bin() -> Path: + return cargo_home() / "bin" + + +def rustup_home() -> Path: + if os.environ.get("RUSTUP_HOME"): + return Path(os.environ["RUSTUP_HOME"]).expanduser() + return Path.home() / ".rustup" + + +def repo_root() -> Path: + return Path(__file__).resolve().parent.parent + + +def toolchain_file() -> Path: + return repo_root() / "rust-toolchain.toml" + + +def load_toolchain_channel() -> str: + with toolchain_file().open("rb") as handle: + data = tomllib.load(handle) + toolchain = data.get("toolchain") + if not isinstance(toolchain, dict): + raise RuntimeError(f"missing [toolchain] in {toolchain_file()}") + channel = toolchain.get("channel") + if not isinstance(channel, str) or not channel: + raise RuntimeError(f"missing toolchain.channel in {toolchain_file()}") + return channel + + +def host_target_triple() -> str: + system = platform.system() + machine = platform.machine().lower() + arch = { + "amd64": "x86_64", + "x86_64": "x86_64", + "arm64": "aarch64", + "aarch64": "aarch64", + }.get(machine) + if arch is None: + raise RuntimeError(f"unsupported architecture: {machine}") + if system == "Windows": + return f"{arch}-pc-windows-msvc" + if system == "Linux": + return f"{arch}-unknown-linux-gnu" + if system == "Darwin": + return f"{arch}-apple-darwin" + raise RuntimeError(f"unsupported platform: {system}") + + +def toolchain_name() -> str: + return f"{load_toolchain_channel()}-{host_target_triple()}" + + +def toolchain_bin() -> Path: + return rustup_home() / "toolchains" / toolchain_name() / "bin" + + +def _find_vswhere() -> Path | None: + candidates = [ + Path(r"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"), + Path(r"C:\Program Files\Microsoft Visual Studio\Installer\vswhere.exe"), + ] + for candidate in candidates: + if candidate.is_file(): + return candidate + return None + + +def _find_vsdevcmd() -> Path | None: + vswhere = _find_vswhere() + if vswhere is None: + return None + result = subprocess.run( + [ + str(vswhere), + "-latest", + "-products", + "*", + "-requires", + "Microsoft.VisualStudio.Component.VC.Tools.x86.x64", + "-property", + "installationPath", + ], + check=False, + capture_output=True, + text=True, + ) + installation_path = result.stdout.strip() + if not installation_path: + return None + candidate = Path(installation_path) / "Common7" / "Tools" / "VsDevCmd.bat" + if candidate.is_file(): + return candidate + return None + + +def _windows_build_env() -> dict[str, str]: + env = os.environ.copy() + toolchain_bin_dir = toolchain_bin() + if toolchain_bin_dir.is_dir(): + env["PATH"] = str(toolchain_bin_dir) + os.pathsep + env.get("PATH", "") + cargo_exe = toolchain_bin_dir / "cargo.exe" + rustc_exe = toolchain_bin_dir / "rustc.exe" + if cargo_exe.is_file(): + env["CARGO"] = str(cargo_exe) + if rustc_exe.is_file(): + env["RUSTC"] = str(rustc_exe) + env["RUSTUP_TOOLCHAIN"] = toolchain_name() + env["CARGO_BUILD_TARGET"] = host_target_triple() + + vsdevcmd = _find_vsdevcmd() + if vsdevcmd is None: + return env + + command = f'"{vsdevcmd}" -arch=x64 -host_arch=x64 >nul && set' + result = subprocess.run( + ["cmd", "/d", "/s", "/c", command], + check=False, + capture_output=True, + text=True, + env=env, + ) + if result.returncode != 0: + return env + for line in result.stdout.splitlines(): + if "=" not in line: + continue + key, value = line.split("=", 1) + env[key] = value + return env + + +def activate() -> None: + bin_dir = cargo_bin() + if not bin_dir.is_dir(): + return + + current_path = os.environ.get("PATH", "") + path_parts = current_path.split(os.pathsep) if current_path else [] + normalized_cargo_bin = os.path.normcase(os.path.normpath(str(bin_dir))) + normalized_parts = { + os.path.normcase(os.path.normpath(part)) for part in path_parts if part + } + if normalized_cargo_bin in normalized_parts: + return + os.environ["PATH"] = ( + str(bin_dir) if not current_path else str(bin_dir) + os.pathsep + current_path + ) + + +def _apply_sccache(env: dict[str, str]) -> dict[str, str]: + """Set RUSTC_WRAPPER=sccache when sccache is available.""" + if env.get("RUSTC_WRAPPER"): + return env + sccache = shutil.which("sccache", path=env.get("PATH")) + if sccache: + env["RUSTC_WRAPPER"] = sccache + return env + + +def clean_env() -> dict[str, str]: + activate() + env = os.environ.copy() + env.pop("VIRTUAL_ENV", None) + env.setdefault("PYTHONUTF8", "1") + if platform.system() == "Windows": + env = env | _windows_build_env() + env.pop("VIRTUAL_ENV", None) + env.setdefault("PYTHONUTF8", "1") + env.setdefault("RUSTUP_TOOLCHAIN", toolchain_name()) + env = _apply_sccache(env) + return env + + +def build_env() -> dict[str, str]: + return clean_env() diff --git a/ci/lint_python/keyboard_interrupt_checker.py b/ci/lint_python/keyboard_interrupt_checker.py index 6894cd78..c56b6913 100644 --- a/ci/lint_python/keyboard_interrupt_checker.py +++ b/ci/lint_python/keyboard_interrupt_checker.py @@ -22,7 +22,6 @@ from dataclasses import dataclass from pathlib import Path - _BROAD_EXCEPT_RE = re.compile( r"^\s*except\s*" r"(?:" @@ -175,7 +174,9 @@ def visit_Try(self, node: ast.Try) -> None: # noqa: N802 self.generic_visit(node) -_INTERRUPT_HANDLER_NAMES = frozenset({"handle_keyboard_interrupt", "notify_main_thread"}) +_INTERRUPT_HANDLER_NAMES = frozenset( + {"handle_keyboard_interrupt", "notify_main_thread"} +) def _find_interrupt_handler_calls(stmts: list[ast.stmt]) -> list[ast.Call]: diff --git a/crates/fastled-cli/Cargo.toml b/crates/fastled-cli/Cargo.toml new file mode 100644 index 00000000..83236a56 --- /dev/null +++ b/crates/fastled-cli/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "fastled-cli" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +description = "FastLED WASM compilation CLI" + +[[bin]] +name = "fastled" +path = "src/main.rs" + +[dependencies] +clap = { workspace = true } diff --git a/crates/fastled-cli/src/main.rs b/crates/fastled-cli/src/main.rs new file mode 100644 index 00000000..da4e37c8 --- /dev/null +++ b/crates/fastled-cli/src/main.rs @@ -0,0 +1,14 @@ +use clap::Parser; + +#[derive(Parser)] +#[command(name = "fastled", version, about = "FastLED WASM compilation CLI")] +struct Cli { + /// Print version and exit + #[arg(long)] + version_info: bool, +} + +fn main() { + let _cli = Cli::parse(); + println!("fastled-cli {}", env!("CARGO_PKG_VERSION")); +} diff --git a/crates/fastled-py/Cargo.toml b/crates/fastled-py/Cargo.toml new file mode 100644 index 00000000..8939db8d --- /dev/null +++ b/crates/fastled-py/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "fastled-py" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +description = "PyO3 bridge for FastLED WASM CLI" + +[lib] +name = "_native" +crate-type = ["cdylib", "lib"] + +[dependencies] +pyo3 = { workspace = true } diff --git a/crates/fastled-py/src/lib.rs b/crates/fastled-py/src/lib.rs new file mode 100644 index 00000000..ad139798 --- /dev/null +++ b/crates/fastled-py/src/lib.rs @@ -0,0 +1,14 @@ +use pyo3::prelude::*; + +/// Return the native module version. +#[pyfunction] +fn version() -> &'static str { + env!("CARGO_PKG_VERSION") +} + +/// FastLED native extension module. +#[pymodule] +fn _native(m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_function(wrap_pyfunction!(version, m)?)?; + Ok(()) +} diff --git a/install b/install index d47759ad..a9e97874 100755 --- a/install +++ b/install @@ -1,35 +1,303 @@ -#!/bin/bash -set -e - -# Check if UV is not found -if ! command -v uv &> /dev/null; then - # If Darwin (macOS), use brew to install UV - if [[ "$OSTYPE" == "darwin"* ]]; then - brew install uv - else - # If it's Windows, use pip to install UV, else use pip3 - if [[ "$OSTYPE" == "msys" ]]; then - pip install uv - else - pip3 install uv - fi - fi -fi - -uv venv --python 3.11 --seed -uv pip install -e . - -uv run pip install -r requirements.testing.txt - - -# If activate exists, delete it -if [[ -f activate ]]; then - rm activate -fi - -# If Windows, then symlink .venv/Scripts/activate to .venv/bin/activate -if [[ "$OSTYPE" == "msys" ]]; then - ln -s .venv/Scripts/activate ./activate -else - ln -s .venv/bin/activate ./activate -fi +#!/usr/bin/env -S uv run --script +# /// script +# requires-python = ">=3.11" +# /// +from __future__ import annotations + +import os +import platform +import shutil +import subprocess +import sys +import tempfile +import time +import urllib.request +from contextlib import contextmanager +from pathlib import Path + +import tomllib + +ROOT = Path(__file__).resolve().parent +TOOLCHAIN_FILE = ROOT / "rust-toolchain.toml" +LOCK_TIMEOUT_SECONDS = 15 * 60 +LOCK_POLL_SECONDS = 0.2 + + +def cargo_home() -> Path: + if os.environ.get("CARGO_HOME"): + return Path(os.environ["CARGO_HOME"]).expanduser() + return Path.home() / ".cargo" + + +def rustup_home() -> Path: + if os.environ.get("RUSTUP_HOME"): + return Path(os.environ["RUSTUP_HOME"]).expanduser() + return Path.home() / ".rustup" + + +def cargo_bin_dir() -> Path: + return cargo_home() / "bin" + + +def rustup_exe_name() -> str: + return "rustup.exe" if os.name == "nt" else "rustup" + + +def homes_are_overridden() -> bool: + return "CARGO_HOME" in os.environ or "RUSTUP_HOME" in os.environ + + +def rustup_executable() -> str | None: + candidate = cargo_bin_dir() / rustup_exe_name() + if candidate.exists(): + return str(candidate) + if homes_are_overridden(): + return None + return shutil.which("rustup") + + +def prepend_cargo_bin_to_path() -> None: + bin_dir = cargo_bin_dir() + current_path = os.environ.get("PATH", "") + path_parts = current_path.split(os.pathsep) if current_path else [] + normalized_bin = os.path.normcase(os.path.normpath(str(bin_dir))) + normalized_parts = { + os.path.normcase(os.path.normpath(part)) for part in path_parts if part + } + if normalized_bin not in normalized_parts: + os.environ["PATH"] = str(bin_dir) + ( + os.pathsep + current_path if current_path else "" + ) + + +def rustup_env() -> dict[str, str]: + env = os.environ.copy() + env["RUSTUP_INIT_SKIP_PATH_CHECK"] = "yes" + env["CARGO_HOME"] = str(cargo_home()) + env["RUSTUP_HOME"] = str(rustup_home()) + return env + + +def host_target_triple() -> str: + system = platform.system() + machine = platform.machine().lower() + + arch = { + "amd64": "x86_64", + "x86_64": "x86_64", + "arm64": "aarch64", + "aarch64": "aarch64", + }.get(machine) + if arch is None: + raise RuntimeError(f"unsupported architecture: {machine}") + + if system == "Windows": + return f"{arch}-pc-windows-msvc" + if system == "Linux": + return f"{arch}-unknown-linux-gnu" + if system == "Darwin": + return f"{arch}-apple-darwin" + raise RuntimeError(f"unsupported platform: {system}") + + +def rustup_init_url() -> str: + suffix = ".exe" if os.name == "nt" else "" + return f"https://static.rust-lang.org/rustup/dist/{host_target_triple()}/rustup-init{suffix}" + + +def load_toolchain_spec() -> dict[str, object]: + with TOOLCHAIN_FILE.open("rb") as handle: + data = tomllib.load(handle) + toolchain = data.get("toolchain") + if not isinstance(toolchain, dict): + raise RuntimeError(f"missing [toolchain] in {TOOLCHAIN_FILE}") + + channel = toolchain.get("channel") + profile = toolchain.get("profile", "minimal") + components = toolchain.get("components", []) + targets = toolchain.get("targets", []) + + if not isinstance(channel, str) or not channel: + raise RuntimeError(f"missing toolchain.channel in {TOOLCHAIN_FILE}") + if not isinstance(profile, str): + raise RuntimeError(f"toolchain.profile must be a string in {TOOLCHAIN_FILE}") + if not isinstance(components, list) or not all( + isinstance(item, str) for item in components + ): + raise RuntimeError( + f"toolchain.components must be a list of strings in {TOOLCHAIN_FILE}" + ) + if not isinstance(targets, list) or not all( + isinstance(item, str) for item in targets + ): + raise RuntimeError( + f"toolchain.targets must be a list of strings in {TOOLCHAIN_FILE}" + ) + + return { + "channel": channel, + "profile": profile, + "components": components, + "targets": targets, + } + + +@contextmanager +def file_lock(name: str): + lock_root = rustup_home() / "tmp" / "fastled-wasm-locks" + lock_root.mkdir(parents=True, exist_ok=True) + lock_path = lock_root / f"{name}.lock" + deadline = time.time() + LOCK_TIMEOUT_SECONDS + while True: + try: + fd = os.open(str(lock_path), os.O_CREAT | os.O_EXCL | os.O_WRONLY) + try: + os.write(fd, f"pid={os.getpid()}\ntime={time.time()}\n".encode("utf-8")) + finally: + os.close(fd) + break + except FileExistsError: + if time.time() >= deadline: + raise TimeoutError(f"timed out waiting for lock {lock_path}") + time.sleep(LOCK_POLL_SECONDS) + try: + yield + finally: + try: + lock_path.unlink() + except FileNotFoundError: + pass + + +def download(url: str, destination: Path) -> None: + destination.parent.mkdir(parents=True, exist_ok=True) + with urllib.request.urlopen(url) as response, destination.open("wb") as handle: + shutil.copyfileobj(response, handle) + + +def bootstrap_rustup() -> None: + if rustup_executable(): + prepend_cargo_bin_to_path() + return + + with file_lock("rustup-bootstrap"): + if rustup_executable(): + prepend_cargo_bin_to_path() + return + + suffix = ".exe" if os.name == "nt" else "" + temp_dir = Path(tempfile.mkdtemp(prefix="fastled-wasm-rustup-init-")) + temp_path = temp_dir / f"rustup-init{suffix}" + download(rustup_init_url(), temp_path) + try: + if os.name != "nt": + temp_path.chmod(0o755) + subprocess.run( + [ + str(temp_path), + "-y", + "--profile", + "minimal", + "--default-toolchain", + "none", + "--default-host", + host_target_triple(), + "--no-modify-path", + ], + check=True, + env=rustup_env(), + ) + finally: + shutil.rmtree(temp_dir, ignore_errors=True) + + prepend_cargo_bin_to_path() + + +def ensure_rust_toolchain() -> int: + bootstrap_rustup() + spec = load_toolchain_spec() + rustup = rustup_executable() + if not rustup: + raise RuntimeError("rustup bootstrap completed but rustup is still unavailable") + + cmd = [ + rustup, + "toolchain", + "install", + str(spec["channel"]), + "--profile", + str(spec["profile"]), + "--no-self-update", + ] + for component in spec["components"]: + cmd.extend(["-c", str(component)]) + for target in spec["targets"]: + cmd.extend(["-t", str(target)]) + + lock_name = f"toolchain-{str(spec['channel']).replace('/', '-').replace(':', '-')}" + with file_lock(lock_name): + return subprocess.run(cmd, check=False, env=rustup_env()).returncode + + +def install_python() -> int: + """Install the Python package and its dependencies.""" + uv = shutil.which("uv") + if uv is None: + if platform.system() == "Darwin": + subprocess.run(["brew", "install", "uv"], check=False) + elif os.name == "nt": + subprocess.run(["pip", "install", "uv"], check=False) + else: + subprocess.run(["pip3", "install", "uv"], check=False) + uv = shutil.which("uv") + + if uv is None: + print("ERROR: uv not found and could not be installed", file=sys.stderr) + return 1 + + if ( + subprocess.run( + [uv, "venv", "--python", "3.11", "--seed"], check=False + ).returncode + != 0 + ): + return 1 + if subprocess.run([uv, "pip", "install", "-e", "."], check=False).returncode != 0: + return 1 + if ( + subprocess.run( + [uv, "run", "pip", "install", "-r", "requirements.testing.txt"], check=False + ).returncode + != 0 + ): + return 1 + + # Manage the activate symlink + activate = ROOT / "activate" + if activate.exists() or activate.is_symlink(): + activate.unlink() + + if os.name == "nt": + target = ROOT / ".venv" / "Scripts" / "activate" + else: + target = ROOT / ".venv" / "bin" / "activate" + + try: + activate.symlink_to(target) + except OSError: + pass # Symlink creation is best-effort + + return 0 + + +def main() -> int: + if install_python() != 0: + return 1 + if ensure_rust_toolchain() != 0: + return 1 + prepend_cargo_bin_to_path() + return subprocess.run(["cargo", "check", "--workspace"], check=False).returncode + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/lint b/lint index d790dddf..490a81b1 100755 --- a/lint +++ b/lint @@ -9,6 +9,26 @@ fi . ./activate +# Ensure the rustup-managed toolchain bin is first in PATH so cargo, rustc, +# clippy and rustfmt resolve to the toolchain declared in rust-toolchain.toml +# rather than any system-installed (e.g. Chocolatey) Rust binaries. +RUSTUP_HOME="${RUSTUP_HOME:-$USERPROFILE/.rustup}" +if [ -z "$RUSTUP_HOME" ] && [ -n "$HOME" ]; then + RUSTUP_HOME="$HOME/.rustup" +fi +CARGO_HOME="${CARGO_HOME:-$USERPROFILE/.cargo}" +if [ -z "$CARGO_HOME" ] && [ -n "$HOME" ]; then + CARGO_HOME="$HOME/.cargo" +fi +if [ -d "$CARGO_HOME/bin" ]; then + export PATH="$(cygpath -u "$CARGO_HOME/bin"):$PATH" +fi + +echo Running cargo fmt check +cargo fmt --all --check +echo Running cargo clippy +cargo clippy --workspace --all-targets -- -D warnings + echo Running ruff src compiler ruff check --fix src compiler echo Running ruff tests compiler diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000..b1dd491c --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "1.94.1" +profile = "minimal" +components = ["clippy", "rustfmt"] diff --git a/test b/test index d28265a2..3939ba9f 100755 --- a/test +++ b/test @@ -9,4 +9,17 @@ fi . ./activate +# Ensure the rustup-managed toolchain bin is first in PATH so cargo uses the +# toolchain declared in rust-toolchain.toml rather than any system-installed +# (e.g. Chocolatey) Rust binaries. +CARGO_HOME="${CARGO_HOME:-$USERPROFILE/.cargo}" +if [ -z "$CARGO_HOME" ] && [ -n "$HOME" ]; then + CARGO_HOME="$HOME/.cargo" +fi +if [ -d "$CARGO_HOME/bin" ]; then + export PATH="$(cygpath -u "$CARGO_HOME/bin"):$PATH" +fi + +cargo test --workspace + pytest -x -v -s --durations=0 tests/unit "$@" From 51dbc46718bfda5f24259a67d3f7ee62570e65c4 Mon Sep 17 00:00:00 2001 From: zackees Date: Sat, 11 Apr 2026 19:49:48 -0700 Subject: [PATCH 2/4] fix(ci): restore bash install script and fix MSVC toolchain resolution - Restore original bash install script format (CI was broken by uv run --script rewrite) - Add Rust toolchain bootstrap to install script (rustup + pinned channel) - Fix _cargo trampoline to prepend toolchain bin to PATH and set CARGO_BUILD_TARGET to MSVC on Windows (prevents Chocolatey GNU contamination) - Use uv run instead of venv activation in lint/test scripts - Remove cygpath dependency from scripts (not available on Linux/macOS) --- _cargo | 39 ++++-- install | 360 +++++++++----------------------------------------------- lint | 42 ++----- test | 25 ++-- 4 files changed, 105 insertions(+), 361 deletions(-) diff --git a/_cargo b/_cargo index a2bd8353..d89acd99 100644 --- a/_cargo +++ b/_cargo @@ -1,16 +1,33 @@ #!/bin/bash -# Trampoline: prefers the shared rustup proxy in ~/.cargo/bin when present. -if [ -n "${CARGO_HOME:-}" ]; then - CARGO_BIN="$CARGO_HOME/bin" -elif [ -n "${HOME:-}" ]; then - CARGO_BIN="$HOME/.cargo/bin" -elif [ -n "${USERPROFILE:-}" ]; then - CARGO_BIN="$USERPROFILE/.cargo/bin" -else - CARGO_BIN="" +# Trampoline: ensure the correct Rust toolchain is used. +# On Windows, Chocolatey may install a GNU-based Rust that conflicts with +# the MSVC toolchain required by this project. This script: +# 1. Puts the rustup-managed toolchain bin FIRST in PATH +# 2. Sets CARGO_BUILD_TARGET to force MSVC on Windows +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" + +# Find rustup +RUSTUP="$(command -v rustup 2>/dev/null || true)" +if [ -z "$RUSTUP" ]; then + exec cargo "$@" fi -if [ -n "$CARGO_BIN" ] && [ -d "$CARGO_BIN" ]; then - export PATH="$CARGO_BIN:$PATH" +# Get the toolchain's bin directory and prepend it to PATH +TOOLCHAIN_BIN="$($RUSTUP run "$(grep channel "$SCRIPT_DIR/rust-toolchain.toml" 2>/dev/null | head -1 | sed 's/.*"\(.*\)"/\1/')" rustc --print sysroot 2>/dev/null)/bin" +if [ -d "$TOOLCHAIN_BIN" ]; then + export PATH="$TOOLCHAIN_BIN:$PATH" fi + +# On Windows, force MSVC target to prevent GNU toolchain contamination +case "$(uname -s)" in + MINGW*|MSYS*|CYGWIN*) + ARCH="$(uname -m)" + if [ "$ARCH" = "x86_64" ]; then + export CARGO_BUILD_TARGET="x86_64-pc-windows-msvc" + else + export CARGO_BUILD_TARGET="aarch64-pc-windows-msvc" + fi + ;; +esac + exec cargo "$@" diff --git a/install b/install index a9e97874..b9e4c9f5 100755 --- a/install +++ b/install @@ -1,303 +1,57 @@ -#!/usr/bin/env -S uv run --script -# /// script -# requires-python = ">=3.11" -# /// -from __future__ import annotations - -import os -import platform -import shutil -import subprocess -import sys -import tempfile -import time -import urllib.request -from contextlib import contextmanager -from pathlib import Path - -import tomllib - -ROOT = Path(__file__).resolve().parent -TOOLCHAIN_FILE = ROOT / "rust-toolchain.toml" -LOCK_TIMEOUT_SECONDS = 15 * 60 -LOCK_POLL_SECONDS = 0.2 - - -def cargo_home() -> Path: - if os.environ.get("CARGO_HOME"): - return Path(os.environ["CARGO_HOME"]).expanduser() - return Path.home() / ".cargo" - - -def rustup_home() -> Path: - if os.environ.get("RUSTUP_HOME"): - return Path(os.environ["RUSTUP_HOME"]).expanduser() - return Path.home() / ".rustup" - - -def cargo_bin_dir() -> Path: - return cargo_home() / "bin" - - -def rustup_exe_name() -> str: - return "rustup.exe" if os.name == "nt" else "rustup" - - -def homes_are_overridden() -> bool: - return "CARGO_HOME" in os.environ or "RUSTUP_HOME" in os.environ - - -def rustup_executable() -> str | None: - candidate = cargo_bin_dir() / rustup_exe_name() - if candidate.exists(): - return str(candidate) - if homes_are_overridden(): - return None - return shutil.which("rustup") - - -def prepend_cargo_bin_to_path() -> None: - bin_dir = cargo_bin_dir() - current_path = os.environ.get("PATH", "") - path_parts = current_path.split(os.pathsep) if current_path else [] - normalized_bin = os.path.normcase(os.path.normpath(str(bin_dir))) - normalized_parts = { - os.path.normcase(os.path.normpath(part)) for part in path_parts if part - } - if normalized_bin not in normalized_parts: - os.environ["PATH"] = str(bin_dir) + ( - os.pathsep + current_path if current_path else "" - ) - - -def rustup_env() -> dict[str, str]: - env = os.environ.copy() - env["RUSTUP_INIT_SKIP_PATH_CHECK"] = "yes" - env["CARGO_HOME"] = str(cargo_home()) - env["RUSTUP_HOME"] = str(rustup_home()) - return env - - -def host_target_triple() -> str: - system = platform.system() - machine = platform.machine().lower() - - arch = { - "amd64": "x86_64", - "x86_64": "x86_64", - "arm64": "aarch64", - "aarch64": "aarch64", - }.get(machine) - if arch is None: - raise RuntimeError(f"unsupported architecture: {machine}") - - if system == "Windows": - return f"{arch}-pc-windows-msvc" - if system == "Linux": - return f"{arch}-unknown-linux-gnu" - if system == "Darwin": - return f"{arch}-apple-darwin" - raise RuntimeError(f"unsupported platform: {system}") - - -def rustup_init_url() -> str: - suffix = ".exe" if os.name == "nt" else "" - return f"https://static.rust-lang.org/rustup/dist/{host_target_triple()}/rustup-init{suffix}" - - -def load_toolchain_spec() -> dict[str, object]: - with TOOLCHAIN_FILE.open("rb") as handle: - data = tomllib.load(handle) - toolchain = data.get("toolchain") - if not isinstance(toolchain, dict): - raise RuntimeError(f"missing [toolchain] in {TOOLCHAIN_FILE}") - - channel = toolchain.get("channel") - profile = toolchain.get("profile", "minimal") - components = toolchain.get("components", []) - targets = toolchain.get("targets", []) - - if not isinstance(channel, str) or not channel: - raise RuntimeError(f"missing toolchain.channel in {TOOLCHAIN_FILE}") - if not isinstance(profile, str): - raise RuntimeError(f"toolchain.profile must be a string in {TOOLCHAIN_FILE}") - if not isinstance(components, list) or not all( - isinstance(item, str) for item in components - ): - raise RuntimeError( - f"toolchain.components must be a list of strings in {TOOLCHAIN_FILE}" - ) - if not isinstance(targets, list) or not all( - isinstance(item, str) for item in targets - ): - raise RuntimeError( - f"toolchain.targets must be a list of strings in {TOOLCHAIN_FILE}" - ) - - return { - "channel": channel, - "profile": profile, - "components": components, - "targets": targets, - } - - -@contextmanager -def file_lock(name: str): - lock_root = rustup_home() / "tmp" / "fastled-wasm-locks" - lock_root.mkdir(parents=True, exist_ok=True) - lock_path = lock_root / f"{name}.lock" - deadline = time.time() + LOCK_TIMEOUT_SECONDS - while True: - try: - fd = os.open(str(lock_path), os.O_CREAT | os.O_EXCL | os.O_WRONLY) - try: - os.write(fd, f"pid={os.getpid()}\ntime={time.time()}\n".encode("utf-8")) - finally: - os.close(fd) - break - except FileExistsError: - if time.time() >= deadline: - raise TimeoutError(f"timed out waiting for lock {lock_path}") - time.sleep(LOCK_POLL_SECONDS) - try: - yield - finally: - try: - lock_path.unlink() - except FileNotFoundError: - pass - - -def download(url: str, destination: Path) -> None: - destination.parent.mkdir(parents=True, exist_ok=True) - with urllib.request.urlopen(url) as response, destination.open("wb") as handle: - shutil.copyfileobj(response, handle) - - -def bootstrap_rustup() -> None: - if rustup_executable(): - prepend_cargo_bin_to_path() - return - - with file_lock("rustup-bootstrap"): - if rustup_executable(): - prepend_cargo_bin_to_path() - return - - suffix = ".exe" if os.name == "nt" else "" - temp_dir = Path(tempfile.mkdtemp(prefix="fastled-wasm-rustup-init-")) - temp_path = temp_dir / f"rustup-init{suffix}" - download(rustup_init_url(), temp_path) - try: - if os.name != "nt": - temp_path.chmod(0o755) - subprocess.run( - [ - str(temp_path), - "-y", - "--profile", - "minimal", - "--default-toolchain", - "none", - "--default-host", - host_target_triple(), - "--no-modify-path", - ], - check=True, - env=rustup_env(), - ) - finally: - shutil.rmtree(temp_dir, ignore_errors=True) - - prepend_cargo_bin_to_path() - - -def ensure_rust_toolchain() -> int: - bootstrap_rustup() - spec = load_toolchain_spec() - rustup = rustup_executable() - if not rustup: - raise RuntimeError("rustup bootstrap completed but rustup is still unavailable") - - cmd = [ - rustup, - "toolchain", - "install", - str(spec["channel"]), - "--profile", - str(spec["profile"]), - "--no-self-update", - ] - for component in spec["components"]: - cmd.extend(["-c", str(component)]) - for target in spec["targets"]: - cmd.extend(["-t", str(target)]) - - lock_name = f"toolchain-{str(spec['channel']).replace('/', '-').replace(':', '-')}" - with file_lock(lock_name): - return subprocess.run(cmd, check=False, env=rustup_env()).returncode - - -def install_python() -> int: - """Install the Python package and its dependencies.""" - uv = shutil.which("uv") - if uv is None: - if platform.system() == "Darwin": - subprocess.run(["brew", "install", "uv"], check=False) - elif os.name == "nt": - subprocess.run(["pip", "install", "uv"], check=False) - else: - subprocess.run(["pip3", "install", "uv"], check=False) - uv = shutil.which("uv") - - if uv is None: - print("ERROR: uv not found and could not be installed", file=sys.stderr) - return 1 - - if ( - subprocess.run( - [uv, "venv", "--python", "3.11", "--seed"], check=False - ).returncode - != 0 - ): - return 1 - if subprocess.run([uv, "pip", "install", "-e", "."], check=False).returncode != 0: - return 1 - if ( - subprocess.run( - [uv, "run", "pip", "install", "-r", "requirements.testing.txt"], check=False - ).returncode - != 0 - ): - return 1 - - # Manage the activate symlink - activate = ROOT / "activate" - if activate.exists() or activate.is_symlink(): - activate.unlink() - - if os.name == "nt": - target = ROOT / ".venv" / "Scripts" / "activate" - else: - target = ROOT / ".venv" / "bin" / "activate" - - try: - activate.symlink_to(target) - except OSError: - pass # Symlink creation is best-effort - - return 0 - - -def main() -> int: - if install_python() != 0: - return 1 - if ensure_rust_toolchain() != 0: - return 1 - prepend_cargo_bin_to_path() - return subprocess.run(["cargo", "check", "--workspace"], check=False).returncode - - -if __name__ == "__main__": - sys.exit(main()) +#!/bin/bash +set -e + +# Check if UV is not found +if ! command -v uv &> /dev/null; then + # If Darwin (macOS), use brew to install UV + if [[ "$OSTYPE" == "darwin"* ]]; then + brew install uv + else + # If it's Windows, use pip to install UV, else use pip3 + if [[ "$OSTYPE" == "msys" ]]; then + pip install uv + else + pip3 install uv + fi + fi +fi + +uv venv --python 3.11 --seed +uv pip install -e . + +uv run pip install -r requirements.testing.txt + + +# If activate exists, delete it +if [[ -f activate ]]; then + rm activate +fi + +# If Windows, then symlink .venv/Scripts/activate to .venv/bin/activate +if [[ "$OSTYPE" == "msys" ]]; then + ln -s .venv/Scripts/activate ./activate +else + ln -s .venv/bin/activate ./activate +fi + +# ── Rust toolchain bootstrap ──────────────────────────────────────────────── +# Install rustup if not present, then install the pinned toolchain. + +CARGO_BIN="${CARGO_HOME:-$HOME/.cargo}/bin" + +# Prefer rustup-managed cargo over system packages (e.g. Chocolatey GNU Rust) +export PATH="$CARGO_BIN:$PATH" + +if ! command -v rustup &> /dev/null; then + echo "Installing rustup..." + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --no-modify-path + export PATH="$CARGO_BIN:$PATH" +fi + +# Parse channel from rust-toolchain.toml +if [[ -f rust-toolchain.toml ]]; then + CHANNEL=$(grep 'channel' rust-toolchain.toml | head -1 | sed 's/.*= *"\(.*\)"/\1/') + echo "Installing Rust toolchain: $CHANNEL" + rustup toolchain install "$CHANNEL" --profile minimal -c clippy -c rustfmt --no-self-update + cargo check --workspace +fi diff --git a/lint b/lint index 490a81b1..85b0658e 100755 --- a/lint +++ b/lint @@ -1,42 +1,26 @@ #!/bin/bash set -e -# if venv doesn't exist, invoke ./install -if [ ! -d ".venv" ]; then - echo "No venv present, so installing..." - ./install -fi - -. ./activate +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" -# Ensure the rustup-managed toolchain bin is first in PATH so cargo, rustc, -# clippy and rustfmt resolve to the toolchain declared in rust-toolchain.toml -# rather than any system-installed (e.g. Chocolatey) Rust binaries. -RUSTUP_HOME="${RUSTUP_HOME:-$USERPROFILE/.rustup}" -if [ -z "$RUSTUP_HOME" ] && [ -n "$HOME" ]; then - RUSTUP_HOME="$HOME/.rustup" -fi -CARGO_HOME="${CARGO_HOME:-$USERPROFILE/.cargo}" -if [ -z "$CARGO_HOME" ] && [ -n "$HOME" ]; then - CARGO_HOME="$HOME/.cargo" +# Rust linting (skip if cargo not available) +if command -v cargo &> /dev/null; then + echo Running cargo fmt check + "$SCRIPT_DIR/_cargo" fmt --all --check + echo Running cargo clippy + "$SCRIPT_DIR/_cargo" clippy --workspace --all-targets -- -D warnings +else + echo "WARNING: cargo not found, skipping Rust linting" fi -if [ -d "$CARGO_HOME/bin" ]; then - export PATH="$(cygpath -u "$CARGO_HOME/bin"):$PATH" -fi - -echo Running cargo fmt check -cargo fmt --all --check -echo Running cargo clippy -cargo clippy --workspace --all-targets -- -D warnings echo Running ruff src compiler -ruff check --fix src compiler +uv run ruff check --fix src compiler echo Running ruff tests compiler -ruff check --fix tests compiler +uv run ruff check --fix tests compiler echo Running black src tests compiler -black src tests compiler +uv run black src tests compiler echo Running isort src tests compiler -isort --profile black src tests compiler +uv run isort --profile black src tests compiler echo Running pyright src tests compiler uv run pyright src compiler tests echo Running keyboard interrupt checker src tests compiler diff --git a/test b/test index 3939ba9f..fdf0e786 100755 --- a/test +++ b/test @@ -2,24 +2,13 @@ set -euo pipefail -if [ ! -d ".venv" ]; then - echo "No venv present, so installing..." - ./install -fi - -. ./activate +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" -# Ensure the rustup-managed toolchain bin is first in PATH so cargo uses the -# toolchain declared in rust-toolchain.toml rather than any system-installed -# (e.g. Chocolatey) Rust binaries. -CARGO_HOME="${CARGO_HOME:-$USERPROFILE/.cargo}" -if [ -z "$CARGO_HOME" ] && [ -n "$HOME" ]; then - CARGO_HOME="$HOME/.cargo" +# Rust tests (skip if cargo not available) +if command -v cargo &> /dev/null; then + "$SCRIPT_DIR/_cargo" test --workspace +else + echo "WARNING: cargo not found, skipping Rust tests" fi -if [ -d "$CARGO_HOME/bin" ]; then - export PATH="$(cygpath -u "$CARGO_HOME/bin"):$PATH" -fi - -cargo test --workspace -pytest -x -v -s --durations=0 tests/unit "$@" +uv run pytest -x -v -s --durations=0 tests/unit "$@" From 7c2f9224d71239bc5c33ccc15a00d601cd993541 Mon Sep 17 00:00:00 2001 From: zackees Date: Sat, 11 Apr 2026 19:52:27 -0700 Subject: [PATCH 3/4] fix(ci): use uv pip instead of uv run pip to avoid pip distro uname crash The venv-bundled pip crashes on Ubuntu 24.04 CI runners when calling uname -rs during user-agent detection. Using uv pip directly avoids invoking the buggy pip entirely. --- install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install b/install index b9e4c9f5..3564c48e 100755 --- a/install +++ b/install @@ -19,7 +19,7 @@ fi uv venv --python 3.11 --seed uv pip install -e . -uv run pip install -r requirements.testing.txt +uv pip install -r requirements.testing.txt # If activate exists, delete it From e4acccebccd054102a1d7570d7aa6116e2dee3c3 Mon Sep 17 00:00:00 2001 From: zackees Date: Sat, 11 Apr 2026 19:54:36 -0700 Subject: [PATCH 4/4] fix(ci): make trampoline scripts executable --- _cargo | 0 _rustc | 0 _rustfmt | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 _cargo mode change 100644 => 100755 _rustc mode change 100644 => 100755 _rustfmt diff --git a/_cargo b/_cargo old mode 100644 new mode 100755 diff --git a/_rustc b/_rustc old mode 100644 new mode 100755 diff --git a/_rustfmt b/_rustfmt old mode 100644 new mode 100755