continuedev / continue

⏩ Continue is the leading open-source AI code assistant. You can connect any models and any context to build custom autocomplete and chat experiences inside VS Code and JetBrains
https://docs.continue.dev/
Apache License 2.0
18.99k stars 1.62k forks source link

Bug: Negative line numbers in showLines function causing errors #2241

Open fawwazanvilen opened 1 month ago

fawwazanvilen commented 1 month ago

Before submitting your bug report

Relevant environment info

- OS: Microsoft Windows 11 Pro 10.0.22631 Build 22631
- Continue: v0.9.207 (pre-release) & v0.8.51
- IDE: VSCode 1.93.0
- config.json:

{
  "models": [
    {
      "title": "OpenRouter Anthropic Claude-3.5-Sonnet",
      "provider": "openrouter",
      "model": "anthropic/claude-3.5-sonnet",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": ""
    },
    {
      "title": "OpenRouter DeepSeek-Coder-V2",
      "provider": "openrouter",
      "model": "deepseek/deepseek-coder",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": "
    },
    {
      "title": "OpenRouter DeepSeek-V2.5",
      "provider": "openrouter",
      "model": "deepseek/deepseek-chat",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": ""
    },
    {
      "title": "OpenRouter Mistral Codestral Mamba",
      "provider": "openrouter",
      "model": "mistralai/codestral-mamba",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": ""
    },
    {
      "title": "OpenRouter OpenAI GPT 4o mini",
      "provider": "openrouter",
      "model": "openai/gpt-4o-mini",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": ""
    },
    {
      "title": "OpenRouter Nous Hermes 3 70B Instruct",
      "provider": "openrouter",
      "model": "nousresearch/hermes-3-llama-3.1-70b",
      "apiBase": "https://openrouter.ai/api/v1",
      "apiKey": ""
    }
  ],
  "customCommands": [
    {
      "name": "test",
      "prompt": "{{{ input }}}\n\nWrite a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.",
      "description": "Write unit tests for highlighted code"
    }
  ],
  "tabAutocompleteModel": {
    // "title": "Starcoder2 3b",
    // "provider": "ollama",
    // "model": "starcoder2:3b"
    // "title": "OpenRouter Codestral Mamba",
    // "provider": "openrouter",
    // "model": "mistralai/codestral-mamba",
    // "apiBase": "https://openrouter.ai/api/v1",
    // "apiKey": ""
  },
  "contextProviders": [
    {
      "name": "code",
      "params": {}
    },
    {
      "name": "docs",
      "params": {}
    },
    {
      "name": "diff",
      "params": {}
    },
    {
      "name": "terminal",
      "params": {}
    },
    {
      "name": "problems",
      "params": {}
    },
    {
      "name": "folder",
      "params": {}
    },
    {
      "name": "codebase",
      "params": {}
    }
  ],
  "slashCommands": [
    {
      "name": "edit",
      "description": "Edit selected code"
    },
    {
      "name": "comment",
      "description": "Write comments for the selected code"
    },
    {
      "name": "share",
      "description": "Export the current chat session to markdown"
    },
    {
      "name": "cmd",
      "description": "Generate a shell command"
    },
    {
      "name": "commit",
      "description": "Generate a git commit message"
    }
  ],
  "embeddingsProvider": {
    "provider": "free-trial"
  },
  "reranker": {
    "name": "free-trial"
  }
}

Description

Hello Continue.dev team!

I've encountered a bug in the VSCode extension where negative line numbers are causing errors. Here are the details:

Description: The showLines function is throwing an error when it receives negative line numbers. This is causing problems when trying to display certain code snippets.

Error Message: Error: Illegal argument: line must be non-negative

Proposed Solution: We could modify the showLines function to handle negative line numbers gracefully. Here's a possible approach:

  1. Ensure line numbers are non-negative by using Math.max(0, lineNumber).
  2. If the end line is less than the start line, swap them to ensure a valid range.
async showLines(
  filepath: string,
  startLine: number,
  endLine: number,
) {
  // Ensure line numbers are non-negative
  startLine = Math.max(0, startLine);
  endLine = Math.max(0, endLine);

  // If endLine is less than startLine, swap them
  if (endLine < startLine) {
    [startLine, endLine] = [endLine, startLine];
  }

  // existing implementation
}

Affected Files: The main file that needs to be updated is likely extensions/vscode/src/ideProtocol.ts, but we may need to make similar changes in other files that call showLines. The CodeSnippetPreview.tsx file seems particularly suspicious because it's doing some string parsing and integer conversion, which could potentially lead to negative numbers.

ideMessenger.ide.showLines(
  props.item.description,
  parseInt(lines[0]) - 1,
  parseInt(lines[1]) - 1,
);

Additional Context: This fix will improve the robustness of the extension and prevent crashes due to unexpected negative line numbers.

I'd be happy to work on a PR to fix this issue if you think this approach sounds good. Let me know if you need any additional information or have any questions!

Thanks for your time and for maintaining this awesome project! 😊

To reproduce

  1. Start a chat with a model in Continue.dev.
  2. In the chat, Ctrl+click (or Cmd+click on macOS) on the directory reference in the chat from the model.
  3. The extension attempts to show the contents of the directory, but encounters an error due to negative line numbers.

Log output

[Extension Host] Error handling webview message: {
  "msg": {
    "messageId": "ecd06720-50a3-4b04-807e-f13a43fc154d",
    "messageType": "showLines",
    "data": {
      "filepath": "\\home\\fawwaz\\Codes\\faw-site\\themes\\faw-monospace\\layouts\\partials\\head\\css.html",
      "startLine": -1,
      "endLine": 0
    }
  }
}

Error: Illegal argument: line must be non-negative
Patrick-Erichsen commented 1 month ago

Hey @fawwazanvilen , thanks for the great writeup here! Would definitely appreciate a PR to address this issue.

I think that the Math.max(0, lineNumber) approach seems solid. If you're able to write a quick unit test for this work after your updates that would be helpful as well.