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

Unable to start language server in VS Code workspace #627

Closed
frittsy opened this issue Apr 9, 2024 · 8 comments · Fixed by #628
Closed

Unable to start language server in VS Code workspace #627

frittsy opened this issue Apr 9, 2024 · 8 comments · Fixed by #628
Labels
bug Something isn't working

Comments

@frittsy
Copy link

frittsy commented Apr 9, 2024

Platform: macOS 14.4.1, M1 arm64
OPA version: 0.63.0
Regal version: 0.20.0
Client: VS Code: 1.88.0

I haven't been able to get the OPA extension for VS Code to work as intended and can't seem to find any issue in GitHub or Slack like this one. I'm not 100% sure if this is an extension issue or a Regal language server issue, but I'm leaning towards the latter so I'm opening the issue here to start.

Here is my full output from regal-ls upon opening a .rego file:

running in workspace mode
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x28 pc=0x100c270c0]

goroutine 33 [running]:
github.com/styrainc/regal/internal/lsp.(*LanguageServer).loadWorkspaceContents.func1({0x14000040be7?, 0x3b?}, {0x0?, 0x0?}, {0x1013600f8, 0x140002d7380})
	/home/runner/work/regal/regal/internal/lsp/server.go:628 +0x40
path/filepath.WalkDir({0x14000040be7, 0x3b}, 0x1400004bc68)
	/home/runner/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.linux-amd64/src/path/filepath/path.go:531 +0xa4
github.com/styrainc/regal/internal/lsp.(*LanguageServer).loadWorkspaceContents(0x1400033e5b0)
	/home/runner/work/regal/regal/internal/lsp/server.go:626 +0x5c
github.com/styrainc/regal/internal/lsp.(*LanguageServer).handleInitialize(0x1400033e5b0, {0x1400027caa0?, 0x1721?}, 0x101200880?, 0x1400007c8a0)
	/home/runner/work/regal/regal/internal/lsp/server.go:606 +0x354
github.com/styrainc/regal/internal/lsp.(*LanguageServer).Handle(0x1400033e5b0, {0x101366e30, 0x1400031fb80}, 0x14000320870, 0x1400007c8a0)
	/home/runner/work/regal/regal/internal/lsp/server.go:90 +0x1a0
github.com/sourcegraph/jsonrpc2.(*HandlerWithErrorConfigurer).Handle(0x14000318b00, {0x101366e30, 0x1400031fb80}, 0x14000320870, 0x1400007c8a0)
	/home/runner/go/pkg/mod/github.com/sourcegraph/jsonrpc2@v0.2.0/handler_with_error.go:21 +0x4c
github.com/sourcegraph/jsonrpc2.(*Conn).readMessages(0x14000320870, {0x101366e30, 0x1400031fb80})
	/home/runner/go/pkg/mod/github.com/sourcegraph/jsonrpc2@v0.2.0/conn.go:205 +0x2a4
created by github.com/sourcegraph/jsonrpc2.NewConn in goroutine 1
	/home/runner/go/pkg/mod/github.com/sourcegraph/jsonrpc2@v0.2.0/conn.go:62 +0x1d0
[Error - 10:10:19 PM] Server initialization failed.
  Message: Pending response rejected since connection got disposed
  Code: -32097 
[Error - 10:10:19 PM] Connection to server got closed. Server will not be restarted.
[Error - 10:10:19 PM] Regal LSP client client: couldn't create connection to server.
  Message: Pending response rejected since connection got disposed
  Code: -32097 
[Error - 10:10:19 PM] Server process exited with code 2.

I tried a few different settings with no success, but this is what I currently have configured in VS Code:

  "opa.dependency_paths.opa": "/opt/homebrew/bin/opa",
  "opa.dependency_paths.regal": "/opt/homebrew/bin/regal",
  "opa.languageServers": [
    "regal"
  ],

Please let me know if there's any additional information that I can provide or debug with.

@anderseknert
Copy link
Member

Thanks for an excellent bug report! Looks like there's some directory in your workspace that Regal isn't permitted to read (or whatever other reasons there might be for WalkDir to fail on that), and in the error handling we're referencing d.Name() without checking if d is set first.

err := filepath.WalkDir(workspaceRootPath, func(path string, d os.DirEntry, err error) error {
		if err != nil {
			return fmt.Errorf("failed to walk workspace dir %q: %w", d.Name(), err)
		}
               // ...
}

Classic rookie mistake, most likely signed yours truly. I'll have it fixed later today and will send you a link to an update binary for you to test with.

@anderseknert anderseknert added the bug Something isn't working label Apr 9, 2024
anderseknert added a commit that referenced this issue Apr 9, 2024
Fixes #627

Signed-off-by: Anders Eknert <anders@styra.com>
@anderseknert
Copy link
Member

Fix is out and v0.20.1 just published. Should be available via brew sometime soon. Thanks @frittsy, and let me know how that works.

@frittsy
Copy link
Author

frittsy commented Apr 9, 2024

@anderseknert thanks for the quick fix! I'm able to extract the real error now, which I believe is still a bug in its own right?

running in workspace mode
[Error - 12:04:21 PM] Server initialization failed.
  Message: failed to load workspace contents: failed to walk workspace dir "/Users/frittsy/Code/IT%20Operations/infrastructure-policies": failed to walk workspace dir "/Users/frittsy/Code/IT%20Operations/infrastructure-policies": lstat /Users/frittsy/Code/IT%20Operations/infrastructure-policies: no such file or directory
  Code: 0 
[Error - 12:04:21 PM] Regal LSP client client: couldn't create connection to server.
  Message: failed to load workspace contents: failed to walk workspace dir "/Users/frittsy/Code/IT%20Operations/infrastructure-policies": failed to walk workspace dir "/Users/frittsy/Code/IT%20Operations/infrastructure-policies": lstat /Users/frittsy/Code/IT%20Operations/infrastructure-policies: no such file or directory
  Code: 0 
[Error - 12:04:23 PM] Server process exited with signal SIGKILL.
[Error - 12:04:23 PM] Connection to server got closed. Server will not be restarted.

It appears to be URL encoding the space character in the folder name. I tried moving the repository into a different directory without a space in it and the language server started successfully 🎉

@anderseknert
Copy link
Member

Hmm yeah, that's a bug for sure. Although it's not as obvious what the cause is, or rather why one go function would encode the path in a way that another go function can't understand 😅 Or I'm just missing something obvious. Anyway, that'll be trivial to add a test case for, so I should be able to figure something out. I'll look into it as soon as I can. Happy to hear you figured it out and provided a workaround! Should help others who might find their way here if they encounter the same.

@charlieegan3
Copy link
Member

VSCode uses URI-encoded values for file names, I think our logic to process them is only handling the file:// prefixes at the moment. 🤦 I wrote that part... I'll get an issue open.

@charlieegan3
Copy link
Member

#629

@anderseknert
Copy link
Member

Ah, at least I'm off the hook then! 👋 😆

@charlieegan3
Copy link
Member

See #631 for the follow on fix for this

srenatus pushed a commit to srenatus/regal that referenced this issue Oct 1, 2024
Fixes StyraInc#627

Signed-off-by: Anders Eknert <anders@styra.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants