Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-windows-utf8-console-codepage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

Force UTF-8 console output on Windows by calling `SetConsoleOutputCP(65001)` at startup, preventing mojibake when the system default codepage is CP-1252
9 changes: 2 additions & 7 deletions crates/google-workspace-cli/src/helpers/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,8 @@ fn process_file(path: &Path) -> Result<Option<serde_json::Value>, GwsError> {
filename.trim_end_matches(".js").trim_end_matches(".gs"),
),
"html" => ("HTML", filename.trim_end_matches(".html")),
"json" => {
if filename == "appsscript.json" {
("JSON", "appsscript")
} else {
return Ok(None);
}
}
"json" if filename == "appsscript.json" => ("JSON", "appsscript"),
"json" => return Ok(None),
_ => return Ok(None),
};

Expand Down
4 changes: 4 additions & 0 deletions crates/google-workspace-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ use error::{print_error_json, GwsError};

#[tokio::main]
async fn main() {
// Force UTF-8 console output on Windows; no-op on other platforms.
#[cfg(windows)]
output::set_console_utf8();

// Load .env file if present (silently ignored if missing)
let _ = dotenvy::dotenv();

Expand Down
40 changes: 40 additions & 0 deletions crates/google-workspace-cli/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ pub(crate) fn info(msg: &str) {
eprintln!("{}", sanitize_for_terminal(msg));
}

// ── Windows console codepage ───────────────────────────────────────────

/// Force the Windows console output codepage to UTF-8 (CP 65001) at startup.
/// Without this, the default system codepage (typically CP-1252) mangles
/// non-ASCII characters printed by `println!` before Rust's stdio layer can
/// re-encode them.
///
/// Failure is non-fatal — terminals that already use UTF-8 will behave
/// correctly regardless, and the call is safe to make unconditionally on any
/// Windows version that ships `kernel32.dll`.
#[cfg(windows)]
pub(crate) fn set_console_utf8() {
extern "system" {
fn SetConsoleOutputCP(wCodePageID: u32) -> i32;
}
// SAFETY: SetConsoleOutputCP is a documented Win32 API with no memory-
// safety requirements. The return value is ignored because failure is
// non-fatal — the terminal simply retains its existing codepage.
let _ = unsafe { SetConsoleOutputCP(65001) };
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -154,3 +175,22 @@ mod tests {
assert!(result.contains("hello"));
}
}

#[cfg(all(test, windows))]
mod windows_tests {
use super::*;

#[test]
fn set_console_utf8_sets_codepage_65001() {
extern "system" {
fn GetConsoleOutputCP() -> u32;
}
set_console_utf8();
// GetConsoleOutputCP returns 0 when no console is attached (e.g. in
// redirected test output). Only assert when we actually have a console.
let cp = unsafe { GetConsoleOutputCP() };
if cp != 0 {
assert_eq!(cp, 65001, "expected UTF-8 codepage (65001), got {cp}");
}
}
}
Loading