Skip to content

High performance self-hosted photo and video management solution.

fix(mobile): prevent crash when generated/openapi has unexpected files (#32599)

Adam Gastineau4f38929bdaaa
diff --git a/mobile/mise.toml b/mobile/mise.toml
index d8c97aac25..f33b762583 100644
--- a/mobile/mise.toml
+++ b/mobile/mise.toml
@@ -151,6 +151,7 @@ depends = ["//:open-api-dart"]
run = [
# This receives passed `--enforce-lockfile` args
"flutter pub get",
+ { task = "install:build-runner-patch" },
{ task = "install:ios" },
]
@@ -210,6 +211,11 @@ fi
cd ios && pod install
'''
+[tasks."install:build-runner-patch"]
+description = "Patch build_runner to prevent crashes when sources are deleted"
+hide = true
+run = "node ./scripts/build_runner_patch.mjs"
+
[tasks."analyze:dart"]
description = "Run Dart analysis"
hide = true
diff --git a/mobile/scripts/build_runner_patch.mjs b/mobile/scripts/build_runner_patch.mjs
new file mode 100644
index 0000000000..77226de2ea
--- /dev/null
+++ b/mobile/scripts/build_runner_patch.mjs
@@ -0,0 +1,20 @@
+// Prevents build_runner crashing when a source file is deleted from a path dependency, which commonly occurs to `generated/openapi` on branch switch
+//
+// Fixed upstream in 2.15.3 (https://github.com/dart-lang/build/pull/5022), but we can't take that dependency due to Dart analyzer being locked
+import { readFileSync, writeFileSync } from 'node:fs';
+
+const packageConfig = new URL('../.dart_tool/package_config.json', import.meta.url);
+
+const config = JSON.parse(readFileSync(packageConfig, 'utf8'));
+const buildRunner = new URL(config.packages.find(({ name }) => name === 'build_runner').rootUri);
+
+const file = new URL(`${buildRunner}/lib/src/build_plan/build_plan.dart`);
+
+const buggy = 'for (final id in deletedOutputs) {';
+const fixed = 'for (final id in deletedOutputs.difference(buildInputs.deletedSources.build().asSet())) {';
+
+const source = readFileSync(file, 'utf8');
+if (source.includes(buggy)) {
+ writeFileSync(file, source.replace(buggy, fixed));
+ console.log('build_runner patch: stopped deleting removed sources from path dependencies');
+}