Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: support single file mode #34160

Closed
stamblerre opened this issue Sep 7, 2019 · 6 comments
Closed

x/tools/gopls: support single file mode #34160

stamblerre opened this issue Sep 7, 2019 · 6 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@stamblerre
Copy link
Contributor

stamblerre commented Sep 7, 2019

When the user opens an individual file in their editor, we don't offer any language features. This is because we don't know the module root. Should we have some hack just to support this mode? We could run go env GOMOD and assume that is the root (if that is empty, the directory of the file could be the root).

Note that this will be particularly difficult, as we won't be able to initialize any views until we receive a textDocument/didOpen for the specific file.

@gopherbot gopherbot added this to the Unreleased milestone Sep 7, 2019
@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label Sep 7, 2019
@stamblerre stamblerre added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository. labels Sep 7, 2019
@golang golang deleted a comment from gopherbot Sep 25, 2019
@dpinela
Copy link
Contributor

dpinela commented Oct 6, 2019

I sometimes write small one-off programs for simple jobs or to experiment, for which this would be very handy.

@pekim
Copy link

pekim commented Oct 10, 2019

I think that the following relates to this issue, but apologies if it doesn't.

In my app (a work-in-progress Go editor) I would like to be able to initialise gopls without a workspace. Then add a workspace when a project window is opened. Add another workspace if another project is opened, and so on. And when a project window is closed, remove the workspace folder from the list of folders

Currently I cannot take this approach because if I send an initialize request with no workspace folder I run in to "single file mode not supported".

I intend on working around this for now by making a special case for the first project to open, and initialising gopls at that point with a single workspace. And if another project is opened, add a workspace with a workspace/workspaceFolders request that would include all open workspaces. However making a special case for the first project to open feels a little ugly.

A couple of questions around this.

  1. Is my intended approach reasonable? I've not got as far as implementing it, as I've been working on getting a single workspace working in my editor first.
  2. Will single file support allow me to initialize with no workspaces, and avoid the special case for the first workspace?

@pekim
Copy link

pekim commented Oct 10, 2019

I've done some more reading, and I think that I was getting confused between multiple workspaces and multi-folder workspaces. If I understand correctly, a single language server (gopls) instance only supports a single workspace. And that workspace may support multiple folders. Although currently gopls only supports a workspace with at least one folder.

Launching multiple vscode instances and looking for gopls processes appears to confirm this.

So I'm going to keep it simple in my application, and launch a separate gopls instance for each of my open workspaces.

That means that support for single file mode probably won't be of interest to me.
Sorry for the noise that I've contributed to this issue.

(I think that #34111 looks like what I'm really after.)

@quasilyte
Copy link
Contributor

What about this:

In the case of a single file mode, we expect a gopls user to send initializationOptions.singleFile that gives a path of a currently opened file.

We grab these options from here:
image

If we have an absolute path to a singleFile, we can then create a workspace folder in a same way we do it for params.RootURI:

if singleFile != "" {
	s.pendingFolders = []protocol.WorkspaceFolder{{
		URI:  "file://" + singleFile,
		Name: path.Base(singleFile),
	}}
}

For the VSCode, we would need to update initializationOptions used for a language server creation:
image

I tried it for myself and it seems to be working fine. There are some problems with locations that contain a lot of unrelated folders and files (like ~ or another generic directory), but it's a separate issue in my opinion, since opening the same folder in a normal way would result in the same problem.

If this plan seems right, I can send a CL that implements it. :)

@stamblerre stamblerre modified the milestones: gopls/v1.0.0, gopls/v0.5.0 Mar 12, 2020
@stamblerre
Copy link
Contributor Author

Sorry for not replying earlier, @quasilyte. Thanks for thinking about this, but I'm hoping that we can avoid having an extra setting for this mode. We will be doing work on managing multiple module roots for gopls/v0.5.0, so I'm hoping we can handle this as part of that work.

Also commenting here as a reminder: We should remember to try to handle the case described in #37875 (comment).

@gopherbot
Copy link

Change https://golang.org/cl/240099 mentions this issue: internal/lsp: support opening single files

@stamblerre stamblerre modified the milestones: gopls/v0.6.0, gopls/v0.5.0 Jun 26, 2020
@stamblerre stamblerre modified the milestones: gopls/v0.5.0, gopls/v0.4.4 Jul 9, 2020
@golang golang locked and limited conversation to collaborators Jul 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants