KreativJos / csharpextensions

C# Extensions for Visual Studio Code
MIT License
73 stars 35 forks source link

Error thrown - Illegal value for `line` #56

Closed KreativJos closed 1 year ago

KreativJos commented 3 years ago

Log output:

[2021-10-27 01:08:48.597] [exthost] [error] Error: Illegal value for `line`
    at a._lineAt (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:88:1174)
    at Object.lineAt (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:88:95)
    at e.default.findConstructorBodyStart (%USERPROFILE%\.vscode\extensions\kreativ-software.csharpextensions-1.5.2\dist\extension.js:2:112512)
    at e.default.getInitializeFromCtorCommand (%USERPROFILE%\.vscode\extensions\kreativ-software.csharpextensions-1.5.2\dist\extension.js:2:112187)
    at o (%USERPROFILE%\.vscode\extensions\kreativ-software.csharpextensions-1.5.2\dist\extension.js:2:107606)
    at e.default.provideCodeActions (%USERPROFILE%\.vscode\extensions\kreativ-software.csharpextensions-1.5.2\dist\extension.js:2:107678)
    at T.provideCodeActions (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:94:172424)
    at %USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:94:196984
    at xe._withAdapter (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:94:191024)
    at xe.$provideCodeActions (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:94:196960)
    at i._doInvokeHandler (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:98:13802)
    at i._invokeHandler (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:98:13486)
    at i._receiveRequest (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:98:12097)
    at i._receiveOneMessage (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:98:10826)
    at %USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:98:8922
    at a.fire (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:66:1712)
    at l.fire (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:74:15990)
    at %USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:113:30514
    at a.fire (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:66:1712)
    at l.fire (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:74:15990)
    at u._receiveMessage (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:74:21260)
    at %USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:74:18138
    at a.fire (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:66:1712)
    at w.acceptChunk (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:74:13351)
    at %USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:74:12699
    at Socket.v (%USERPROFILE%\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:113:13465)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at Pipe.onStreamRead (internal/stream_base_commons.js:188:23)

Caused somewhere in codeActionProvider (a new Position call)

bard83 commented 1 year ago

This error depends on some magic number which is used to define loop limit. Unfortunately it exceeds the file's lines and raises this error. Let's say that it can be easily fixed, but it would be interesting to understand the nature of the number.

 private findConstructorBodyStart(document: TextDocument, position: Position): Position | null {
        for (let lineNo = position.line; lineNo < position.line + 5; lineNo++) {
            const line = document.lineAt(lineNo);
...

it seems to me that 5 (the magic number) it has been used with some strong assumptions: the class gets generated from csharextension template, it does not use file scoped namespace and it does not expect body expression constructor.

Below a working example:

namespace MyTest.Workspace
{
    public class NewClass
    {
        public NewClass(int field)
        {
            this.Field = field;

        }

        public int Field { get; }
    }
}

Here one which throws the above error:

namespace MyTest.Workspace;

public class NewClass
{
    public NewClass(int field)
    {
        this.Field = field;

    }

    public int Field { get; }
}

things get even worse using expression body constructor.

namespace MyTest.Workspace;

public class NewClass
{
    public NewClass(int field)
    =>    this.Field = field;

    public int Field { get; }
}

The solution will be to replace 5 with the following instruction:

const limit = min((position.line + (document.lineCount /2)), document.linesCount);

This also means that the code action needs to support body expression constructor.