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: confusion over case of file name on macOS #61293

Closed
TaeukSun opened this issue Jul 11, 2023 · 10 comments
Closed

x/tools/gopls: confusion over case of file name on macOS #61293

TaeukSun opened this issue Jul 11, 2023 · 10 comments
Labels
gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@TaeukSun
Copy link

$ go version
go version go1.18 darwin/amd64

What did you expect to see?

go/packages.Load for constConnectionType.go

What did you see instead?

Problem:
This file is within module ".", which is not included in your workspace.
To fix this problem, you can add a go.work file that uses this directory.
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.go list

gopls Print
[Error - 오후 5:06:20] Request textDocument/codeAction failed.
Message: no package metadata for file file:///Users/tu/g***/g***/models/g***/ConstConnectionType.go
Code: 0

First case is Wrong.. My file name start with 'c' not 'C'

if i change filename, it works.
ex)
constConnectionType.go -> constConnectionType_1.go works
but constConnectionType.go -> ConstConnectionType.go not works

anyone know about this issue?

@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label Jul 11, 2023
@Erick-254
Copy link

I recommend following the naming conventions for Go files. Use lowercase letters and, if needed, separate words with underscores. For example, you can rename your file to const_connection_type.go or something similar.
Also ensure that the file is located within your Go module's directory structure, as indicated by the error message you received. If the file is not within the module, you may need to move it to the correct location.

@TaeukSun
Copy link
Author

TaeukSun commented Jul 12, 2023

thanks for your comment.
i'm sure that file is located in go module's directory structure, and some my colleague don't have a experience for same issue on same project.... and my different computer have same issue... i don't know what is problem ...

@adonovan
Copy link
Member

Could you give more detail on what exactly you did? Did you create the file through your editor (which one?) or some other way? What did you do in your editor that triggered the codeAction request for the wrong name? Most editors will refuse to load a file with the wrong name: they will correct the spelling to match what appears in the directory.

(Some macOS file systems are configured to use case-sensitive matching, like Linux. I assume you haven't enabled this setting, but for completeness I should ask.)

If you're comfortable, please share a more complete log of the editor-to-gopls session. Thanks.

@adonovan adonovan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 13, 2023
@adonovan adonovan changed the title gopls not work.... x/tools/gopls: confusion over case of file name on macOS Jul 13, 2023
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jul 13, 2023
@gopherbot gopherbot added this to the Unreleased milestone Jul 13, 2023
@TaeukSun
Copy link
Author

TaeukSun commented Jul 14, 2023

I create file and modify file's name in vscode(Mac OS). Maybe when I modify file's name, this problem exist.
ex) ConstConnectionType.go -> constConnectionType.go
but this problem is not always happened.
when this problem is happened, 'go run' or 'go build' work well

this is another example of this problem (I modified file name from device_HeartBeat.go to device_heartbeat.go)

[Info - 오전 11:25:26] 2023/07/14 11:25:26 gopathwalk: scanned /Users/tu/.asdf/installs/golang/1.18/packages/pkg/mod in 12.689434ms

[Error - 오전 11:48:00] Request textDocument/codeAction failed.
Message: no package metadata for file file:///Users/tu/xxx/resource/device_HeartBeat.go
Code: 0
[Info - 오전 11:48:00] 2023/07/14 11:48:00 fixImports(filename="/Users/tu/xxx/resource/device_HeartBeat.go"), abs="/Users/tu/xxx/resource/device_HeartBeat.go", srcDir="/Users/tu/xxx/resource" ...

[Error - 오전 11:48:00] Request textDocument/inlayHint failed.
Message: getting file for InlayHint: no package metadata for file file:///Users/tu/xxx/resource/device_HeartBeat.go
Code: 0
[Error - 오전 11:48:00] Request textDocument/semanticTokens/range failed.
Message: semantictokens are disabled
Code: 0
[Error - 오전 11:48:00] Request textDocument/semanticTokens/full failed.
Message: semantictokens are disabled
Code: 0

@TaeukSun
Copy link
Author

TaeukSun commented Jul 14, 2023

if you need more information, please reply. i really wanna solve this problem.... @adonovan

@hyangah
Copy link
Contributor

hyangah commented Jul 14, 2023

Here is a repro:

  1. Start with main.go, open from the directory, and open the main.go file in vscode.
[Trace - 8:07:56 AM] Sending notification 'textDocument/didOpen'.
Params: {"textDocument":{"uri":"file:///Users/hakim/repro/main.go","languageId":"go","version":1,"text":"package main\n\nfunc main() {}\n"}}

