Gopls reports warning on vsc when creating os/arch specific files

I’m using visual studio code on windows

I have a package with 3 files: osctrl_darwin.go, osctrl_linux.go and osctrl_windows.go
the first 2 contain:

package osctrl

import (
	"syscall"
)

// GetSyscallNewProcessGroup returns a SysProcAttr struct to start a new process group
func GetSyscallNewProcessGroup() *syscall.SysProcAttr {
	syscallNewProcessGroupLin := &syscall.SysProcAttr{
		Setpgid: true,
	}

	return syscallNewProcessGroupLin
}

the third one (for windows) is this:

package osctrl

import (
	"syscall"
)

// GetSyscallNewProcessGroup returns a SysProcAttr struct to start a new process group
func GetSyscallNewProcessGroup() *syscall.SysProcAttr {
	syscallNewProcessGroupWin := &syscall.SysProcAttr{
		CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
	}

	return syscallNewProcessGroupWin
}

gopls reports No packages found for open file C:my\pack\path\osctrl\osctrl_darwin.go: <nil>. If this file contains build tags, try adding "-tags=<build tag>" to your gopls "buildFlag" configuration (see (https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags-string). Otherwise, see the troubleshooting guidelines for help investigating (https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md).

and I am unable to get info when I hover with the cursor on syscall.SysProcAttr (in the linux and darwin files) while I can on windows

(the program compiles fine thought with set GOOS=linux && set GOARCH=386 && go build .)

what should I do to solve these warnings?

Gopls uses golang compiler API, and therefore can’t see into the files that are not buildable on your platform.

You can tell the gopls to pretend it was for Darwin now, though Linux then still won’t work, while windows is now broken.

It’s your choice to switch back and forth between “profiles” or life with the fact that you won’t have tooling support in some files.

ok thanks @NobbZ , how can I tell gopls to switch to an other os?

Add corresponding tag to that list.

@NobbZ in this case if I want gopls to interpret the code as if I was on linux what should I write?

(on that site, there are literally 2 lines about “buildFlags”)

As I understand the info about buildflags, it’s linux.

@NobbZ I tried gopls -tags linux but it does not work…

I might have found a solution:

I have to add:

"gopls.env": {
    "GOFLAGS": "-tags=linux",
},

in .vscode/settings.json

is it correct? (can I add multiple OS?)

Why don’t you do what is explained in the linked document?

{
  "gopls": {
    "buildFlags" : [
      "linux"
    ]
  }
}

(Typed on mobile, might contain errors)

@NobbZ tried your solution but there is a gopls error:

Error loading workspace: err: exit status 1: stderr: cannot find package "linux" in any of: C:\Program Files\Go\src\linux (from $GOROOT) C:\Users\luca\go\src\linux (from $GOPATH) cannot find package "-e" in any of: C:\Program Files\Go\src\-e (from $GOROOT) C:\Users\luca\go\src\-e (from $GOPATH) cannot find package "-f" in any of: C:\Program Files\Go\src\-f (from $GOROOT) C:\Users\luca\go\src\-f (from $GOPATH) cannot find package "{{context.ReleaseTags}}" in any of: C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT) C:\Users\luca\go\src\{{context.ReleaseTags}} (from $GOPATH) : packages.Load error

what should I do?
(plus when I do my solution there are errors (not warnings) for the duplication of a function in the _windows.go file)

thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.