config-loader: retry empty files

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-26 11:05:37 +02:00
parent e5b6767025
commit 773ea341d2
2 changed files with 14 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/config-loader': patch
---
The `FileConfigSource` will now retry file reading after a short delay if it reads an empty file. This is to avoid flakiness during watch mode where change events can trigger before the file content has been written.
@@ -47,7 +47,15 @@ export interface FileConfigSourceOptions {
async function readFile(path: string): Promise<string | undefined> {
try {
return await fs.readFile(path, 'utf8');
const content = await fs.readFile(path, 'utf8');
// During watching we may sometimes read files too early before the file content has been written.
// We never expect the writing to take a long time, but if we encounter an empty file then check
// again after a short delay for safety.
if (content === '') {
await new Promise(resolve => setTimeout(resolve, 10));
return await fs.readFile(path, 'utf8');
}
return content;
} catch (error) {
if (error.code === 'ENOENT') {
return undefined;