Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

"could not import syscall/js" error message even though it builds and runs successfully #1874

Closed
coreyog opened this issue Aug 26, 2018 · 33 comments

Comments

@coreyog
Copy link

coreyog commented Aug 26, 2018

package main

import (
	"syscall/js"
)

func main() {
	js.Global().Call("alert", "Hello World")
}

When built with GOOS=js GOARCH=wasm go build main.go using Go 1.11 this produces a WASM binary without error. However VSCode shows that there's an error:

could not import syscall/js (can't find import: "syscall/js")

I think this has to do with that namespace being experimental while wasm support stabilizes over the coming versions. I'm given very little details about the error.

As a result of the namespace not being found, intellisense also doesn't work.

I'm running VSCode 1.26.1 on Linux Mint 19 x64 with Go 1.11

@coreyog
Copy link
Author

coreyog commented Aug 26, 2018

I reopened the project to investigate further and when I moused over the import with it's red squiggly line, I got this error:

build constraints exclude all Go files in /usr/local/go/src/syscall/js

I think the plugin doesn't use an altered GOOS and GOARCH when checking the code for errors.

I tried setting go.toolsEnvVars in the project settings but I'm still getting the could not import error on the import.

@coreyog
Copy link
Author

coreyog commented Aug 26, 2018

In Output I see:

/home/corey/src/goprojects/src/wasm-hw>Finished running tool: /home/corey/src/goprojects/bin/golint

/home/corey/src/goprojects/src/wasm-hw>Finished running tool: /usr/local/go/bin/go build -i -o /tmp/go-code-check.198072839 wasm-hw
go build runtime/internal/atomic: mkdir /usr/local/go/pkg/js_wasm: permission denied
go build runtime/internal/sys: open /usr/local/go/pkg/js_wasm/runtime/internal/sys.a: no such file or directory
go build internal/race: mkdir /usr/local/go/pkg/js_wasm: permission denied
go build sync/atomic: mkdir /usr/local/go/pkg/js_wasm: permission denied
go build internal/cpu: open /usr/local/go/pkg/js_wasm/internal/cpu.a: no such file or directory

/home/corey/src/goprojects/src/wasm-hw>Finished running tool: /usr/local/go/bin/go vet ./...

I've set my go.toolsEnvVars as follows:

{
  "go.toolsEnvVars": {
    "GOOS": "js",
    "GOARCH": "wasm",
    "GOROOT": "/usr/local/go",
    "GOPATH": "/home/corey/src/goprojects"
  }
}

GoRoot and GoPath reflect what the normal env variables are. I'm still getting the could not import error. I'm not sure why it's trying to do things in my /usr/local/go folder or how to tell it to build those files in $GOPATH/pkg.

Adding write privileges to the $GOROOT/pkg folder not only feels like a bad move, but it also doesn't entirely fix the problem. The error goes away but there's no intellisense for anything under js.

@eternal-flame-AD
Copy link
Contributor

Experiencing the same issue.

uzuna added a commit to uzuna/go-wasm-demo that referenced this issue Sep 5, 2018
- syscall/jsでエラーが出るのを避ける
- エラーは出ないがmetthodの予測は出ないままなのでさらなる対応が必要
- relation : microsoft/vscode-go#1874
uzuna added a commit to uzuna/go-wasm-demo that referenced this issue Sep 5, 2018
- syscall/jsでエラーが出るのを避ける
- エラーは出ないがmetthodの予測は出ないままなのでさらなる対応が必要
- relation : microsoft/vscode-go#1874

demo: js operation

-
goから操作するのはsyscall/jsのAPIを通して文字列でjsのprop/methodを呼び出す
- Dom操作APIの作成か、Dom操作周りのwrapperを書くのが早いかどちらだろう
uzuna added a commit to uzuna/go-wasm-demo that referenced this issue Sep 5, 2018
- syscall/jsでエラーが出るのを避ける
- エラーは出ないがmetthodの予測は出ないままなのでさらなる対応が必要
- relation : microsoft/vscode-go#1874
@ramya-rao-a
Copy link
Contributor

Can you try adding the setting "go.installDependenciesWhenBuilding": false. See #1464 for another case where the build process had permission issues

@eternal-flame-AD
Copy link
Contributor

@ramya-rao-a Thanks, I followed your steps and the error is not coming out. However, there is still no IntelliSense for syscall/js package.

@ramya-rao-a
Copy link
Contributor

  • If there are any running process for gocode, close them
  • Run gocode -s -debug in a terminal and try for intellisense in VS Code. Results from gocode will show up in the terminal.

If the results from gocode show zero candidates, then the issue is with gocode

@eternal-flame-AD
Copy link
Contributor

It indeed said zero candidates. I guess that's an upstream issue then.

@ramya-rao-a
Copy link
Contributor

Are you using modules?
Also, can you try again by setting GOOS=js GOARCH=wasm in your terminal before running gocode -s -debug. Remember to kill existing processes for gocode

And why is the GOPATH being set to /home/corey/src/goprojects? Shouldnt it be just /home/corey ?

@coreyog
Copy link
Author

coreyog commented Sep 12, 2018

It should be /home/corey/src/goprojects. /home/corey/src also has other non-Go project folders that I don't want mixed in with go projects.

I'll try setting env variables like you suggest when I get a chance.

@ramya-rao-a
Copy link
Contributor

  • What do you get when you run Go: Current GOPATH ?
  • Are you using the new modules feature in Go 1.11?

@eternal-flame-AD
Copy link
Contributor

I tried setting env before starting gocode but it is still not spawning candidates :(

@ramya-rao-a
Copy link
Contributor

@eternal-flame-AD You still didn't mention if you were using the new modules feature or not :)

@coreyog What about you? Are you using modules?

@coreyog
Copy link
Author

coreyog commented Sep 13, 2018

No, I am not. As shown by my minimal code sample above.

@ramya-rao-a
Copy link
Contributor

@coreyog And you are able to get out of the initial error reporting by VS Code like @eternal-flame-AD ?

Can you check your $GOROOT/pkg? Do you have a folder there for wasm? Does that have the package for syscall?

@eternal-flame-AD
Copy link
Contributor

I was NOT using modules. :) @ramya-rao-a

@ramya-rao-a
Copy link
Contributor

@eternal-flame-AD @coreyog If you don't have syscall/js under $GOROOT/pkg/js_wasm, then can you run go install syscall/js after setting the GOOS and GOARCH as env vars and try again?

@eternal-flame-AD
Copy link
Contributor

I ran the command and $GOPATH/pkg/js_wasm dir appeared. However, the IntelliSense wasn't fixed.

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 13, 2018

@eternal-flame-AD Ensure GOOS and GOARCH are js, wasm and retry
#1874 (comment)

Do you see zero candidates still?

@eternal-flame-AD
Copy link
Contributor

Yep I think the problem was still there:(

 ~/code/go/src/github.com/eternal-flame-AD/hellowasm  ls /usr/lib/go/pkg
include  js_wasm  linux_amd64  linux_amd64_dynlink  linux_amd64_race  linux_amd64_shared  linux_amd64_testcshared_shared  obj  tool
 ~/code/go/src/github.com/eternal-flame-AD/hellowasm  kill 1521
 ~/code/go/src/github.com/eternal-flame-AD/hellowasm  kill 32471
 ~/code/go/src/github.com/eternal-flame-AD/hellowasm  rm /tmp/gocode-daemon.ef
 ~/code/go/src/github.com/eternal-flame-AD/hellowasm  env GOOS=js GOARCH=wasm gocode -s --debug
2018/09/13 12:38:22 Got autocompletion request for '/home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go'
2018/09/13 12:38:22 Cursor at: 99
2018/09/13 12:38:22 -------------------------------------------------------
package main

import (
        "fmt"
        "syscall/js"
)

func main() {
        fmt.Println("loaded")
        js.Global()
        j#s.
}
2018/09/13 12:38:22 -------------------------------------------------------
2018/09/13 12:38:22 Error parsing input file (outer block):
2018/09/13 12:38:22  /home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go:12:1: expected selector or type assertion, found '}'
2018/09/13 12:38:22  /home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go:12:3: expected ';', found 'EOF'
2018/09/13 12:38:22  /home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go:12:3: expected '}', found 'EOF'
2018/09/13 12:38:22 Elapsed duration: 2.52415ms
2018/09/13 12:38:22 Offset: 0
2018/09/13 12:38:22 Number of candidates found: 1
2018/09/13 12:38:22 Candidates are:
2018/09/13 12:38:22   package js
2018/09/13 12:38:22 =======================================================
2018/09/13 12:38:22 Got autocompletion request for '/home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go'
2018/09/13 12:38:22 Cursor at: 101
2018/09/13 12:38:22 -------------------------------------------------------
package main

import (
        "fmt"
        "syscall/js"
)

func main() {
        fmt.Println("loaded")
        js.Global()
        js.#
}
2018/09/13 12:38:22 -------------------------------------------------------
2018/09/13 12:38:22 Error parsing input file (outer block):
2018/09/13 12:38:22  /home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go:11:5: expected selector or type assertion, found ';'
2018/09/13 12:38:22 Elapsed duration: 2.981524ms
2018/09/13 12:38:22 Offset: 0
2018/09/13 12:38:22 Number of candidates found: 0
2018/09/13 12:38:22 Candidates are:
2018/09/13 12:38:22 =======================================================
2018/09/13 12:38:22 Got autocompletion request for '/home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go'
2018/09/13 12:38:22 Cursor at: 121
2018/09/13 12:38:22 -------------------------------------------------------
package main
import "syscall/js"

import (
        "fmt"
        "syscall/js"
)

func main() {
        fmt.Println("loaded")
        js.Global()
        js.#
}
2018/09/13 12:38:22 -------------------------------------------------------
2018/09/13 12:38:22 Error parsing input file (outer block):
2018/09/13 12:38:22  /home/ef/code/go/src/github.com/eternal-flame-AD/hellowasm/main.go:12:5: expected selector or type assertion, found ';'
2018/09/13 12:38:22 Elapsed duration: 2.52288ms
2018/09/13 12:38:22 Offset: 0
2018/09/13 12:38:22 Number of candidates found: 0
2018/09/13 12:38:22 Candidates are:
2018/09/13 12:38:22 =======================================================

@ramya-rao-a
Copy link
Contributor

@eternal-flame-AD Last question. You have set GOOS and GOARCH to js and wasm in the toolsEnvVars setting right?

"go.toolsEnvVars": {
    "GOOS": "js",
    "GOARCH": "wasm"
  }

@eternal-flame-AD
Copy link
Contributor

 ~  cat code/go/src/github.com/eternal-flame-AD/hellowasm/.vscode/settings.json 
{
    "go.toolsEnvVars": {
        "GOARCH":"wasm",
        "GOOS":"js",
    },
    "go.testEnvVars": {
        "GOARCH":"wasm",
        "GOOS":"js",
    },
    "go.installDependenciesWhenBuilding": false,
}

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 13, 2018

Ok, I am running out of ideas, lets take this up in mdempsky/gocode#56

@microsoft microsoft deleted a comment from eternal-flame-AD Sep 13, 2018
@vscodebot vscodebot bot closed this as completed Sep 20, 2018
@vscodebot
Copy link

vscodebot bot commented Sep 20, 2018

This issue has been closed automatically because it needs more information and has not had recent activity. Thank you for your contributions.

@eternal-flame-AD
Copy link
Contributor

I think this issue should be reopened. I dug a little through the vscode-go code and found this line which masked all GOARCH and GOOS env.

https://github.com/Microsoft/vscode-go/blob/d1a9f9a8cd7428c552517d72af148b7b116b9ed0/src/goSuggest.ts#L185
After I got rid of this in a dev environment, the suggestions worked perfectly.

@eternal-flame-AD
Copy link
Contributor

I am not sure how to reproduce the issue mentioned on the code comments before this line. However I think we should definitely at least add an exception for wasm/js

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Oct 15, 2018

That's a great find @eternal-flame-AD!

The commit behind that change is f73f41d which was made as part of the PR #156 to fix the issue #140

@stamblerre Any idea if the comment in the above commit still holds true?

@eternal-flame-AD Can you try cross-compiling with say linux and see if your fix still works?

@ramya-rao-a ramya-rao-a reopened this Oct 15, 2018
@stamblerre
Copy link
Contributor

@ramya-rao-a: gocode tracks the build.Context of requests, so if you explicitly set GOOS and GOARCH in a client request, it should work correctly.

@ramya-rao-a
Copy link
Contributor

Thanks @stamblerre!

@eternal-flame-AD Would you be interested in submitting a PR to fix this?

@eternal-flame-AD
Copy link
Contributor

Yep.

I found that when GOOS and GOARCH are set, gocode barely tracks import correctly:( I have to run like sudo env GOOS=js GOARCH=wasm go install math/rand in order to get math/rand suggestions working on wasm/js

I found a solution to the problem that is enabling source import when spawning gocode daemon. Should we include this change in the PR?

diff --git a/src/goSuggest.ts b/src/goSuggest.ts
index a5d9e66..40a63d1 100644
--- a/src/goSuggest.ts
+++ b/src/goSuggest.ts
@@ -187,14 +187,11 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
 				return reject();
 			}
 
-			// Unset GOOS and GOARCH for the `gocode` process to ensure that GOHOSTOS and GOHOSTARCH
-			// are used as the target operating system and architecture. `gocode` is unable to provide
-			// autocompletion when the Go environment is configured for cross compilation.
-			let env = Object.assign({}, getToolsEnvVars(), { GOOS: '', GOARCH: '' });
+			let env = Object.assign({}, getToolsEnvVars());
 			let stdout = '';
 			let stderr = '';
 
-			let goCodeFlags = ['-f=json'];
+			let goCodeFlags = ['-f=json','-source=true'];
 			if (!this.setGocodeOptions) {
 				goCodeFlags.push('-builtin');
 			}

@ramya-rao-a
Copy link
Contributor

Including the -source flag slows things down a lot.

Are you saying that if you add the source flag, you dont have to run go install on the packages for which you want completions?

@eternal-flame-AD
Copy link
Contributor

Yeah. Or maybe only enabling this when cross compiling?

@eternal-flame-AD
Copy link
Contributor

I think we could move this discussion to #2015 as the corrent topic is more related to detailed implementation of the fix.

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Oct 17, 2018

The fix for this issue is now out in the latest update to the Go extension (0.6.92). Thanks @eternal-flame-AD and @stamblerre!

Note: Part of the fix here is in gocode. Therefore, run the command Go: Install/Update Tools, select gocode and press Ok to update it

@vscodebot vscodebot bot locked and limited conversation to collaborators Dec 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants