diff --git a/MODULE.bazel b/MODULE.bazel index 3d96c6f8b7..0a2521d75a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -326,7 +326,6 @@ http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "ht http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") new_local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "new_local_repository") -include("//bazel/modules:powershell.MODULE.bazel") include("//bazel/modules:wine.MODULE.bazel") new_local_repository( diff --git a/bazel/modules/BUILD.bazel b/bazel/modules/BUILD.bazel index a3da848ef3..305b32d365 100644 --- a/bazel/modules/BUILD.bazel +++ b/bazel/modules/BUILD.bazel @@ -1,4 +1,3 @@ exports_files([ - "powershell.MODULE.bazel", "wine.MODULE.bazel", ]) diff --git a/bazel/modules/powershell.MODULE.bazel b/bazel/modules/powershell.MODULE.bazel deleted file mode 100644 index 995a24951c..0000000000 --- a/bazel/modules/powershell.MODULE.bazel +++ /dev/null @@ -1,14 +0,0 @@ -http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -# Pin the self-contained Windows distribution so Wine tests need neither a -# system PowerShell installation nor a separate .NET runtime. This intentionally -# stays on 7.2.24 for the test fixture: 7.4.16 and 7.6.2 currently fail during -# CLR startup under the pinned Wine 11 runtime, while 7.2.24 runs successfully. -http_archive( - name = "powershell_windows_x86_64", - build_file = "//third_party/powershell:BUILD.bazel", - sha256 = "a1ccb6d8ad52f917470a136c3752af4465f261bcbe570cf44f52aa69ae6e867e", - urls = [ - "https://github.com/PowerShell/PowerShell/releases/download/v7.2.24/PowerShell-7.2.24-win-x64.zip", - ], -) diff --git a/bazel/modules/wine.MODULE.bazel b/bazel/modules/wine.MODULE.bazel index d50e8a3607..9f60b2ac40 100644 --- a/bazel/modules/wine.MODULE.bazel +++ b/bazel/modules/wine.MODULE.bazel @@ -11,3 +11,16 @@ http_archive( "https://github.com/Kron4ek/Wine-Builds/releases/download/11.0/wine-11.0-amd64-wow64.tar.xz", ], ) + +# Pin the self-contained Windows distribution so Wine tests need neither a +# system PowerShell installation nor a separate .NET runtime. This intentionally +# stays on 7.2.24 for the test fixture: 7.4.16 and 7.6.2 currently fail during +# CLR startup under the pinned Wine 11 runtime, while 7.2.24 runs successfully. +http_archive( + name = "powershell_windows_x86_64", + build_file = "//third_party/powershell:BUILD.bazel", + sha256 = "a1ccb6d8ad52f917470a136c3752af4465f261bcbe570cf44f52aa69ae6e867e", + urls = [ + "https://github.com/PowerShell/PowerShell/releases/download/v7.2.24/PowerShell-7.2.24-win-x64.zip", + ], +) diff --git a/bazel/rules/testing/wine.bzl b/bazel/rules/testing/wine.bzl index fd87ba353a..9f0fbade1f 100644 --- a/bazel/rules/testing/wine.bzl +++ b/bazel/rules/testing/wine.bzl @@ -5,22 +5,18 @@ load("//:defs.bzl", "WINDOWS_GNULLVM_RUSTC_LINK_FLAGS") load(":foreign_platform_binary.bzl", "foreign_platform_binary") _WINE_RUNTIME_BINARIES = { + "pwsh": "@powershell_windows_x86_64//:pwsh", + "pwsh-runtime-marker": "@powershell_windows_x86_64//:runtime_marker", "wine": "@wine_linux_x86_64//:wine", "wine-runtime-marker": "@wine_linux_x86_64//:runtime_marker", "wineserver": "@wine_linux_x86_64//:wineserver", } -_POWERSHELL_RUNTIME_BINARIES = { - "pwsh": "@powershell_windows_x86_64//:pwsh", - "pwsh-runtime-marker": "@powershell_windows_x86_64//:runtime_marker", -} - def wine_rust_test( name, windows_binaries, host_binaries = {}, data = [], - include_powershell = False, target_compatible_with = [], **kwargs): """Defines an x86-64 Linux Rust test with a pinned Wine runtime. @@ -34,9 +30,9 @@ def wine_rust_test( * `CARGO_BIN_EXE_wine` and `CARGO_BIN_EXE_wineserver` identify Wine tools. * `CARGO_BIN_EXE_wine-runtime-marker` identifies a file whose parent is the Wine DLL directory to use as `WINEDLLPATH`. - * When `include_powershell` is true, `CARGO_BIN_EXE_pwsh` identifies the - pinned PowerShell executable and `CARGO_BIN_EXE_pwsh-runtime-marker` - identifies a file whose parent is the complete PowerShell runtime. + * `CARGO_BIN_EXE_pwsh` identifies the pinned PowerShell executable and + `CARGO_BIN_EXE_pwsh-runtime-marker` identifies a file whose parent is the + complete PowerShell runtime. These are Bazel runfile locations. Resolve binaries with `codex_utils_cargo_bin::cargo_bin`; `:wine_test_support` resolves the fixed @@ -47,15 +43,14 @@ def wine_rust_test( windows_binaries: Map from `CARGO_BIN_EXE_*` suffixes to Windows targets. host_binaries: Map from `CARGO_BIN_EXE_*` suffixes to Linux host targets. data: Additional runtime data for the Linux test. - include_powershell: Whether to include the pinned PowerShell runtime for Windows. target_compatible_with: Additional compatibility constraints. **kwargs: Remaining attributes forwarded to `rust_test`. """ binaries = dict(_WINE_RUNTIME_BINARIES) - runtime_data = ["@wine_linux_x86_64//:runtime"] - if include_powershell: - binaries.update(_POWERSHELL_RUNTIME_BINARIES) - runtime_data.append("@powershell_windows_x86_64//:runtime") + runtime_data = [ + "@powershell_windows_x86_64//:runtime", + "@wine_linux_x86_64//:runtime", + ] for binary_name in sorted(host_binaries.keys()): if binary_name in binaries: diff --git a/bazel/rules/testing/wine/src/lib.rs b/bazel/rules/testing/wine/src/lib.rs index f4fb4bd2a7..efe7608b97 100644 --- a/bazel/rules/testing/wine/src/lib.rs +++ b/bazel/rules/testing/wine/src/lib.rs @@ -47,7 +47,7 @@ struct WineProcesses { struct WineRuntimePaths { dll_path: PathBuf, - powershell_runtime: Option, + powershell_runtime: PathBuf, wine: PathBuf, wineserver: PathBuf, } @@ -80,9 +80,7 @@ impl WineTestCommand { pub fn spawn(self) -> Result { let runtime = WineRuntimePaths::from_runfiles()?; let prefix = TempDir::new().context("create isolated Wine prefix")?; - if let Some(powershell_runtime) = runtime.powershell_runtime.as_deref() { - install_powershell_runtime(prefix.path(), powershell_runtime)?; - } + install_powershell_runtime(prefix.path(), &runtime.powershell_runtime)?; let mut command = StdCommand::new(&runtime.wine); configure_wine_environment(&mut command, &runtime, prefix.path()); command @@ -173,17 +171,10 @@ impl WineRuntimePaths { .context("locate Wine runtime directory")? .to_path_buf(); let wineserver = codex_utils_cargo_bin::cargo_bin("wineserver")?; - let powershell_runtime = if std::env::var_os("CARGO_BIN_EXE_pwsh-runtime-marker").is_some() { - let marker = codex_utils_cargo_bin::cargo_bin("pwsh-runtime-marker")?; - Some( - marker - .parent() - .context("locate PowerShell runtime directory")? - .to_path_buf(), - ) - } else { - None - }; + let powershell_runtime = codex_utils_cargo_bin::cargo_bin("pwsh-runtime-marker")? + .parent() + .context("locate PowerShell runtime directory")? + .to_path_buf(); Ok(Self { dll_path, powershell_runtime, @@ -346,10 +337,7 @@ fn configure_wine_environment(command: &mut StdCommand, runtime: &WineRuntimePat .env("TMP", r"C:\windows\temp"); } -/// Installs the opt-in pinned PowerShell runtime into a Wine prefix. -/// -/// The calling `wine_rust_test` target must set `include_powershell = True`; -/// otherwise the runtime marker is absent. +/// Installs the pinned PowerShell runtime into a Wine prefix. pub fn install_pinned_powershell_runtime(prefix: &Path) -> Result<()> { let marker = codex_utils_cargo_bin::cargo_bin("pwsh-runtime-marker")?; let runtime = marker diff --git a/codex-rs/core/tests/remote_env_windows/BUILD.bazel b/codex-rs/core/tests/remote_env_windows/BUILD.bazel index a4ab18dc3e..5feb1021fc 100644 --- a/codex-rs/core/tests/remote_env_windows/BUILD.bazel +++ b/codex-rs/core/tests/remote_env_windows/BUILD.bazel @@ -6,27 +6,22 @@ wine_rust_test( srcs = [ "non_native_cwd_tests.rs", "remote_env_windows_test.rs", - "wine_exec_server_harness.rs", ], crate_name = "remote_env_windows_test", crate_root = "remote_env_windows_test.rs", - include_powershell = True, windows_binaries = { "wine-windows-exec-server": "//codex-rs/exec-server/testing:windows-exec-server", }, deps = [ - "//bazel/rules/testing/wine:wine_test_support", "//codex-rs/core/tests/common", "//codex-rs/exec-server", + "//codex-rs/exec-server/testing:wine-exec-server-harness", "//codex-rs/features", "//codex-rs/protocol", - "//codex-rs/utils/cargo-bin", "//codex-rs/utils/path-uri", - "//codex-rs/utils/pty", "@crates//:anyhow", "@crates//:pretty_assertions", "@crates//:serde_json", - "@crates//:tempfile", "@crates//:tokio", ], ) diff --git a/codex-rs/core/tests/remote_env_windows/README.md b/codex-rs/core/tests/remote_env_windows/README.md index 45c0253e0a..92c9a89739 100644 --- a/codex-rs/core/tests/remote_env_windows/README.md +++ b/codex-rs/core/tests/remote_env_windows/README.md @@ -1,11 +1,8 @@ -# Windows remote-environment tests +# Windows remote-environment test -This directory cross-builds a small Windows exec-server fixture and runs it -under a pinned Wine runtime from x86-64 Linux Bazel. The Windows fixture links -only `codex-exec-server` because the full Codex Windows graph does not yet -cross-build, and the smaller target is faster to iterate on. The direct smoke -test runs pinned PowerShell and built-in `cmd.exe` through the real exec-server -with Windows cwd URI values. +This Bazel-only `test_codex` integration test runs a Windows exec-server fixture +under pinned Wine and exercises the normal model tool-call and remote-execution +path. ## Running the test @@ -15,18 +12,13 @@ bazel test \ --test_output=errors ``` -No system Wine or apt packages are required. The target is intentionally -limited to x86-64 for simplicity, so Bazel marks it incompatible elsewhere. -Every process gets a fresh `WINEPREFIX` and isolated wineserver. - -The reusable rules and Rust harness live under `//bazel/rules/testing`. Tests -resolve their Windows executable with `codex_utils_cargo_bin::cargo_bin`, then -use `WineTestProcess::scope` for async teardown and panic-safe cleanup. +No system Wine is required. Every process gets a fresh `WINEPREFIX` and isolated +wineserver. ## Current limitations -- ConPTY/TTY behavior is not yet covered. +- PowerShell and ConPTY/TTY behavior are not yet covered. - Wine loads shared objects and PE DLLs at runtime, so the host must still provide the declared compatible glibc version. -- ARM64 Linux is intentionally unsupported for now. It is more complicated to - support, and the behavior covered here should not vary by architecture. +- The target is intentionally limited to x86-64 for simplicity. It can expand + if we find aarch64-specific behavior worth testing. diff --git a/codex-rs/core/tests/remote_env_windows/non_native_cwd_tests.rs b/codex-rs/core/tests/remote_env_windows/non_native_cwd_tests.rs index 168e072de8..92b873412b 100644 --- a/codex-rs/core/tests/remote_env_windows/non_native_cwd_tests.rs +++ b/codex-rs/core/tests/remote_env_windows/non_native_cwd_tests.rs @@ -20,34 +20,17 @@ use core_test_support::test_codex::test_codex; use core_test_support::test_codex::turn_permission_fields; use core_test_support::wait_for_event; use serde_json::json; -use tokio::io::AsyncBufReadExt; -use tokio::io::BufReader; -use wine_test_support::WineTestCommand; +use wine_exec_server_harness::WineExecServerHarness; const CALL_ID: &str = "wine-cmd-smoke"; const COMMAND: &str = "echo WINE_BAZEL_OK&&cd"; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn windows_exec_server_rejects_non_native_cwd_uri() -> Result<()> { - let executable = codex_utils_cargo_bin::cargo_bin("wine-windows-exec-server")?; - let mut exec_server = WineTestCommand::new(executable) - .env("CODEX_HOME", r"C:\codex-home") - .spawn()?; - let stdout = exec_server.take_stdout(); + let (exec_server, exec_server_url) = WineExecServerHarness::start().await?; exec_server .scope(async move { - let mut lines = BufReader::new(stdout).lines(); - let exec_server_url = loop { - let line = lines - .next_line() - .await? - .context("Wine exec-server exited before reporting its URL")?; - if line.starts_with("ws://") { - break line; - } - }; - let server = start_mock_server().await; let arguments = serde_json::to_string(&json!({ "cmd": COMMAND, diff --git a/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs b/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs index bb6486bb17..5e93db4bd5 100644 --- a/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs +++ b/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs @@ -1,9 +1,6 @@ #[cfg(not(target_os = "linux"))] compile_error!("the Wine exec-server test can only run on Linux"); -#[path = "wine_exec_server_harness.rs"] -mod wine_exec_server_harness; - #[path = "non_native_cwd_tests.rs"] mod non_native_cwd_tests; @@ -23,12 +20,11 @@ use codex_utils_path_uri::PathUri; use pretty_assertions::assert_eq; use tokio::time::timeout; use wine_exec_server_harness::POWERSHELL_PATH; -use wine_exec_server_harness::POWERSHELL_VERSION; -use wine_exec_server_harness::WINDOWS_WORKSPACE; -use wine_exec_server_harness::WineExecServer; +use wine_exec_server_harness::WineExecServerHarness; const TEST_TIMEOUT: Duration = Duration::from_secs(180); const POWERSHELL_PREFLIGHT_MARKER: &str = "WINE_PWSH_PREFLIGHT"; +const WINDOWS_WORKSPACE: &str = r"C:\workspace"; const POWERSHELL_PREFLIGHT_SCRIPT: &str = concat!( "$ErrorActionPreference = 'Stop'; ", "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); ", @@ -48,11 +44,9 @@ struct CommandOutput { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn windows_exec_server_runs_powershell_and_cmd_under_wine() -> Result<()> { timeout(TEST_TIMEOUT, async { - let (server, websocket_url) = WineExecServer::start().await?; - let test_result = exercise_exec_server(websocket_url).await; - let shutdown_result = server.shutdown().await; - test_result?; - shutdown_result + let (server, websocket_url) = + WineExecServerHarness::start_with_workspace(WINDOWS_WORKSPACE).await?; + server.scope(exercise_exec_server(websocket_url)).await }) .await .context("Wine exec-server test timed out")? @@ -102,16 +96,18 @@ async fn exercise_exec_server(websocket_url: String) -> Result<()> { powershell.output ) })?; + let fields = preflight.split('|').collect::>(); + anyhow::ensure!(fields.len() == 6, "unexpected PowerShell preflight: {preflight}"); + assert_eq!(fields[0], POWERSHELL_PREFLIGHT_MARKER); assert_eq!( - preflight.split('|').collect::>(), - vec![ - POWERSHELL_PREFLIGHT_MARKER, - POWERSHELL_VERSION, - "Core", - "true", - WINDOWS_WORKSPACE, - "92", - ] + fields[1].split('.').next(), + Some("7"), + "expected PowerShell 7.x, got {}", + fields[1], + ); + assert_eq!( + &fields[2..], + &["Core", "true", WINDOWS_WORKSPACE, "92"] ); let cmd = run_non_tty_command( diff --git a/codex-rs/exec-server/testing/BUILD.bazel b/codex-rs/exec-server/testing/BUILD.bazel index ad0f5b8551..bb996b6597 100644 --- a/codex-rs/exec-server/testing/BUILD.bazel +++ b/codex-rs/exec-server/testing/BUILD.bazel @@ -1,4 +1,21 @@ -load("@rules_rust//rust:defs.bzl", "rust_binary") +load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") + +rust_library( + name = "wine-exec-server-harness", + testonly = True, + srcs = ["wine_exec_server_harness.rs"], + crate_name = "wine_exec_server_harness", + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//codex-rs/core/tests/remote_env_windows:__pkg__"], + deps = [ + "//bazel/rules/testing/wine:wine_test_support", + "//codex-rs/utils/cargo-bin", + "//codex-rs/utils/pty", + "@crates//:anyhow", + "@crates//:tempfile", + "@crates//:tokio", + ], +) rust_binary( name = "windows-exec-server", diff --git a/codex-rs/exec-server/testing/windows_exec_server.rs b/codex-rs/exec-server/testing/windows_exec_server.rs index c9ad9c05c9..9efc5286fc 100644 --- a/codex-rs/exec-server/testing/windows_exec_server.rs +++ b/codex-rs/exec-server/testing/windows_exec_server.rs @@ -9,7 +9,6 @@ use codex_exec_server::ExecServerRuntimePaths; #[tokio::main] async fn main() -> Result<(), Box> { - std::fs::create_dir_all(r"C:\workspace")?; let current_exe = std::env::current_exe()?; // This fixture is always a Windows executable, so it neither invokes nor // needs the separate Linux sandbox binary. diff --git a/codex-rs/core/tests/remote_env_windows/wine_exec_server_harness.rs b/codex-rs/exec-server/testing/wine_exec_server_harness.rs similarity index 79% rename from codex-rs/core/tests/remote_env_windows/wine_exec_server_harness.rs rename to codex-rs/exec-server/testing/wine_exec_server_harness.rs index 3b3962cf22..3ecbbde13f 100644 --- a/codex-rs/core/tests/remote_env_windows/wine_exec_server_harness.rs +++ b/codex-rs/exec-server/testing/wine_exec_server_harness.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use std::future::Future; use std::io::Write; +use std::path::Path; use std::path::PathBuf; use std::process::Stdio; use std::time::Duration; @@ -19,11 +21,11 @@ use tokio::time::timeout; const START_TIMEOUT: Duration = Duration::from_secs(30); const BLOCKING_CLEANUP_TIMEOUT: Duration = Duration::from_secs(5); -pub(crate) const POWERSHELL_PATH: &str = r"C:\Program Files\PowerShell\7\pwsh.exe"; -pub(crate) const POWERSHELL_VERSION: &str = "7.2.24"; -pub(crate) const WINDOWS_WORKSPACE: &str = r"C:\workspace"; +/// PowerShell's installation path inside prefixes prepared by the harness. +pub const POWERSHELL_PATH: &str = r"C:\Program Files\PowerShell\7\pwsh.exe"; -pub(crate) struct WineExecServer { +/// Runs the Windows exec-server fixture in an isolated Wine prefix. +pub struct WineExecServerHarness { processes: Option, } @@ -37,10 +39,23 @@ struct WineProcesses { wineserver: PathBuf, } -impl WineExecServer { - pub(crate) async fn start() -> Result<(Self, String)> { +impl WineExecServerHarness { + /// Starts the fixture without creating an application workspace. + pub async fn start() -> Result<(Self, String)> { + Self::start_inner(/*workspace*/ None).await + } + + /// Starts the fixture after creating `workspace` inside the Wine prefix. + pub async fn start_with_workspace(workspace: &str) -> Result<(Self, String)> { + Self::start_inner(Some(workspace)).await + } + + async fn start_inner(workspace: Option<&str>) -> Result<(Self, String)> { let prefix = TempDir::new().context("create Wine prefix")?; wine_test_support::install_pinned_powershell_runtime(prefix.path())?; + if let Some(workspace) = workspace { + create_windows_directory(prefix.path(), workspace)?; + } let executable = codex_utils_cargo_bin::cargo_bin("wine-windows-exec-server")?; let wine = codex_utils_cargo_bin::cargo_bin("wine")?; @@ -159,7 +174,22 @@ impl WineExecServer { } } - pub(crate) async fn shutdown(mut self) -> Result<()> { + /// Runs an operation and then performs bounded asynchronous teardown. + pub async fn scope(self, future: impl Future>) -> Result { + let scope_result = future.await; + let shutdown_result = self.shutdown().await; + match (scope_result, shutdown_result) { + (Ok(value), Ok(())) => Ok(value), + (Err(error), Ok(())) => Err(error), + (Ok(_), Err(error)) => Err(error), + (Err(error), Err(shutdown_error)) => { + Err(error.context(format!("Wine teardown also failed: {shutdown_error:#}"))) + } + } + } + + /// Stops the exec-server process and its isolated wineserver. + pub async fn shutdown(mut self) -> Result<()> { let result = self .processes .as_mut() @@ -171,14 +201,25 @@ impl WineExecServer { } } -impl Drop for WineExecServer { +impl Drop for WineExecServerHarness { fn drop(&mut self) { if self.processes.is_some() && !std::thread::panicking() { - panic!("WineExecServer dropped without calling async shutdown"); + panic!("WineExecServerHarness dropped without calling async shutdown"); } } } +fn create_windows_directory(prefix: &Path, windows_path: &str) -> Result<()> { + let relative = windows_path + .strip_prefix(r"C:\") + .context("Wine test workspace must be an absolute C: path")?; + let host_path = relative + .split('\\') + .fold(prefix.join("drive_c"), |path, segment| path.join(segment)); + std::fs::create_dir_all(&host_path) + .with_context(|| format!("create Wine test workspace {}", host_path.display())) +} + impl WineProcesses { async fn shutdown(&mut self) -> Result<()> { self.process.request_terminate();