From 773ea341d28cc87176b74979798718cb06012f55 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 26 Sep 2023 11:05:37 +0200 Subject: [PATCH] config-loader: retry empty files Signed-off-by: Patrik Oldsberg --- .changeset/shy-clouds-reflect.md | 5 +++++ packages/config-loader/src/sources/FileConfigSource.ts | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/shy-clouds-reflect.md diff --git a/.changeset/shy-clouds-reflect.md b/.changeset/shy-clouds-reflect.md new file mode 100644 index 0000000000..2861bad9f3 --- /dev/null +++ b/.changeset/shy-clouds-reflect.md @@ -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. diff --git a/packages/config-loader/src/sources/FileConfigSource.ts b/packages/config-loader/src/sources/FileConfigSource.ts index da31c128a0..1de0fbf2d8 100644 --- a/packages/config-loader/src/sources/FileConfigSource.ts +++ b/packages/config-loader/src/sources/FileConfigSource.ts @@ -47,7 +47,15 @@ export interface FileConfigSourceOptions { async function readFile(path: string): Promise { 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;