Skip to content

Commit aa90354

Browse files
committed
fix(android): start an interrupted plugin mirror over
A mirror's manifest is removed before a sync changes it and written back once every file is in place and include.gradle is migrated. An interrupted or failed sync then resets the mirror instead of failing on paths whose type changed, keeping stray files or skipping the migration.
1 parent 1c87ff6 commit aa90354

5 files changed

Lines changed: 208 additions & 12 deletions

File tree

‎lib/contracts/internal/android-plugin-mirror-service.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ export abstract class AndroidPluginMirrorService {
99
/**
1010
* Brings `<mirrorsRoot>/<dir>` in line with each plugin's package.json and
1111
* platforms/android, and removes mirrors of plugins not in `mirrors`.
12+
* @param onChanged Adapts a mirror whose files changed. If it throws, the
13+
* next sync starts that mirror over.
1214
* @returns The names of the plugins whose mirror changed.
1315
*/
1416
abstract syncMirrors(
1517
mirrorsRoot: string,
1618
mirrors: IAndroidPluginMirror[],
19+
onChanged?: (mirror: IAndroidPluginMirror) => void,
1720
): string[];
1821
}

‎lib/services/android-project-service.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,9 @@ export class AndroidProjectService
796796
}
797797
layout.changedMirrors.delete(pluginData.name);
798798

799-
this.$androidPluginBuildService.migrateIncludeGradle({
800-
...options,
801-
platformsAndroidDirPath: options.aarOutputDir,
802-
});
799+
if (layout.pluginBuildLocation !== "platforms") {
800+
this.$androidPluginBuildService.migrateIncludeGradle(options);
801+
}
803802
}
804803

805804
public shouldRepreparePlugin(
@@ -1015,6 +1014,15 @@ export class AndroidProjectService
10151014
),
10161015
};
10171016
}),
1017+
({ pluginName }) => {
1018+
const { nativeDir } = plugins.get(pluginName);
1019+
this.$androidPluginBuildService.migrateIncludeGradle({
1020+
pluginName,
1021+
platformsAndroidDirPath: nativeDir,
1022+
aarOutputDir: nativeDir,
1023+
tempPluginDirPath,
1024+
});
1025+
},
10181026
);
10191027
for (const name of synced) {
10201028
changedMirrors.add(name);

‎lib/services/android/android-plugin-mirror-service.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const NATIVE_DIR = `${PLATFORMS_DIR_NAME}/android`;
3434
* aar). Copies keep the upstream mtime, and files whose upstream content is
3535
* unchanged are never rewritten, so Gradle's path and mtime based inputs stay
3636
* up to date across syncs.
37+
*
38+
* A mirror has no manifest while a sync is changing it, so one that was
39+
* interrupted is started over.
3740
*/
3841
export class AndroidPluginMirrorServiceImpl implements AndroidPluginMirrorService {
3942
private fs = inject(FileSystem);
@@ -43,11 +46,12 @@ export class AndroidPluginMirrorServiceImpl implements AndroidPluginMirrorServic
4346
public syncMirrors(
4447
mirrorsRoot: string,
4548
mirrors: IAndroidPluginMirror[],
49+
onChanged?: (mirror: IAndroidPluginMirror) => void,
4650
): string[] {
4751
this.removeUnknownMirrors(mirrorsRoot, mirrors);
4852

4953
return mirrors
50-
.filter((mirror) => this.syncMirror(mirror))
54+
.filter((mirror) => this.syncMirror(mirror, onChanged))
5155
.map((mirror) => mirror.pluginName);
5256
}
5357

@@ -71,10 +75,19 @@ export class AndroidPluginMirrorServiceImpl implements AndroidPluginMirrorServic
7175
}
7276
}
7377

