onflow/cadence-tools

Language Server does not build for WebAssembly anymore

turbolent opened this issue ยท 10 comments

Problem

Attempting to compile the Language Server for WebAssembly fails, though it used to work:

Steps to Reproduce

$ cd languageserver
$ make wasm
GOARCH=wasm GOOS=js go build -o languageserver.wasm  ./cmd/languageserver
package github.com/onflow/cadence-tools/languageserver/cmd/languageserver
	imports github.com/onflow/cadence-tools/languageserver/server
	imports github.com/onflow/cadence-tools/lint
	imports github.com/onflow/flow-cli/pkg/flowkit/gateway
	imports github.com/onflow/flow-emulator
	imports github.com/onflow/flow-emulator/convert
	imports github.com/onflow/flow-emulator/convert/sdk
	imports github.com/onflow/flow-go/access
	imports github.com/onflow/flow-go/consensus/hotstuff
	imports github.com/onflow/flow-go/module
	imports github.com/onflow/flow-go/state/protocol
	imports github.com/onflow/flow-go/storage
	imports github.com/dgraph-io/badger/v2
	imports golang.org/x/sys/unix: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/golang.org/x/sys@v0.2.0/unix
make: *** [wasm] Error 1

Acceptance Criteria

Compilation to WebAssembly should succeed like it used to.

Investigation

Problem

  • The language server depends on flowkit
  • flowkit started to depend on the test framework
  • The test framework depends on the emulator
  • The Emulator does not support compilation to WebAssembly

Potential solutions

  • Get the Emulator to compile to WebAssembly

    • @bluesign already started working on it ๐ŸŽ‰ ๐Ÿ‘ See onflow/flow-emulator#301

    • With onflow/flow-emulator#301 merged, GOARCH=wasm GOOS=js go build ./cmd/emulator still fails, because flow-go itself does not build for WebAssembly:

      Click to expand
      $ GOARCH=wasm GOOS=js go build ./cmd/emulator
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/convert
      	imports github.com/onflow/flow-emulator/convert/sdk
      	imports github.com/onflow/flow-go/access
      	imports github.com/onflow/flow-go/consensus/hotstuff/signature
      	imports github.com/onflow/flow-go/storage
      	imports github.com/dgraph-io/badger/v2
      	imports golang.org/x/sys/unix: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/golang.org/x/sys@v0.4.0/unix
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/storage/sqlite
      	imports github.com/glebarez/go-sqlite
      	imports modernc.org/libc
      	imports modernc.org/libc/errno: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/modernc.org/libc@v1.22.2/errno
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/storage/sqlite
      	imports github.com/glebarez/go-sqlite
      	imports modernc.org/libc
      	imports modernc.org/libc/pthread: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/modernc.org/libc@v1.22.2/pthread
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/storage/sqlite
      	imports github.com/glebarez/go-sqlite
      	imports modernc.org/libc
      	imports modernc.org/libc/signal: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/modernc.org/libc@v1.22.2/signal
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/storage/sqlite
      	imports github.com/glebarez/go-sqlite
      	imports modernc.org/libc
      	imports modernc.org/libc/stdio: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/modernc.org/libc@v1.22.2/stdio
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/storage/sqlite
      	imports github.com/glebarez/go-sqlite
      	imports modernc.org/libc/sys/types: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/modernc.org/libc@v1.22.2/sys/types
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/storage/sqlite
      	imports github.com/glebarez/go-sqlite
      	imports modernc.org/libc
      	imports modernc.org/libc/time: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/modernc.org/libc@v1.22.2/time
      package github.com/onflow/flow-emulator/cmd/emulator
      	imports github.com/onflow/flow-emulator
      	imports github.com/onflow/flow-emulator/storage/sqlite
      	imports github.com/glebarez/go-sqlite
      	imports modernc.org/libc
      	imports modernc.org/libc/unistd: build constraints exclude all Go files in /Users/bastian/go/pkg/mod/modernc.org/libc@v1.22.2/unistd
    • @bluesign is also already trying to get flow-go to compile for WebAssembly ๐ŸŽ‰ ๐Ÿ‘ See onflow/flow-go#3879

  • Remove test framework from flowkit, move further down in the dependency chain (Flow CLI)

Preventing regression

  • In flow-cli: We should build flowkit for WebAssembly on CI
  • In cadence-tools: We should build the LS for WebAssembly on CI, by running make wasm

@turbolent there is an ugly workaround, if you wish ( while waiting flow-go fixes to land, it will take a bit of time probably )

@bluesign Great idea to replace the problematic dependency (badger) with a mock/fake ๐Ÿ‘
Also, thank you for working on bringing back a storage solution suitable for WebAssembly ๐Ÿ‘

I'll ask the protocol team to give some guidance so timing/effort regarding replacing/abstracting badger in flow-go, i.e. if we should use the workaround or rather wait for the proper fix.

I refactored flowkit to not depend on cadence-tools, I think generally speaking that's better architecture since any used tools should be used by the flow-cli package directly and not by its flowkit package.
onflow/flow-cli#845

However, this is not proving to work. When I try to bisect and get to that same difference @turbolent you mentioned I manage to make it work if the /linter import changes. This is really weird. https://github.com/onflow/cadence-tools/compare/f46efb5..4e04255#diff-9dc030944615b63c919527ae35f799e2c63ff5137f18ef2165d07d808fd2b81fL52

Also related: #32

I believe the issue is coming out of cadence-tools using the flowkit/gateway which in turn uses emulator, which wasn't the case before. I can refactor so LS doesn't use the emulator and then for lint I believe I can do the same.

@sideninja Sounds good! Thanks for the refactoring / decoupling. Please let me know if you need any help

@sideninja Is the refactoring/decoupling happening in another issue or is this it?

The work I'm doing refactoring flowkit now will allow the work to unblock this.