From ed4ee6fdfb5879e699e1d3bde3bc7f55ce81c857 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 15 Apr 2026 09:45:03 +0200 Subject: [PATCH] cli: fix config path resolution for embedded-postgres detection Resolve config paths relative to the target package directory instead of the workspace root in readDatabaseClient, matching the behavior of the rest of the config loading system. Signed-off-by: Patrik Oldsberg Made-with: Cursor Signed-off-by: Patrik Oldsberg Made-with: Cursor --- .../fix-embedded-postgres-config-paths.md | 5 ++++ .patches/pr-33914.txt | 1 + .../src/lib/runner/runBackend.test.ts | 23 ++++++++++++++++++- .../src/lib/runner/runBackend.ts | 9 ++++++-- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changeset/fix-embedded-postgres-config-paths.md create mode 100644 .patches/pr-33914.txt diff --git a/.changeset/fix-embedded-postgres-config-paths.md b/.changeset/fix-embedded-postgres-config-paths.md new file mode 100644 index 0000000000..7697636fb5 --- /dev/null +++ b/.changeset/fix-embedded-postgres-config-paths.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli-module-build': patch +--- + +Fixed config path resolution for the embedded-postgres database client detection to resolve paths relative to the target package directory rather than the workspace root. diff --git a/.patches/pr-33914.txt b/.patches/pr-33914.txt new file mode 100644 index 0000000000..e9b94dde35 --- /dev/null +++ b/.patches/pr-33914.txt @@ -0,0 +1 @@ +Fix config path resolution for embedded-postgres detection in `repo start` diff --git a/packages/cli-module-build/src/lib/runner/runBackend.test.ts b/packages/cli-module-build/src/lib/runner/runBackend.test.ts index b6ac8fd54d..4f655b041c 100644 --- a/packages/cli-module-build/src/lib/runner/runBackend.test.ts +++ b/packages/cli-module-build/src/lib/runner/runBackend.test.ts @@ -50,10 +50,11 @@ jest.mock('ctrlc-windows', () => ({ })); const mockToConfig = jest.fn(); +const mockConfigSourcesDefault = jest.fn().mockReturnValue({}); jest.mock('@backstage/config-loader', () => ({ ConfigSources: { - default: () => ({}), + default: (...args: any[]) => mockConfigSourcesDefault(...args), toConfig: (...args: any[]) => mockToConfig(...args), }, })); @@ -216,6 +217,26 @@ describe('runBackend', () => { }); }); + it('should resolve config paths relative to targetDir', async () => { + mockToConfig.mockResolvedValue({ + close: jest.fn(), + getOptionalString: () => undefined, + }); + + runBackend({ + entry: 'src/index', + targetDir: '/root/packages/backend', + configPaths: ['../../config/local.yaml'], + }); + await jest.advanceTimersByTimeAsync(100); + + expect(mockConfigSourcesDefault).toHaveBeenCalledWith( + expect.objectContaining({ + argv: ['--config', '/root/config/local.yaml'], + }), + ); + }); + it('should not start embedded DB for other database clients', async () => { mockToConfig.mockResolvedValue({ close: jest.fn(), diff --git a/packages/cli-module-build/src/lib/runner/runBackend.ts b/packages/cli-module-build/src/lib/runner/runBackend.ts index 464ff10cec..95dd8784fd 100644 --- a/packages/cli-module-build/src/lib/runner/runBackend.ts +++ b/packages/cli-module-build/src/lib/runner/runBackend.ts @@ -68,7 +68,10 @@ export async function runBackend(options: RunBackendOptions) { let embeddedDb: Awaited> | undefined; - const dbClient = await readDatabaseClient(options.configPaths); + const dbClient = await readDatabaseClient( + options.configPaths, + options.targetDir, + ); if (dbClient === 'embedded-postgres') { embeddedDb = await startEmbeddedDb(); extraEnv.APP_CONFIG_backend_database = JSON.stringify({ @@ -220,14 +223,16 @@ export async function runBackend(options: RunBackendOptions) { async function readDatabaseClient( configPaths?: string[], + targetDir?: string, ): Promise { const rootDir = targetPaths.rootDir; + const configBaseDir = targetDir ?? rootDir; const source = ConfigSources.default({ rootDir, allowMissingDefaultConfig: true, argv: (configPaths ?? []).flatMap(p => [ '--config', - isAbsolutePath(p) ? p : resolvePath(rootDir, p), + isAbsolutePath(p) ? p : resolvePath(configBaseDir, p), ]), });