74-
private syncMirror(mirror: IAndroidPluginMirror): boolean {
78+
private syncMirror(
79+
mirror: IAndroidPluginMirror,
80+
onChanged?: (mirror: IAndroidPluginMirror) => void,
81+
): boolean {
7582
const manifestPath = path.join(mirror.mirrorDir, MIRROR_MANIFEST_FILE_NAME);
7683
let previous = this.readManifest(manifestPath);
7784
let changed = false;
85+
const beginChange = () => {
86+
if (!changed) {
87+
this.fs.deleteFile(manifestPath);
88+
changed = true;
89+
}
90+
};
7891
// Files the CLI added (the built aar) are not in the manifest, so a
7992
// mirror taken over by another package would keep them.
8093
if (
@@ -84,9 +97,9 @@ export class AndroidPluginMirrorServiceImpl implements AndroidPluginMirrorServic
8497
this.logger.trace(
8598
`Resetting plugin mirror ${mirror.mirrorDir} for ${mirror.pluginName}.`,
8699
);
100+
beginChange();
87101
this.fs.deleteDirectory(mirror.mirrorDir);
88102
previous = { files: {} };
89-
changed = true;
90103
}
91104

92105
const next: IMirrorManifest = {
@@ -107,8 +120,8 @@ export class AndroidPluginMirrorServiceImpl implements AndroidPluginMirrorServic
107120
const upstream = new Set(upstreamFiles);
108121
for (const relativePath of Object.keys(previous.files)) {
109122
if (!upstream.has(relativePath)) {
123+
beginChange();
110124
this.deleteMirroredFile(mirror.mirrorDir, relativePath);
111-
changed = true;
112125
}
113126
}
114127

@@ -124,19 +137,23 @@ export class AndroidPluginMirrorServiceImpl implements AndroidPluginMirrorServic
124137
}
125138

126139
if (!known || known.sha1 !== sha1 || !this.fs.exists(destination)) {
140+
beginChange();
127141
const stat = this.fs.getFsStats(source);
128142
this.fs.deleteFile(destination);
129143
this.fs.copyFile(source, destination);
130144
// Read-only stores (pnpm, Nix, Bazel) ship 0444 files; the CLI rewrites
131145
// some mirrored files and replaces others on the next sync.
132146
this.fs.chmod(destination, stat.mode | 0o200);
133147
this.fs.utimes(destination, stat.atime, stat.mtime);
134-
changed = true;
135148
}
136149

137150
next.files[relativePath] = { sha1 };
138151
}
139152

153+
if (changed) {
154+
onChanged?.(mirror);
155+
}
156+
140157
const manifestContent = JSON.stringify(next, null, "\t");
141158
if (
142159
!this.fs.exists(manifestPath) ||

‎test/services/android-gradle-facts.ts‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe("android plugin layout and Gradle facts", () => {
4747
let migrateIncludeGradleCalls: IPluginBuildOptions[];
4848
let outdatedAar: boolean;
4949
let buildAarError: Error;
50+
let migrateIncludeGradleError: Error;
5051
let memo: NativeFileHashesMemo;
5152
let logger: stubs.LoggerStub;
5253

@@ -157,6 +158,7 @@ describe("android plugin layout and Gradle facts", () => {
157158
migrateIncludeGradleCalls = [];
158159
outdatedAar = false;
159160
buildAarError = null;
161+
migrateIncludeGradleError = null;
160162

161163
const injector = new Yok();
162164
injector.register("injector", injector);
@@ -184,6 +186,9 @@ describe("android plugin layout and Gradle facts", () => {
184186
},
185187
migrateIncludeGradle: (options: IPluginBuildOptions) => {
186188
migrateIncludeGradleCalls.push(options);
189+
if (migrateIncludeGradleError) {
190+
throw migrateIncludeGradleError;
191+
}
187192
return false;
188193
},
189194
hasOutdatedAar: () => outdatedAar,
@@ -618,10 +623,45 @@ describe("android plugin layout and Gradle facts", () => {
618623
aarName: "foo_plugin_x",
619624
pluginBuildLocation: "platforms",
620625
});
621-
assert.equal(
622-
migrateIncludeGradleCalls[0].platformsAndroidDirPath,
623-
mirrorNativeDir,
626+
assert.sameMembers(
627+
migrateIncludeGradleCalls.map((call) => call.platformsAndroidDirPath),
628+
["core", "foo_plugin_x", "bar_plugin_x"].map((name) =>
629+
path.join(platformDir, ".ns-plugins", name, "platforms", "android"),
630+
),
631+
);
632+
});
633+
634+
it("migrates the include.gradle of a copy without preparing the plugin", async () => {
635+
addPlugin({
636+
name: "@foo/plugin-x",
637+
nativeFiles: { "include.gradle": "dependencies {}" },
638+
});
639+
const projectData = createProjectData("platforms");
640+
const copies = () =>
641+
migrateIncludeGradleCalls.map((call) => call.platformsAndroidDirPath);
642+
const copy = path.join(
643+
platformDir,
644+
".ns-plugins",
645+
"plugin_x",
646+
"platforms",
647+
"android",
648+
);
649+
650+
migrateIncludeGradleError = new Error("cannot write");
651+
await assert.isRejected(
652+
service.beforePrepareAllPlugins(projectData, dependencies),
653+
/cannot write/,
654+
);
655+
migrateIncludeGradleError = null;
656+
await service.beforePrepareAllPlugins(projectData, dependencies);
657+
assert.deepStrictEqual(copies(), [copy, copy]);
658+
659+
await service.beforePrepareAllPlugins(projectData, dependencies);
660+
await service.preparePluginNativeCode(
661+
pluginData("@foo/plugin-x"),
662+
projectData,
624663
);
664+
assert.deepStrictEqual(copies(), [copy, copy]);
625665
});
626666

627667
it("gives every copy its own directory, also for plugins without sources to build", async () => {

‎test/services/android/android-plugin-mirror-service.ts‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,134 @@ describe("androidPluginMirrorService", () => {
204204
);
205205
});
206206

207+
describe("an interrupted sync", () => {
208+
const interruptSync = () => {
209+
const copyFile = fs.copyFile;
210+
fs.copyFile = function (source: string, destination: string) {
211+
if (path.basename(source) === "zzz.txt") {
212+
throw new Error("interrupted");
213+
}
214+
return copyFile.call(this, source, destination);
215+
};
216+
fs.writeFile(upstream("platforms", "android", "zzz.txt"), "z");
217+
try {
218+
assert.throws(
219+
() => mirrorService.syncMirrors(mirrorsRoot, [mirror]),
220+
"interrupted",
221+
);
222+
} finally {
223+
fs.copyFile = copyFile;
224+
}
225+
};
226+
const assertRecovers = () => {
227+
assert.deepStrictEqual(mirrorService.syncMirrors(mirrorsRoot, [mirror]), [
228+
"@foo/plugin-x",
229+
]);
230+
assert.deepStrictEqual(
231+
mirrorService.syncMirrors(mirrorsRoot, [mirror]),
232+
[],
233+
);
234+
};
235+
let config: string;
236+
237+
beforeEach(() => {
238+
config = upstream("platforms", "android", "assets", "config");
239+
});
240+
241+
it("recovers when a file turned into a directory upstream", () => {
242+
fs.writeFile(config, "file");
243+
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
244+
fs.deleteFile(config);
245+
fs.writeFile(path.join(config, "settings.json"), "{}");
246+
interruptSync();
247+
248+
assertRecovers();
249+
250+
assert.equal(
251+
fs.readText(
252+
mirrored("platforms", "android", "assets", "config", "settings.json"),
253+
),
254+
"{}",
255+
);
256+
});
257+
258+
it("recovers when a directory turned into a file upstream", () => {
259+
fs.writeFile(path.join(config, "settings.json"), "{}");
260+
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
261+
fs.deleteDirectory(config);
262+
fs.writeFile(config, "file");
263+
interruptSync();
264+
265+
assertRecovers();
266+
267+
assert.equal(
268+
fs.readText(mirrored("platforms", "android", "assets", "config")),
269+
"file",
270+
);
271+
});
272+
273+
it("does not leave files behind that upstream removed before the next sync", () => {
274+
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
275+
const extra = upstream("platforms", "android", "libs", "extra.aar");
276+
fs.writeFile(extra, "extra");
277+
interruptSync();
278+
fs.deleteFile(extra);
279+
280+
assertRecovers();
281+
282+
assert.isFalse(
283+
fs.exists(mirrored("platforms", "android", "libs", "extra.aar")),
284+
);
285+
});
286+
});
287+
288+
describe("onChanged", () => {
289+
let changed: string[];
290+
const onChanged = ({ pluginName }: IAndroidPluginMirror) => {
291+
changed.push(pluginName);
292+
};
293+
294+
beforeEach(() => {
295+
changed = [];
296+
});
297+
298+
it("is called once the files of a changed mirror are in place", () => {
299+
mirrorService.syncMirrors(mirrorsRoot, [mirror], (syncedMirror) => {
300+
onChanged(syncedMirror);
301+
assert.equal(
302+
fs.readText(mirrored("platforms", "android", "include.gradle")),
303+
"a",
304+
);
305+
});
306+
307+
assert.deepStrictEqual(changed, ["@foo/plugin-x"]);
308+
});
309+
310+
it("is not called for an unchanged mirror", () => {
311+
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
312+
313+
mirrorService.syncMirrors(mirrorsRoot, [mirror], onChanged);
314+
315+
assert.deepStrictEqual(changed, []);
316+
});
317+
318+
it("is called again after it failed", () => {
319+
assert.throws(
320+
() =>
321+
mirrorService.syncMirrors(mirrorsRoot, [mirror], () => {
322+
throw new Error("failed");
323+
}),
324+
"failed",
325+
);
326+
327+
assert.deepStrictEqual(
328+
mirrorService.syncMirrors(mirrorsRoot, [mirror], onChanged),
329+
["@foo/plugin-x"],
330+
);
331+
assert.deepStrictEqual(changed, ["@foo/plugin-x"]);
332+
});
333+
});
334+
207335
it("removes the mirrors of plugins that are gone", () => {
208336
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
209337
assert.isTrue(fs.exists(mirror.mirrorDir));

0 commit comments

Comments
 (0)