diff --git a/.changeset/hip-pets-glow.md b/.changeset/hip-pets-glow.md new file mode 100644 index 0000000000..2b988c0d4c --- /dev/null +++ b/.changeset/hip-pets-glow.md @@ -0,0 +1,5 @@ +--- +'@backstage/config-loader': patch +--- + +From now on the `$file` placeholder will trim the whitespaces and newline characters from the end of the file it reads. diff --git a/packages/config-loader/src/lib/transform/include.test.ts b/packages/config-loader/src/lib/transform/include.test.ts index 0913b31f66..c80efe6785 100644 --- a/packages/config-loader/src/lib/transform/include.test.ts +++ b/packages/config-loader/src/lib/transform/include.test.ts @@ -49,6 +49,8 @@ const readFile = jest.fn(async (path: string) => { const content = ( { [resolvePath(root, 'my-secret')]: 'secret', + [resolvePath(root, 'with-newline-at-the-end')]: + 'value without newline at the end\n\n', [resolvePath(root, 'my-data.json')]: '{"a":{"b":{"c":42}}}', [resolvePath(root, 'my-data.yaml')]: 'some:\n yaml:\n key: 7', [resolvePath(root, 'my-data.yml')]: 'different: { key: hello }', @@ -92,6 +94,14 @@ describe('includeTransform', () => { includeTransform({ $file: 'no-secret' }, root), ).rejects.toThrow('File not found!'); }); + it('should trim newlines from end of file', async () => { + await expect( + includeTransform({ $file: 'with-newline-at-the-end' }, root), + ).resolves.toEqual({ + applied: true, + value: 'value without newline at the end', + }); + }); it('should include env vars', async () => { await expect(includeTransform({ $env: 'SECRET' }, root)).resolves.toEqual({ diff --git a/packages/config-loader/src/lib/transform/include.ts b/packages/config-loader/src/lib/transform/include.ts index 569b61c82c..84e4a9294d 100644 --- a/packages/config-loader/src/lib/transform/include.ts +++ b/packages/config-loader/src/lib/transform/include.ts @@ -73,7 +73,7 @@ export function createIncludeTransform( case '$file': try { const value = await readFile(resolvePath(baseDir, includeValue)); - return { applied: true, value }; + return { applied: true, value: value.trimEnd() }; } catch (error) { throw new Error(`failed to read file ${includeValue}, ${error}`); }