cadence icon indicating copy to clipboard operation
cadence copied to clipboard

[LS] Crashing on referencing non-existing imported variable

Open devbugging opened this issue 2 years ago • 2 comments

Problem

When referencing a variable that doesn't exist in the imported contract the LS crashes with the following stack:

Langauge Server report:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x50 pc=0x10238c724]

goroutine 27 [running]:
github.com/onflow/cadence/runtime/ast.(*CompositeDeclaration).DeclarationMembers(0x0)
	/Users/dapper/go/pkg/mod/github.com/onflow/[email protected]/runtime/ast/composite.go:69 +0x4
github.com/onflow/cadence/languageserver/server.(*Server).convertError.func3(0x140005c1b80, 0x0)
	/Users/dapper/go/pkg/mod/github.com/onflow/cadence/[email protected]/server/server.go:2208 +0x50
github.com/onflow/cadence/languageserver/server.(*Server).maybeAddDeclarationActionsResolver.func1()
	/Users/dapper/go/pkg/mod/github.com/onflow/cadence/[email protected]/server/server.go:2630 +0x328
github.com/onflow/cadence/languageserver/server.(*Server).CodeAction(0x14000204540, {0x103789bd8, 0x14000592c98}, 0x140008539e0)
	/Users/dapper/go/pkg/mod/github.com/onflow/cadence/[email protected]/server/server.go:874 +0x244
github.com/onflow/cadence/languageserver/protocol.(*Server).handleCodeAction(0x140003c35c0, 0x14000015b78)
	/Users/dapper/go/pkg/mod/github.com/onflow/cadence/[email protected]/protocol/methods.go:103 +0xbc
github.com/onflow/cadence/languageserver/jsonrpc2.(*handler).Handle(0x14000592cb8, {0x1037806e0, 0x1400013a008}, 0x140000d5ef0, 0x1400086f4a0)
	/Users/dapper/go/pkg/mod/github.com/onflow/cadence/[email protected]/jsonrpc2/server.go:53 +0x25c
github.com/sourcegraph/jsonrpc2.(*Conn).readMessages(0x140000d5ef0, {0x1037806e0, 0x1400013a008})
	/Users/dapper/go/pkg/mod/github.com/sourcegraph/[email protected]/jsonrpc2.go:522 +0x300
created by github.com/sourcegraph/jsonrpc2.NewConn
	/Users/dapper/go/pkg/mod/github.com/sourcegraph/[email protected]/jsonrpc2.go:334 +0x1f4

Langauge Server Client report:

[Info  - 12:18:52 PM] Connection to server got closed. Server will restart.
[Error - 12:18:52 PM] Request textDocument/codeAction failed.
Error: Connection got disposed.
	at Object.dispose (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:29:4057)
	at Object.dispose (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:34:9518)
	at Af.handleConnectionClosed (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:36:9073)
	at Af.handleConnectionClosed (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:39:12267)
	at n (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:36:8755)
	at hu.invoke (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:2:9583)
	at Rr.fire (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:2:10348)
	at ot (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:3:10845)
	at hu.invoke (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:2:9583)
	at Rr.fire (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:2:10348)
	at Po.fireClose (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:2:12622)
	at Socket.<anonymous> (/Users/dapper/.vscode/extensions/onflow.cadence-0.6.3/out/main.js:2:14174)
	at Socket.emit (events.js:327:22)
	at Pipe.<anonymous> (net.js:673:12)
[Warn  - 12:18:52 PM] Failed to register command. Will retry 9 more times... err: jsonrpc2: code -32601 message: Unhandled method client/registerCapability
[Info  - 12:18:52 PM] parsing file:///Users/dapper/Dev/env/vscode-test/Bar.cdc took 30.209µs
[Info  - 12:18:52 PM] checking file:///Users/dapper/Dev/env/vscode-test/Bar.cdc took 5.403ms
[Info  - 12:18:52 PM] parsing file:///Users/dapper/Dev/env/vscode-test/Foo.cdc took 43.791µs
[Info  - 12:18:52 PM] checking file:///Users/dapper/Dev/env/vscode-test/Foo.cdc took 79.709µs
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x50 pc=0x102d04724]

Steps to Reproduce

Create a contract in vscode, deploy it, now create a transaction that imports that contract and reference a non-existing variable, check the output, it should show the crash.

devbugging avatar Mar 31 '22 16:03 devbugging

As discussed with @turbolent this is happening due to the checker used being the checker for the current program not the checker for the imported program. As a solution we should either:

  • get the correct checker for the imported program, saved to the checker map
  • ignore this altogether for remote contracts (possibly ok for now)

devbugging avatar Apr 05 '22 18:04 devbugging

Put back to backlog after the initial discovery. cc @kerrywei

DOD:

  • [ ] Fix the bug
  • [ ] Deploy the new version

devbugging avatar May 12 '22 08:05 devbugging

@sideninja sorry, I had missed this. Looking into it

turbolent avatar Sep 08 '22 17:09 turbolent

@sideninja as discussed in the call, only proposing the action for the current location, i.e. when the getter returns a type, is the best solution. in the future we could also extend it to support proposing a code action for the declaration's location

turbolent avatar Sep 08 '22 17:09 turbolent

@turbolent that would be nice to add. Also I'm keen to find out why it is null? is that because the incorrect checker is being used? but if that's true why I couldn't reproduce it in a test? I did remote contract import and tried to access members it didn't have, it came to that line but it didn't return null. Ofc I will add sentry to report such case, maybe there's something new to be learned. This is really raw draft test (so don't judge is just suppose to quickly test not yet to be included), https://github.com/onflow/cadence/blob/ls/bugfix/reported-bugfixes/languageserver/server/server_test.go#L14 with it I supposed to get that null value there but didn't. I also tried it out manually to use deployed contract in vscode but it didn't crash.

The diagnostic of the test ran is:

[{{{3 10} {3 13}} 1 <nil> <nil>  value of type `Foo` has no member `bar`. unknown member [] [] <nil>}]

devbugging avatar Sep 08 '22 17:09 devbugging

@sideninja OK to close, given #1950 should have fixed this?

turbolent avatar Sep 21 '22 16:09 turbolent