language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Add `schemaPath` support

Open erbiker opened this issue 11 months ago • 0 comments

Add schemaPath Setting Support to Language Server

Summary

This PR adds support for the prisma.schemaPath VS Code setting to the Language Server, enabling proper multi-file schema support in monorepos and projects with custom schema locations.

Problem

Previously, the prisma.schemaPath setting was only used for CLI commands (like prisma generate) but was not passed to the Language Server. This caused the following issues:

  • Multi-file schemas in custom directories failed to be compatible with the LSP when the prisma.config.ts was not at the workspace root (colocated at the custom directory)
    • "Go to Definition" failed for cross-file references
    • Autocomplete, hover, and other IDE features didn't work across files
    • The Language Server only validated the currently opened file in isolation

The Prisma CLI (generate, validate, etc.) worked with the same configuration, making this purely a Language Server/extension issue.

Solution

This PR implements the following changes:

1. Language Server Changes (packages/language-server/)

  • src/lib/types.ts: Added schemaPath field to LSSettings interface
  • src/lib/Schema.ts:
    • Added PrismaSchemaLoadOptions interface with schemaPath and configRoot options
    • Updated PrismaSchema.load() to accept options object (backward compatible with string configRoot)
    • Implemented priority-based schema path resolution:
      1. schemaPath from VS Code settings
      2. schema path from prisma.config.ts
      3. Current document path (fallback)
  • src/server.ts: Updated all LSP request handlers to pass schemaPath from settings to PrismaSchema.load()

2. VS Code Extension Changes (packages/vscode/)

  • package.json: Updated prisma.schemaPath setting description to clarify it now works with the Language Server and supports directories

3. Tests (packages/language-server/src/__test__/)

  • schema-path.test.ts: Test suite covering:
    • Loading multi-file schemas via schemaPath option
    • Fallback behavior when schemaPath is not provided
    • Backward compatibility with old string configRoot parameter
    • Priority of schemaPath over prisma.config.ts
    • Direct loading from SchemaDocument arrays

4. Documentation

  • docs/language-server.md: Added detailed section on schema path resolution and VS Code configuration
  • packages/vscode/package.json: Enhanced setting description with examples

Behavior

Before

// Language Server ignored prisma.schemaPath setting
const schema = await PrismaSchema.load({ 
  currentDocument, 
  allDocuments 
})
// ❌ Only loaded current document's directory

After

// Language Server respects prisma.schemaPath setting
const schema = await PrismaSchema.load(
  { currentDocument, allDocuments },
  { schemaPath: settings.schemaPath }
)
// ✅ Loads all files from configured schema path

Schema Path Resolution Priority

  1. schemaPath option (from VS Code prisma.schemaPath setting)
  2. prisma.config.ts (discovered by searching upward from schemaPath or current document)
  3. Current document path (fallback)

This matches the Prisma CLI behavior, ensuring consistency between IDE and command-line tooling.

Configuration Example

Users can now configure multi-file schemas in monorepos:

// .vscode/settings.json
{
  "prisma.schemaPath": "packages/backend/prisma"
}
// packages/backend/prisma.config.ts
export default defineConfig({
  schema: './prisma',
  migrations: {
    seed: 'tsx ./scripts/seed.ts',
  },
});

Backward Compatibility

  • ✅ Existing code using PrismaSchema.load(input, configRoot) continues to work
  • ✅ Tests using string configRoot parameter are unaffected
  • ✅ Projects without schemaPath setting work as before

erbiker avatar Jan 01 '26 19:01 erbiker