|
| 1 | +import { formatDocument } from '@zenstackhq/language'; |
1 | 2 | import fs from 'node:fs'; |
2 | 3 | import path from 'node:path'; |
3 | 4 | import { describe, expect, it } from 'vitest'; |
4 | | -import { createProject, runCli } from './utils'; |
| 5 | +import { createProject, getDefaultPrelude, runCli } from './utils'; |
5 | 6 |
|
6 | 7 | const model = ` |
7 | 8 | model User { |
@@ -306,6 +307,50 @@ export default plugin; |
306 | 307 | expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true); |
307 | 308 | }); |
308 | 309 |
|
| 310 | + it('should resolve plugin paths relative to the schema file where the plugin is declared', async () => { |
| 311 | + // Entry schema imports a sub-schema that declares a plugin with a relative path. |
| 312 | + // The plugin path should resolve relative to the sub-schema, not the entry schema. |
| 313 | + const { workDir } = await createProject( |
| 314 | + `import './core/core' |
| 315 | +
|
| 316 | +${getDefaultPrelude()} |
| 317 | +
|
| 318 | +model User { |
| 319 | + id String @id @default(cuid()) |
| 320 | +} |
| 321 | +`, |
| 322 | + { customPrelude: true }, |
| 323 | + ); |
| 324 | + |
| 325 | + // Create core/ subdirectory with its own schema and plugin |
| 326 | + const coreDir = path.join(workDir, 'zenstack/core'); |
| 327 | + fs.mkdirSync(coreDir, { recursive: true }); |
| 328 | + |
| 329 | + const coreSchema = await formatDocument(` |
| 330 | +plugin foo { |
| 331 | + provider = './my-core-plugin.ts' |
| 332 | +} |
| 333 | +`); |
| 334 | + fs.writeFileSync(path.join(coreDir, 'core.zmodel'), coreSchema); |
| 335 | + |
| 336 | + // Plugin lives next to the core schema, NOT next to the entry schema |
| 337 | + fs.writeFileSync( |
| 338 | + path.join(coreDir, 'my-core-plugin.ts'), |
| 339 | + ` |
| 340 | +const plugin = { |
| 341 | + name: 'core-plugin', |
| 342 | + statusText: 'Testing core plugin', |
| 343 | + async generate() {}, |
| 344 | +}; |
| 345 | +export default plugin; |
| 346 | +`, |
| 347 | + ); |
| 348 | + |
| 349 | + // This would fail if the plugin path was resolved relative to the entry schema |
| 350 | + runCli('generate', workDir); |
| 351 | + expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true); |
| 352 | + }); |
| 353 | + |
309 | 354 | it('should prefer CLI options over @core/typescript plugin settings for generateModels and generateInput', async () => { |
310 | 355 | const modelWithPlugin = ` |
311 | 356 | plugin typescript { |
|
0 commit comments