Skip to content

Commit 0e44f9a

Browse files
committed
fix(android): delete leftover aars only after the new aar exists
buildAar deleted a plugin's leftover aars (the old `<short>.aar`, or a scope-qualified aar the CLI built for it) before running Gradle. When the build failed, for example offline, the plugin was left with no aar at all. The leftovers are still identified before the build, and a scope-qualified aar that differs from the CLI's earlier output still stops prepare before Gradle runs. They are now deleted only after the new aar has been copied, or when no rebuild is needed and the aar under the current name is already there. A failed build keeps the old aar, and hasOutdatedAar keeps reporting the plugin so the next prepare retries.
1 parent c6a117e commit 0e44f9a

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
254254
...staleAars.map((staleAar) => staleAar.aarPath),
255255
],
256256
);
257-
this.removeStaleAars(options.pluginName, staleAars);
257+
this.ensureStaleAarsAreBuildOutputs(options.pluginName, staleAars);
258258

259259
const shouldBuildAar = await this.shouldBuildAar({
260260
hasAarSources,
@@ -297,6 +297,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
297297
this.writePluginHashInfo(pluginSourceFileHashesInfo, pluginTempDir);
298298
}
299299

300+
this.removeStaleAars(staleAars);
301+
300302
return shouldBuildAar;
301303
}
302304

@@ -388,12 +390,10 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
388390
}
389391
}
390392

391-
/**
392-
* Gradle templates add every aar in a plugin's platforms/android to the app,
393-
* so an aar the CLI built for this plugin under another candidate name would
394-
* duplicate its classes.
395-
*/
396-
private removeStaleAars(pluginName: string, staleAars: IStaleAar[]): void {
393+
private ensureStaleAarsAreBuildOutputs(
394+
pluginName: string,
395+
staleAars: IStaleAar[],
396+
): void {
397397
for (const { aarPath, previousOutputPath } of staleAars) {
398398
if (
399399
previousOutputPath &&
@@ -404,7 +404,15 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
404404
);
405405
}
406406
}
407+
}
407408

409+
/**
410+
* Gradle templates add every aar in a plugin's platforms/android to the app,
411+
* so an aar the CLI built for this plugin under another candidate name would
412+
* duplicate its classes. Only call once the aar under the current name
413+
* exists, so a failed build leaves the plugin with its previous aar.
414+
*/
415+
private removeStaleAars(staleAars: IStaleAar[]): void {
408416
for (const { aarPath } of staleAars) {
409417
this.$watchIgnoreListService.addFileToIgnoreList(aarPath);
410418
this.$logger.trace(`Removing stale plugin aar ${aarPath}`);

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe("androidPluginBuildService", () => {
2424
const pluginName = "my-plugin";
2525
const shortPluginName = getShortPluginName(pluginName);
2626
let spawnFromEventCalled = false;
27+
let failGradleBuild = false;
2728
let ignoredFiles: string[] = [];
2829
let hashedFiles: string[] = [];
2930
let fs: IFileSystem;
@@ -75,12 +76,16 @@ describe("androidPluginBuildService", () => {
7576
hasChangesInShasums?: boolean;
7677
}): void {
7778
const testInjector: IInjector = new stubs.InjectorStub();
79+
failGradleBuild = false;
7880
testInjector.register("fs", FsLib.FileSystem);
7981
testInjector.register("childProcess", {
8082
spawnFromEvent: async (
8183
command: string,
8284
args: string[],
8385
): Promise<ISpawnResult> => {
86+
if (failGradleBuild) {
87+
throw new Error("Gradle build failed");
88+
}
8489
const pluginDir = args[args.indexOf("-p") + 1];
8590
const aar = path.join(
8691
pluginDir,
@@ -513,6 +518,58 @@ dependencies {
513518
assert.isFalse(androidBuildPluginService.hasOutdatedAar(config));
514519
});
515520

521+
it("keeps the short-name aar when the build fails", async () => {
522+
const config = setupScoped();
523+
const staleAar = path.join(pluginFolder, `${shortPluginName}.aar`);
524+
fs.writeFile(staleAar, "previous build");
525+
failGradleBuild = true;
526+
527+
await assert.isRejected(
528+
androidBuildPluginService.buildAar(config),
529+
/Gradle build failed/,
530+
);
531+
532+
assert.equal(fs.readText(staleAar), "previous build");
533+
assert.isFalse(
534+
fs.exists(path.join(pluginFolder, `${qualifiedName}.aar`)),
535+
);
536+
assert.isTrue(androidBuildPluginService.hasOutdatedAar(config));
537+
});
538+
539+
it("deletes the short-name aar only once the new aar is in place", async () => {
540+
const config = setupScoped();
541+
const newAar = path.join(pluginFolder, `${qualifiedName}.aar`);
542+
const staleAar = path.join(pluginFolder, `${shortPluginName}.aar`);
543+
fs.writeFile(staleAar, "previous build");
544+
const deleteFile = fs.deleteFile.bind(fs);
545+
const newAarExistedOnDelete: boolean[] = [];
546+
fs.deleteFile = (file: string) => {
547+
if (file === staleAar) {
548+
newAarExistedOnDelete.push(fs.exists(newAar));
549+
}
550+
deleteFile(file);
551+
};
552+
553+
assert.isTrue(await androidBuildPluginService.buildAar(config));
554+
555+
assert.deepStrictEqual(newAarExistedOnDelete, [true]);
556+
assert.isFalse(fs.exists(staleAar));
557+
});
558+
559+
it("deletes a short-name aar that appears when no rebuild is needed", async () => {
560+
const config = setupScoped();
561+
assert.isTrue(await androidBuildPluginService.buildAar(config));
562+
const staleAar = path.join(pluginFolder, `${shortPluginName}.aar`);
563+
fs.writeFile(staleAar, "previous build");
564+
assert.isTrue(androidBuildPluginService.hasOutdatedAar(config));
565+
566+
assert.isFalse(await androidBuildPluginService.buildAar(config));
567+
568+
assert.isFalse(fs.exists(staleAar));
569+
assert.include(ignoredFiles, staleAar);
570+
assert.isFalse(androidBuildPluginService.hasOutdatedAar(config));
571+
});
572+
516573
it("deletes the old short-name aars of clashing plugins built by an older CLI", async () => {
517574
const foo = setupScoped({
518575
pluginName: "@foo/plugin-x",

0 commit comments

Comments
 (0)