lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
8.98k stars 909 forks source link

lib/pq does not compile for wasm #987

Open provtimofei opened 4 years ago

provtimofei commented 4 years ago

``This library does not compile for the GOOS=js GOARCH=wasm The error:

github.com/lib/pq

../../go/pkg/mod/github.com/lib/pq@v1.7.0/connector.go:102:13: undefined: userCurrent I, actually, build a substitute for wasm for my code using lib/pq. And I used build constrains as in https://golang.org/pkg/go/build/#hdr-Build_Constraints. But go mod is pulling lib/pq in anyway, what is definitely a bug. Can you, please, implement empty userCurrent for all unsupported architectures, with a build constrains like // +build !aix !darwin !dragonfly !freebsd !linux !nacl !netbsd !openbsd !plan9 !solaris !rumprun !windows I just want the package to compile for wasm and js.

leighmcculloch commented 3 years ago

The error message above is a little confusing to read, but this issue looks legit. It's impossible to import a package that is part of a Go module that has pq in its dependency graph, even if the importer isn't using the pq functionality.

This is because the pq package does not provide a userCurrent function for all architectures/operating-systems. It only provides an implementation for windows in user_windows.go and for a variety of others in user_posix.go: https://github.com/lib/pq/blob/b2901c7946b69f1e7226214f9760e31620499595/user_posix.go#L3

It looks to me like user_posix.go's build tag could be simplified to !windows and that would make using modules that have pq as a dependency, much easier.

Would such a change be acceptable?

leighmcculloch commented 3 years ago

Another option would be to provide a fallback userCurrent that simply errors, requiring the user to specify the username manually. Something like:

func userCurrent() (string, error) {
    return "", ErrCouldNotDetectUsername
}
Ceddicedced commented 1 week ago

Just to keep this issue updated;

Compiling for WASM via GOOS=js is NOT failing: CGO_ENABLED=0 GOOS=js GOARCH=wasm go build -o ./build/js.wasm .

While compiling to GOOS=wasip1 IS failling:

CGO_ENABLED=0 GOOS=wasip1 GOARCH=wasm go build -o ./build/wasip1.wasm .

# github.com/lib/pq
/go/pkg/mod/github.com/lib/pq@v1.10.10-0.20230707155734-3d613208bca2/connector.go:107:13: undefined: userCurrent