From bcec5f1540e0765fd302a443c6435943bb4aecb5 Mon Sep 17 00:00:00 2001
From: Zxilly <zxilly@outlook.com>
Date: Fri, 9 Aug 2024 17:43:11 +0000
Subject: [PATCH] syscall,misc/wasm: fix path expansion on non-unix platforms

When running a go binary compiled to wasm using node.js on a Windows platform,
the absolute path passed in is also incorrectly forced to expand.

For example:

E:\Project\CS_Project\gsv\testdata\result.gob.gz

will results to

open C:\Users\zxilly\AppData\Local\wasm-exec\go1.23rc1\E:\Project\CS_Project\gsv\testdata\result.gob.gz: No such file or directory

C:\Users\zxilly\AppData\Local\wasm-exec\go1.23rc1 is the place of
wasm_exec_node.js

Fixes: #68820

Change-Id: Ic30c6242302f8915ac1b8ea9f24546935cbb791e
GitHub-Last-Rev: f35ff1a2eef86c3b6431bb2be75448c3ac553f1b
GitHub-Pull-Request: golang/go#68255
Reviewed-on: https://go-review.googlesource.com/c/go/+/595797
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
---
 misc/wasm/wasm_exec.js      | 8 ++++++++
 misc/wasm/wasm_exec_node.js | 1 +
 src/syscall/fs_js.go        | 7 +++----
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js
index bc6f2102428..0f635d6d540 100644
--- a/misc/wasm/wasm_exec.js
+++ b/misc/wasm/wasm_exec.js
@@ -73,6 +73,14 @@
 		}
 	}
 
+	if (!globalThis.path) {
+		globalThis.path = {
+			resolve(...pathSegments) {
+				return pathSegments.join("/");
+			}
+		}
+	}
+
 	if (!globalThis.crypto) {
 		throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
 	}
diff --git a/misc/wasm/wasm_exec_node.js b/misc/wasm/wasm_exec_node.js
index 986069087bc..dd65b19867d 100644
--- a/misc/wasm/wasm_exec_node.js
+++ b/misc/wasm/wasm_exec_node.js
@@ -11,6 +11,7 @@ if (process.argv.length < 3) {
 
 globalThis.require = require;
 globalThis.fs = require("fs");
+globalThis.path = require("path");
 globalThis.TextEncoder = require("util").TextEncoder;
 globalThis.TextDecoder = require("util").TextDecoder;
 
diff --git a/src/syscall/fs_js.go b/src/syscall/fs_js.go
index 793b9a2d41c..b6138ebeb1e 100644
--- a/src/syscall/fs_js.go
+++ b/src/syscall/fs_js.go
@@ -16,6 +16,7 @@ import (
 func now() (sec int64, nsec int32)
 
 var jsProcess = js.Global().Get("process")
+var jsPath = js.Global().Get("path")
 var jsFS = js.Global().Get("fs")
 var constants = jsFS.Get("constants")
 
@@ -101,10 +102,8 @@ func Open(path string, openmode int, perm uint32) (int, error) {
 		}
 	}
 
-	if path[0] != '/' {
-		cwd := jsProcess.Call("cwd").String()
-		path = cwd + "/" + path
-	}
+	path = jsPath.Call("resolve", path).String()
+
 	f := &jsFile{
 		path:    path,
 		entries: entries,
-- 
GitLab