config-loader: retry empty files
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user