[Trace - 8:07:56 AM] Sending request 'textDocument/codeAction - (1)'.
Params: {"textDocument":{"uri":"file:///Users/hakim/repro/main.go"},"range":{"start":{"line":0,"character":9},"end":{"line":0,"character":9}},"context":{"diagnostics":[],"triggerKind":2}}
...
[Trace - 8:07:57 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2023/07/14 08:07:57 go/packages.Load #1\n\tsnapshot=0\n\tdirectory=file:///Users/hakim/repro\n\tquery=[/Users/hakim/repro/... builtin]\n\tpackages=2\n"}

So far ok.

  1. Rename the file main.go to Main.go using vscode File Explorer UI.
[Trace - 8:08:08 AM] Sending notification 'workspace/didChangeWatchedFiles'.
Params: {"changes":[{"uri":"file:///Users/hakim/repro/Main.go","type":1},{"uri":"file:///Users/hakim/repro/main.go","type":3}]}

[Trace - 8:08:08 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2023/07/14 08:08:08 go/packages.Load #2\n\tsnapshot=2\n\tdirectory=file:///Users/hakim/repro\n\tquery=[file=/Users/hakim/repro/main.go]\n\tpackages=1\n"}

[Trace - 8:08:08 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2023/07/14 08:08:08 go/packages.Load #2\n\tsnapshot=2\n\tdirectory=file:///Users/hakim/repro\n\tpackage=\"foo\"\n\tfiles=[/Users/hakim/repro/Main.go /Users/hakim/repro/main.go]\n"}

[Trace - 8:08:08 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2023/07/14 08:08:08 go/packages.Load #2: updating metadata for 1 packages\n"}

[Trace - 8:08:08 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///Users/hakim/repro/main.go","version":1,"diagnostics":[{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}},"severity":1,"source":"go list","message":"case-insensitive file name collision: \"Main.go\" and \"main.go\""},{"range":{"start":{"line":2,"character":5},"end":{"line":2,"character":9}},"severity":1,"code":"DuplicateDecl","codeDescription":{"href":"https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#DuplicateDecl"},"source":"compiler","message":"main redeclared in this block","relatedInformation":[{"location":{"uri":"file:///Users/hakim/repro/Main.go","range":{"start":{"line":2,"character":5},"end":{"line":2,"character":9}}},"message":"other declaration of main"}]}]}

[Trace - 8:08:08 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///Users/hakim/repro/Main.go","diagnostics":[{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}},"severity":1,"source":"go list","message":"case-insensitive file name collision: \"Main.go\" and \"main.go\""},{"range":{"start":{"line":2,"character":5},"end":{"line":2,"character":9}},"severity":1,"code":"DuplicateDecl","codeDescription":{"href":"https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#DuplicateDecl"},"source":"compiler","message":"main redeclared in this block (see details)","relatedInformation":[{"location":{"uri":"file:///Users/hakim/repro/main.go","range":{"start":{"line":2,"character":5},"end":{"line":2,"character":9}}},"message":"main redeclared in this block"},{"location":{"uri":"file:///Users/hakim/repro/Main.go","range":{"start":{"line":2,"character":5},"end":{"line":2,"character":9}}},"message":"other declaration of main (this error)"}]}]}

See we get the duplicate file on case-insensitive fs error.
VS Code changed all visible UI components to the new name (Main.go),
but LSP client (= VSCode) keeps using the old names.

[Trace - 8:08:08 AM] Sending request 'textDocument/codeAction - (17)'.
Params: {"textDocument":{"uri":"file:///Users/hakim/repro/main.go"},"range":{"start":{"line":0,"character":9},"end":{"line":0,"character":9}},"context":{"diagnostics":[],"triggerKind":2}}

[Trace - 8:08:08 AM] Received response 'textDocument/codeAction - (17)' in 4ms.
No result returned.
...
  1. Restart the language server ("Go: Restart Language Server"). Leave the Main.go file open.

[Trace - 8:08:18 AM] Sending request 'shutdown - (20)'.
[Trace - 8:08:18 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2023/07/14 08:08:18 Shutdown session\n\tshutdown_session=1\n"}

[Trace - 8:08:18 AM] Received response 'shutdown - (20)' in 3ms.
No result returned.

[Trace - 8:08:18 AM] Sending notification 'exit'.
No parameters provided.

[Trace - 8:08:18 AM] Sending request 'initialize - (0)'.
...


[Trace - 8:08:18 AM] Sending notification 'textDocument/didOpen'.
Params: {"textDocument":{"uri":"file:///Users/hakim/repro/main.go","languageId":"go","version":1,"text":"package main\n\nfunc main() {}\n"}}

[Trace - 8:08:18 AM] Sending request 'textDocument/documentSymbol - (1)'.
Params: {"textDocument":{"uri":"file:///Users/hakim/repro/main.go"}}

...

[Trace - 8:08:19 AM] Received response 'textDocument/semanticTokens/full - (8)' in 204ms. Request failed: semantictokens are disabled (0).
[Trace - 8:08:20 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///Users/hakim/repro/main.go","version":1,"diagnostics":[{"range":{"start":{"line":0,"character":8},"end":{"line":0,"character":12}},"severity":2,"source":"go list","message":"This file is within module \".\", which is not included in your workspace.\nTo fix this problem, you can add a go.work file that uses this directory.\nSee the documentation for more information on setting up your workspace:\nhttps://github.com/golang/tools/blob/master/gopls/doc/workspace.md."}]}

See VS Code still sends requests withmain.go (lower case), even though its UI elements show Main.go.

After this state, even when we rename Main.go to main.go, the diagnostic message doesn't go away.

The only way to recover is to reload the VS Code (Developer: Reload Window command from the command palettes) so VS Code's internal state is cleared.

This looks like a VS Code's (or VS Code lsp client's) bug IMO.

@hyangah
Copy link
Contributor

hyangah commented Jul 14, 2023

Ok, a bit of digging in vscode issue tracker led me to conclude, this is a known bug in vscode. As discussed in
microsoft/language-server-protocol#1263 VS Code has many bugs related to file name casing handling. My personal recommendation is to stay with lower cases only and avoid renaming involving case changes. If you encounter this issue, reload the vscode window.

Some more contexts

@TaeukSun
Copy link
Author

TaeukSun commented Jul 17, 2023

Thank you for interesting in this @hyangah
I understand your explanation. But Your Solution(Reload Window Command) dose not work to me ...

[Info - 오후 12:06:45] 2023/07/17 12:06:45 module github.com/stretchr/objx has not been downloaded and will be ignored

[Info - 오후 12:06:45] 2023/07/17 12:06:45 module gopkg.in/check.v1 has not been downloaded and will be ignored

[Info - 오후 12:06:45] 2023/07/17 12:06:45 fixImports(filename="/Users/itsme/xx/resource/device_HeartBeat.go"), abs="/Users/itsme/xx/resource/device_HeartBeat.go", srcDir="/Users/itsme/xx/resource" ...

[Error - 오후 12:06:45] Request textDocument/codeAction failed.
Message: context canceled
Code: 0
[Error - 오후 12:06:45] Request textDocument/inlayHint failed.
Message: getting file for InlayHint: no package metadata for file file:///Users/itsme/xx/resource/device_HeartBeat.go
Code: 0
[Error - 오후 12:06:45] Request textDocument/semanticTokens/range failed.
Message: semantictokens are disabled
Code: 0
[Error - 오후 12:06:45] Request textDocument/semanticTokens/full failed.
Message: semantictokens are disabled
Code: 0
[Error - 오후 12:07:09] Request textDocument/inlayHint failed.
Message: getting file for InlayHint: no package metadata for file file:///Users/itsme/xx/resource/device_HeartBeat.go
Code: 0
[Error - 오후 12:07:09] Request textDocument/semanticTokens/range failed.
Message: semantictokens are disabled
Code: 0

After reload , There is same problem...

This file is within module ".", which is not included in your workspace.
To fix this problem, you can add a go.work file that uses this directory.
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.

Thank you for your comment. if i get more information or solution working to me, i will comment again...

@hyangah
Copy link
Contributor

hyangah commented Jul 27, 2023

@TaeukSun Unfortunately, the problem is inside vscode. I am afraid building any workaround in the gopls side will add significant complexity and may cause other kinds of bugs. I think it's best to avoid renaming files and directories in this way on mac.

EDIT: I think you will need to close the editor tab of the renamed file before reloading, and reload the vscode window, to make this problem go away.

@golang/tools-team Independent of this issue, I think the error message needs improvement.

@hyangah hyangah removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 27, 2023
@TaeukSun
Copy link
Author

@hyangah Thank you for your comment. and I understand that this problem is dependent on vscode.

And this work for me. thank you!!!

EDIT: I think you will need to close the editor tab of the renamed file before reloading, and reload the vscode window, to
make this problem go away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. 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