|
| 1 | +# Visual Studio Code Extension |
| 2 | + |
| 3 | +!!! danger "Preview" |
| 4 | + |
| 5 | + The SQLMesh Visual Studio Code extension is in preview and undergoing active development. You may encounter bugs or API incompatibilities with the SQLMesh version you are running. |
| 6 | + |
| 7 | + We encourage you to try the extension and [create Github issues](https://github.com/tobikodata/sqlmesh/issues) for any problems you encounter. |
| 8 | + |
| 9 | +In this guide, you'll set up the SQLMesh extension in the Visual Studio Code IDE software (which we refer to as "VSCode"). |
| 10 | + |
| 11 | +We'll show you the capabilities of the extension and how to troubleshoot common issues. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +### VSCode extension |
| 16 | + |
| 17 | +Install the extension through the official Visual Studio [marketplace website](https://marketplace.visualstudio.com/items?itemName=tobikodata.sqlmesh) or by searching for `SQLMesh` in the VSCode "Extensions" tab. |
| 18 | + |
| 19 | +Learn more about installing VSCode extensions in the [official documentation](https://code.visualstudio.com/docs/configure/extensions/extension-marketplace#_install-an-extension). |
| 20 | + |
| 21 | +### Python setup |
| 22 | + |
| 23 | +While installing the extension is simple, setting up and configuring a Python environment in VSCode is a bit more involved. |
| 24 | + |
| 25 | +We recommend using a dedicated *Python virtual environment* to install SQLMesh. Visit the [Python documentation](https://docs.python.org/3/library/venv.html) for more information about virtual environments. |
| 26 | + |
| 27 | +We describe the steps to create and activate a virtual environment below, but additional information is available on the [SQLMesh installation page](installation.md). |
| 28 | + |
| 29 | +We first install the SQLMesh library, which is required by the extension. |
| 30 | + |
| 31 | +Open a terminal instance in your SQLMesh project's directory and issue this command to create a virtual environment in the `.venv` directory: |
| 32 | + |
| 33 | +```bash |
| 34 | +python -m venv .venv |
| 35 | +``` |
| 36 | + |
| 37 | +Next, activate the virtual environment: |
| 38 | + |
| 39 | +```bash |
| 40 | +source .venv/bin/activate |
| 41 | +``` |
| 42 | + |
| 43 | +#### Open-source SQLMesh |
| 44 | + |
| 45 | +If you are using open-source SQLMesh, install SQLMesh with the `lsp` extra that enables the VSCode extension (learn more about SQLMesh extras [here](installation.md#install-extras)): |
| 46 | + |
| 47 | +```bash |
| 48 | +pip install 'sqlmesh[lsp]' |
| 49 | +``` |
| 50 | + |
| 51 | +#### Tobiko Cloud |
| 52 | + |
| 53 | +If you are using Tobiko Cloud, the `tcloud` library will install SQLMesh for you. |
| 54 | + |
| 55 | +First, follow the [Python setup](#python-setup) steps above to create and activate a Python environment. Next, install `tcloud`: |
| 56 | + |
| 57 | +```bash |
| 58 | +pip install tcloud |
| 59 | +``` |
| 60 | + |
| 61 | +Finally, add the `lsp` extra to your `tcloud.yml` configuration file, as described [here](../cloud/tcloud_getting_started.md#connect-tobiko-cloud-to-data-warehouse). |
| 62 | + |
| 63 | +### VSCode Python interpreter |
| 64 | + |
| 65 | +A Python virtual environment contains its own copy of Python (the "Python interpreter"). We need to make sure VSCode is using your virtual environment's interpreter rather than a system-wide or other interpreter that does not have access to the SQLMesh library we just installed. |
| 66 | + |
| 67 | +Confirm that VSCode is using the correct interpreter by going to the [command palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) and clicking `Python: Select Interpreter`. Select the Python executable that's in the virtual environment's directory `.venv`. |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +Once that's done, validate that the everything is working correctly by checking the `sqlmesh` channel in the [output panel](https://code.visualstudio.com/docs/getstarted/userinterface#_output-panel). It displays the Python interpreter path and details of your SQLMesh installation: |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## Features |
| 76 | + |
| 77 | +SQLMesh's VSCode extension makes it easy to edit and understand your SQLMesh project with these features: |
| 78 | + |
| 79 | +- Lineage |
| 80 | + - Interactive view of model lineage |
| 81 | +- Editor |
| 82 | + - Auto-completion for model names and SQLMesh keywords |
| 83 | + - Model summaries when hovering over model references |
| 84 | + - Links to open model files from model references |
| 85 | + - Inline SQLMesh linter diagnostics |
| 86 | +- VSCode commands |
| 87 | + - Format SQLMesh project files |
| 88 | + - Sign in/out of Tobiko Cloud (Tobiko Cloud users only) |
| 89 | + |
| 90 | +### Lineage |
| 91 | + |
| 92 | +The extension adds a lineage view to SQLMesh models. To view the lineage of a model, go to the `Lineage` tab in the panel: |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +### Editor |
| 97 | + |
| 98 | +The SQLMesh VSCode extension includes several features that make editing SQLMesh models easier and quicker: |
| 99 | + |
| 100 | +**Completion** |
| 101 | + |
| 102 | +See auto-completion suggestions when writing SQL models, keywords, or model names. |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +**Go to definition and hover information** |
| 107 | + |
| 108 | +Hovering over a model name shows a tooltip with the model description. Clicking the model name opens the file containing the model definition. |
| 109 | + |
| 110 | +**Diagnostics** |
| 111 | + |
| 112 | +If you have the [SQLMesh linter](../guides/linter.md) enabled, issues are reported directly in your editor. This works for both SQLMesh's built-in linter rules and custom linter rules. |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +**Formatting** |
| 117 | + |
| 118 | +SQLMesh's model formatting tool is integrated directly into the editor, so it's easy to format models consistently. |
| 119 | + |
| 120 | +### Commands |
| 121 | + |
| 122 | +The SQLMesh VSCode extension provides the following commands in the VSCode command palette: |
| 123 | + |
| 124 | +- `Format SQLMesh project` |
| 125 | +- `Sign in to Tobiko Cloud` (Tobiko Cloud users only) |
| 126 | +- `Sign out of Tobiko Cloud` (Tobiko Cloud users only) |
| 127 | + |
| 128 | +## Troubleshooting |
| 129 | + |
| 130 | +### Python environment woes |
| 131 | + |
| 132 | +The most common problem is the extension not using the correct Python interpreter. |
| 133 | + |
| 134 | +Follow the [setup process described above](#vscode-python-interpreter) to ensure that the extension is using the correct Python interpreter. |
| 135 | + |
| 136 | +If you have checked the VSCode `sqlmesh` output channel and the extension is still not using the correct Python interpreter, please raise an issue [here](https://github.com/tobikodata/sqlmesh/issues). |
| 137 | + |
| 138 | +### Missing Python dependencies |
| 139 | + |
| 140 | +When installing SQLMesh, some dependencies required by the VSCode extension are not installed unless you specify the `lsp` "extra". |
| 141 | + |
| 142 | +If you are using open-source SQLMesh, install the `lsp` extra by running this command in your terminal: |
| 143 | + |
| 144 | +```bash |
| 145 | +pip install 'sqlmesh[lsp]' |
| 146 | +``` |
| 147 | + |
| 148 | +If you are using Tobiko Cloud, make sure `lsp` is included in the list of extras specified in the [`tcloud.yaml` configuration file](../cloud/tcloud_getting_started.md#connect-tobiko-cloud-to-data-warehouse). |
| 149 | + |
| 150 | +### SQLMesh compatibility |
| 151 | + |
| 152 | +While the SQLMesh VSCode extension is in preview and the APIs to the underlying SQLMesh version are not stable, we do not guarantee compatibility between the extension and the SQLMesh version you are using. |
| 153 | + |
| 154 | +If you encounter a problem, please raise an issue [here](https://github.com/tobikodata/sqlmesh/issues). |
0 commit comments