sirupsen / logrus

Structured, pluggable logging for Go.
MIT License
24.3k stars 2.26k forks source link

can't build for wasm (go 1.21 rc) #1394

Closed imjasonh closed 11 months ago

imjasonh commented 11 months ago

The upcoming Go 1.21 release includes support for compiling to Web Assembly (wasm): https://tip.golang.org/doc/go1.21#wasm

You can try out the RC:

$ go install golang.org/dl/go1.21rc2@latest
$ go1.21rc2 download
$ go1.21rc2 version
go version go1.21rc2 darwin/arm64

However, I've found that anything I try to build that depends on logrus fails when building for Wasm:

GOOS=wasip1 GOARCH=wasm go1.21rc2 build
# github.com/sirupsen/logrus
../../go/pkg/mod/github.com/sirupsen/logrus@v1.9.3/terminal_check_notappengine.go:13:10: undefined: isTerminal

(source here: https://gist.github.com/imjasonh/1ca7e1bd0d95d5c54b8550c50fb557bd)

This isn't a big deal for now, because it's still an RC, but it could be nice to figure out how to avoid this dependency for wasm builds.

imjasonh commented 11 months ago

I suspect we'd just need something like this, but for wasm: https://github.com/sirupsen/logrus/blob/master/terminal_check_js.go

imjasonh commented 11 months ago

Aaaaand you're already on it. https://github.com/sirupsen/logrus/commit/7165f5e779a587f37fff841ecbae4d6bce9a2aa1

$ go get -u github.com/sirupsen/logrus@master
go: downloading github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af
$ GOOS=wasip1 GOARCH=wasm go1.21rc2 build
# works 

Thank you! 😄

HarikrishnanBalagopal commented 10 months ago

Update 2

Ok so it seems the PR https://github.com/sirupsen/logrus/pull/1388 is not part of the 1.9.3 release version Changed the line in go.mod to require github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af and now it works

$ make build-wasm 
CGO_ENABLED=0 GOOS=wasip1 GOARCH=wasm go build -o bin/test.wasm .
# CGO_ENABLED=0 tinygo build -o bin/test.wasm -target=wasi .
$ echo $?
0

Update

With go 1.21

$ go version
go version go1.21.0 darwin/amd64
$ GOOS=wasip1 GOARCH=wasm go build -o main.wasm main.go
# github.com/sirupsen/logrus
/Users/user/go/pkg/mod/github.com/sirupsen/logrus@v1.9.3/terminal_check_notappengine.go:13:10: undefined: isTerminal

The /Users/user/go/pkg/mod/github.com/sirupsen/logrus@v1.9.3/terminal_check_notappengine.go file

// +build !appengine,!js,!windows,!nacl,!plan9

package logrus

import (
    "io"
    "os"
)

func checkIfTerminal(w io.Writer) bool {
    switch v := w.(type) {
    case *os.File:
        return isTerminal(int(v.Fd()))
    default:
        return false
    }
}

Original

Hi, I am running into this issue when compiling with TinyGo for WASI target

$ go version
go version go1.20.7 darwin/amd64
$ tinygo version
tinygo version 0.28.1 darwin/amd64 (using go version go1.20.7 and LLVM version 15.0.0)
$ tinygo build -o main.wasm -target=wasi main.go

tinygo:wasm-ld: error: /var/folders/dt/y3srs7vs6pz9bqwrbjwt38pc0000gp/T/tinygo4119433949/main.o: undefined symbol: golang.org/x/sys/unix.Syscall
failed to run tool: wasm-ld
error: failed to link /var/folders/dt/y3srs7vs6pz9bqwrbjwt38pc0000gp/T/tinygo4119433949/main: exit status 1

The main.go file

package main

import "github.com/sirupsen/logrus"

func main() {
    logrus.Infof("start")
    logrus.Infof("done")
}

The go.mod file

module foo.com/b

go 1.20

require github.com/sirupsen/logrus v1.9.3

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

The go.sum file

github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=