Skip to content

Latest commit

 

History

History
247 lines (181 loc) · 8.66 KB

getting-started.md

File metadata and controls

247 lines (181 loc) · 8.66 KB

Getting Started

Requirements

Requirement Version Notes
Node.js 20+ Node 18 is EOL
.NET Runtime 8.0+ SDK only needed for building from source
Prettier 3.x

Installing from npm

npm install --save-dev prettier-plugin-tsql prettier

The npm package includes the compiled .NET DLL (bin/dotnet/SqlScriptDom.dll). No separate .NET build step is needed.

Basic Configuration

Create or update your Prettier config to include the plugin:

// prettier.config.js
export default {
    plugins: ['prettier-plugin-tsql'],
};

The plugin registers itself for .sql and .tsql files automatically.

VS Code

Option A — Prettier SQL extension (recommended, no npm required)

Install Prettier SQL from the VS Code Marketplace. It bundles both prettier-plugin-tsql and prettier-plugin-postgresql — no npm install or config file needed.

Requires the .NET 8 Runtime. The extension prompts you if it isn't found.

When the extension is updated, VS Code shows a "Reload Required" notification automatically — click Reload Now to activate the new version. Because the extension runs formatting in a short-lived child process, the DLL is never held open between operations and there is no file-lock issue on update.

Configure options in Settings → search "Prettier SQL", or add to your workspace settings:

{
    "editor.formatOnSave": true,
    "prettierSql.sqlKeywordCase": "lower",
    "prettierSql.sqlDensity": "standard",
    "prettierSql.printWidth": 120
}

For plain .sql files, add -- tsql as the first line to select the T-SQL parser explicitly, or set prettierSql.defaultDialect to tsql in settings.

Option B — Prettier extension with npm plugin

If you already use Prettier - Code formatter in your project, you can add prettier-plugin-tsql as an npm dependency instead:

npm install --save-dev prettier-plugin-tsql prettier

Then configure the plugin in your Prettier config:

// prettier.config.js
export default {
    plugins: ['prettier-plugin-tsql'],
};

Add to your VS Code settings:

{
    "editor.formatOnSave": true,
    "[sql]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    }
}

Updating the plugin: The .NET DLL is loaded into VS Code's extension host process while VS Code is running. On Windows this creates a hard file lock — npm install will fail to overwrite the DLL if VS Code is open. On macOS and Linux the lock is softer but VS Code will keep running the old version anyway.

Close VS Code first, then run npm install, then reopen VS Code.

SQL Server Management Studio 22 (Visual Studio 2026)

SSMS 22 is built on the Visual Studio 2026 shell and supports third-party extensions (unofficially — no marketplace yet, but extensions are not blocked). The PrettierX64 extension brings Prettier formatting to Visual Studio 2022 and 2026, and can load custom plugins including prettier-plugin-tsql.

Node.js is required. Prettier is a Node.js library and must be invoked via a node process. npm and a project are not required — see setup below.

Experimental — this integration has not been formally tested. Plugin loading and .sql file triggering depend on PrettierX64 internals that are not fully documented. See Limitations below before relying on it.

How PrettierX64 finds Prettier

PrettierX64 walks up the directory tree from the file being formatted looking for a node_modules/prettier installation. If it finds one it uses it — including any plugins configured in prettier.config.js alongside it. If it finds nothing it falls back to its own bundled copy of Prettier, which will not have prettier-plugin-tsql.

Setup

1. Install Node.js (20+) from nodejs.org if not already present.

2. Install prettier and the plugin into your Windows user profile directory so they are reachable from any file saved under C:\Users\<you>\:

cd $env:USERPROFILE
npm install prettier prettier-plugin-tsql

3. Create a prettier.config.js in %USERPROFILE%:

// C:\Users\<you>\prettier.config.js
export default {
    plugins: ['prettier-plugin-tsql'],
    sqlKeywordCase: 'lower',
};

4. Install PrettierX64 from the Visual Studio Marketplace and restart SSMS.

5. Format a SQL file with Ctrl+K, Ctrl+J (or right-click → Format with Prettier).

Limitations

Limitation Detail
Unsaved query windows SSMS assigns temp file paths outside %USERPROFILE% to unsaved tabs, so PrettierX64 won't find the config. Save the file first (Ctrl+S) to a location under Documents\.
SSMS compatibility unconfirmed PrettierX64 targets VS 2022/2026 but has not been tested inside SSMS 22 specifically. Microsoft marks third-party SSMS extensions as unsupported.
.sql file detection SQL is not a default Prettier file type. Detection relies on prettier-plugin-tsql registering the .sql extension and PrettierX64 passing the filename to Prettier's API.
.NET DLL loading prettier-plugin-tsql loads a native .NET assembly at runtime via node-api-dotnet. Behavior inside the Visual Studio process (which is itself a .NET host) is untested.
Statements kept as written A few statements the plugin has no printer for are emitted exactly as you wrote them (no reformatting); nothing is dropped. See the README's "Pending implementation".

Troubleshooting

Open View → Output and select "Prettier x64" from the dropdown to see which Prettier installation and config file PrettierX64 is using. If the output shows the bundled version rather than your local install, the node_modules folder was not found on the path from your file up to %USERPROFILE%.


Building from Source

If you want to contribute or use a local build:

Prerequisites

Steps

# 1. Clone and install Node dependencies
git clone https://github.com/schallm/prettier-plugin-tsql.git
cd prettier-plugin-tsql
npm install

# 2. Build the C# parser DLL
npm run build:dotnet

# 3. Compile TypeScript
npm run build:ts

# Or do both at once:
npm run build

# 4. Run tests
npm test

Build Scripts

Script Description
npm run build Full build (C# + TypeScript)
npm run build:dotnet Compile C# project → bin/dotnet/SqlScriptDom.dll
npm run build:ts Compile TypeScript → dist/
npm run build:ts:watch TypeScript watch mode
npm test Run all tests
npm run test:watch Vitest watch mode

Using a Local Build

Point your Prettier config at the local dist/index.js:

// prettier.config.js
export default {
    plugins: ['./path/to/prettier-plugin-tsql/dist/index.js'],
};

Verifying the Setup

Create a test file test.sql:

select
  Books.BookId,
  Books.Title,
  Books.Price,
  Authors.LastName
from
  Books
  inner join Authors on Books.AuthorId = Authors.Id
where Books.InStock = 1
order by Books.Title asc;

Run Prettier:

npx prettier --write test.sql

Expected output:

select
  Books.BookId,
  Books.Title,
  Books.Price,
  Authors.LastName
from
  Books
  inner join Authors on Books.AuthorId = Authors.Id
where Books.InStock = 1
order by Books.Title asc;