Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cli): strip Windows verbatim paths before spawning node api (#74066)

Chris Volzerd871ba0d3804
diff --git a/cli/.sampo/changesets/windows-api-verbatim-path.md b/cli/.sampo/changesets/windows-api-verbatim-path.md
new file mode 100644
index 000000000000..205e11d9a94c
--- /dev/null
+++ b/cli/.sampo/changesets/windows-api-verbatim-path.md
@@ -0,0 +1,5 @@
+---
+cargo/posthog-cli: patch
+---
+
+Fixed `posthog-cli api` commands crashing on Windows when loading the bundled Node.js script.
diff --git a/cli/Cargo.lock b/cli/Cargo.lock
index ce356ce29daa..0c7c682ad7c8 100644
--- a/cli/Cargo.lock
+++ b/cli/Cargo.lock
@@ -2093,6 +2093,7 @@ dependencies = [
"colored",
"crossterm 0.28.1",
"dirs",
+ "dunce",
"globset",
"inquire",
"libc",
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 9381cda62ac1..6bb0bcd8ac5a 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -24,6 +24,7 @@ path = "src/main.rs"
[dependencies]
clap = { version = "4.5.31", features = ["derive", "env"] }
dirs = "6.0.0"
+dunce = "1.0.5"
inquire = "0.7.5"
chrono = "0.4.42"
posthog-symbol-data = "0.5.0"
diff --git a/cli/src/api_proxy.rs b/cli/src/api_proxy.rs
index 3380342e4bd1..d2ca92fbeb64 100644
--- a/cli/src/api_proxy.rs
+++ b/cli/src/api_proxy.rs
@@ -107,8 +107,12 @@ impl ApiProxyError {
}
}
+// This module canonicalizes through `dunce` because `std::fs::canonicalize`
+// returns `\\?\`-prefixed verbatim paths on Windows, and Node cannot load a
+// main script from such a path (it exits with `EISDIR: illegal operation on a
+// directory, lstat 'C:'` before any script code runs).
fn canonicalize_file(path: &Path) -> Option<PathBuf> {
- let resolved = path.canonicalize().ok()?;
+ let resolved = dunce::canonicalize(path).ok()?;
if resolved.is_file() {
Some(resolved)
} else {
@@ -152,8 +156,7 @@ fn materialize_embedded_script(
.map_err(|source| materialize_failed(MaterializeStep::CreateDir, source))?;
fs::write(&path, bundle)
.map_err(|source| materialize_failed(MaterializeStep::Write, source))?;
- let resolved = path
- .canonicalize()
+ let resolved = dunce::canonicalize(&path)
.map_err(|source| materialize_failed(MaterializeStep::Canonicalize, source))?;
if !resolved.is_file() {
return Err(materialize_failed(
@@ -212,11 +215,10 @@ fn find_script() -> Result<PathBuf, ApiProxyError> {
}
let path = PathBuf::from(path);
let resolved =
- path.canonicalize()
- .map_err(|source| ApiProxyError::ConfiguredPathMissing {
- path: path.display().to_string(),
- source,
- })?;
+ dunce::canonicalize(&path).map_err(|source| ApiProxyError::ConfiguredPathMissing {
+ path: path.display().to_string(),
+ source,
+ })?;
if !resolved.is_file() {
return Err(ApiProxyError::ConfiguredPathNotAFile {
path: resolved.display().to_string(),
@@ -386,14 +388,19 @@ mod tests {
assert_eq!(
resolved,
- temp_dir
- .path()
- .join("api-cli")
- .join(env!("CARGO_PKG_VERSION"))
- .join(API_CLI_BUNDLE)
- .canonicalize()
- .expect("canonicalize materialized bundle")
+ dunce::canonicalize(
+ temp_dir
+ .path()
+ .join("api-cli")
+ .join(env!("CARGO_PKG_VERSION"))
+ .join(API_CLI_BUNDLE),
+ )
+ .expect("canonicalize materialized bundle")
);
+ // Node rejects verbatim paths as a main script, so the resolved path
+ // must stay in legacy `C:\...` form on Windows.
+ #[cfg(windows)]
+ assert!(!resolved.to_string_lossy().starts_with(r"\\?\"));
assert_eq!(
fs::read(resolved).expect("read materialized bundle"),
b"embedded bundle"
@@ -453,7 +460,7 @@ mod tests {
assert_eq!(
resolved,
- legacy_bundle.canonicalize().expect("canonicalize legacy")
+ dunce::canonicalize(&legacy_bundle).expect("canonicalize legacy")
);
}
@@ -488,8 +495,7 @@ mod tests {
assert_eq!(
resolved,
- embedded_bundle_path(&install_dir)
- .canonicalize()
+ dunce::canonicalize(embedded_bundle_path(&install_dir))
.expect("canonicalize embedded bundle")
);
assert_eq!(