Skip to content

Commit de80a0b

Browse files
committed
Try further restricting where the patched go compiler is used.
1 parent 301bb7d commit de80a0b

1 file changed

Lines changed: 35 additions & 37 deletions

File tree

flake.nix

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,42 @@
2020
flake-utils.lib.eachDefaultSystem (
2121
system:
2222
let
23-
# Overlay to patch Go to always use Fetch API in WASM
24-
goWasmFetchOverlay = final: prev: {
25-
go_1_24 = prev.go_1_24.overrideAttrs (oldAttrs: {
26-
patches = (oldAttrs.patches or [ ]) ++ [
27-
(final.writeText "go-wasm-always-use-fetch.patch" ''
28-
--- a/src/net/http/roundtrip_js.go
29-
+++ b/src/net/http/roundtrip_js.go
30-
@@ -13,7 +13,6 @@ import (
31-
"io"
32-
"net/http/internal/ascii"
33-
"strconv"
34-
- "strings"
35-
"syscall/js"
36-
)
37-
38-
@@ -56,8 +55,8 @@ var jsFetchMissing = js.Global().Get("fetch").IsUndefined()
39-
//
40-
// TODO(go.dev/issue/60810): See if it's viable to test the Fetch API
41-
// code path.
42-
-var jsFetchDisabled = js.Global().Get("process").Type() == js.TypeObject &&
43-
- strings.HasPrefix(js.Global().Get("process").Get("argv0").String(), "node")
44-
+// Patched to always use Fetch API for better WASM compatibility in Node.js
45-
+var jsFetchDisabled = false
46-
47-
// RoundTrip implements the [RoundTripper] interface using the WHATWG Fetch API.
48-
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
49-
'')
50-
];
51-
});
52-
};
53-
54-
# Apply overlay to get patched Go
23+
# Import nixpkgs without any overlays
5524
pkgs = import nixpkgs {
5625
inherit system;
57-
overlays = [ goWasmFetchOverlay ];
5826
};
5927

28+
# Use regular Go for everything except WASM builds
6029
go = pkgs.go_1_24;
30+
31+
# Create patched Go specifically for WASM builds
32+
goWasm = go.overrideAttrs (oldAttrs: {
33+
patches = (oldAttrs.patches or [ ]) ++ [
34+
(pkgs.writeText "go-wasm-always-use-fetch.patch" ''
35+
--- a/src/net/http/roundtrip_js.go
36+
+++ b/src/net/http/roundtrip_js.go
37+
@@ -13,7 +13,6 @@ import (
38+
"io"
39+
"net/http/internal/ascii"
40+
"strconv"
41+
- "strings"
42+
"syscall/js"
43+
)
44+
45+
@@ -56,8 +55,8 @@ var jsFetchMissing = js.Global().Get("fetch").IsUndefined()
46+
//
47+
// TODO(go.dev/issue/60810): See if it's viable to test the Fetch API
48+
// code path.
49+
-var jsFetchDisabled = js.Global().Get("process").Type() == js.TypeObject &&
50+
- strings.HasPrefix(js.Global().Get("process").Get("argv0").String(), "node")
51+
+// Patched to always use Fetch API for better WASM compatibility in Node.js
52+
+var jsFetchDisabled = false
53+
54+
// RoundTrip implements the [RoundTripper] interface using the WHATWG Fetch API.
55+
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
56+
'')
57+
];
58+
});
6159
nodejs = pkgs.nodejs_22;
6260

6361
# Treefmt config.
@@ -86,7 +84,7 @@
8684

8785
nativeBuildInputs = [
8886
pkgs.bash
89-
go
87+
goWasm # Use patched Go for WASM build
9088
nodejs
9189
pkgs.yarn
9290
pkgs.yarnConfigHook
@@ -174,9 +172,9 @@
174172
echo " return bytes;" >> dist/sindri/wasm/wasm.mjs
175173
echo "}" >> dist/sindri/wasm/wasm.mjs
176174
177-
# Copy the real Go wasm_exec.js runtime.
178-
cp "${go}/share/go/lib/wasm/wasm_exec.js" dist/sindri/wasm/wasm_exec.js
179-
cp "${go}/share/go/lib/wasm/wasm_exec.js" dist/sindri/wasm/wasm_exec.mjs
175+
# Copy the real Go wasm_exec.js runtime from the patched Go.
176+
cp "${goWasm}/share/go/lib/wasm/wasm_exec.js" dist/sindri/wasm/wasm_exec.js
177+
cp "${goWasm}/share/go/lib/wasm/wasm_exec.js" dist/sindri/wasm/wasm_exec.mjs
180178
181179
runHook postBuild
182180
'';

0 commit comments

Comments
 (0)