Skip to content

Commit

Permalink
Merge pull request backstage#11083 from backstage/freben/empty-config
Browse files Browse the repository at this point in the history
handle empty config files gracefully
  • Loading branch information
freben authored Apr 26, 2022
2 parents bc7cbb7 + 9e8ef53 commit 08e7fc5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-flies-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/config-loader': patch
---

Handle empty config files gracefully
12 changes: 12 additions & 0 deletions packages/config-loader/src/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('loadConfig', () => {
`,
'/root/secrets/substituted.txt': '123abc',
'/root/${ESCAPE_ME}.txt': 'notSubstituted',
'/root/empty.yaml': '# just a comment',
});
});

Expand Down Expand Up @@ -469,6 +470,17 @@ describe('loadConfig', () => {
await new Promise(resolve => setTimeout(resolve, 1000));
});

it('handles empty files gracefully', async () => {
await expect(
loadConfig({
configRoot: '/root',
configTargets: [{ path: '/root/empty.yaml' }],
}),
).resolves.toEqual({
appConfigs: [],
});
});

function defer<T>() {
let resolve: (value: T) => void;
const promise = new Promise<T>(_resolve => {
Expand Down
16 changes: 10 additions & 6 deletions packages/config-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,17 @@ export async function loadConfig(
};

const input = yaml.parse(await readFile(configPath));
const substitutionTransform = createSubstitutionTransform(env);
const data = await applyConfigTransforms(dir, input, [
createIncludeTransform(env, readFile, substitutionTransform),
substitutionTransform,
]);

fileConfigs.push({ data, context: basename(configPath) });
// A completely empty file ends up as a null return value
if (input !== null) {
const substitutionTransform = createSubstitutionTransform(env);
const data = await applyConfigTransforms(dir, input, [
createIncludeTransform(env, readFile, substitutionTransform),
substitutionTransform,
]);

fileConfigs.push({ data, context: basename(configPath) });
}
}

return { fileConfigs, loadedPaths };
Expand Down

0 comments on commit 08e7fc5

Please sign in to comment.