Skip to content

Commit 8769ccf

Browse files
committed
fix(android): reset a plugin mirror when another package takes it over
Mirror directories under .ns-plugins are named after aar names, so a different package with the same name can reuse one. Files the CLI added (the built aar) are not in the mirror manifest and would survive, letting Gradle pick up the previous package's aar. The manifest now records the owning package; a mirror without it, or owned by another package, is recreated.
1 parent 0955a2e commit 8769ccf

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ interface IMirroredFile {
1414
}
1515

1616
interface IMirrorManifest {
17+
/**
18+
* The npm package the mirror holds. Mirror directories are named after
19+
* aar names, which another package can take over.
20+
*/
21+
pluginName?: string;
1722
files: { [relativePath: string]: IMirroredFile };
1823
}
1924

@@ -71,8 +76,26 @@ export class AndroidPluginMirrorService implements IAndroidPluginMirrorService {
7176

7277
private syncMirror(mirror: IAndroidPluginMirror): boolean {
7378
const manifestPath = path.join(mirror.mirrorDir, MIRROR_MANIFEST_FILE_NAME);
74-
const previous = this.readManifest(manifestPath);
75-
const next: IMirrorManifest = { files: {} };
79+
let previous = this.readManifest(manifestPath);
80+
let changed = false;
81+
// Files the CLI added (the built aar) are not in the manifest, so a
82+
// mirror taken over by another package would keep them.
83+
if (
84+
previous.pluginName !== mirror.pluginName &&
85+
this.$fs.exists(mirror.mirrorDir)
86+
) {
87+
this.$logger.trace(
88+
`Resetting plugin mirror ${mirror.mirrorDir} for ${mirror.pluginName}.`,
89+
);
90+
this.$fs.deleteDirectory(mirror.mirrorDir);
91+
previous = { files: {} };
92+
changed = true;
93+
}
94+
95+
const next: IMirrorManifest = {
96+
pluginName: mirror.pluginName,
97+
files: {},
98+
};
7699
const excluded = new Set(
77100
(mirror.excludedNativeFiles ?? []).map(
78101
(file) => `${NATIVE_DIR}/${toPosix(file)}`,
@@ -81,7 +104,6 @@ export class AndroidPluginMirrorService implements IAndroidPluginMirrorService {
81104
const upstreamFiles = this.getUpstreamFiles(mirror.packageDir).filter(
82105
(relativePath) => !excluded.has(relativePath),
83106
);
84-
let changed = false;
85107

86108
// Before copying, so a path that turned from a file into a directory (or
87109
// back) upstream is free when its replacement is copied.

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,71 @@ describe("androidPluginMirrorService", () => {
209209
assert.isTrue(fs.exists(packageDir));
210210
});
211211

212+
describe("a mirror directory another package used", () => {
213+
const builtAar = () => mirrored("platforms", "android", "foo_plugin_x.aar");
214+
const otherPackage = () => {
215+
const otherPackageDir = path.join(
216+
path.dirname(path.dirname(packageDir)),
217+
"@bar",
218+
"plugin-x",
219+
);
220+
fs.writeJson(path.join(otherPackageDir, "package.json"), {
221+
name: "@bar/plugin-x",
222+
});
223+
fs.writeFile(
224+
path.join(otherPackageDir, "platforms", "android", "include.gradle"),
225+
"b",
226+
);
227+
return otherPackageDir;
228+
};
229+
230+
it("drops the files the CLI added for the previous package", () => {
231+
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
232+
fs.writeFile(builtAar(), "built for @foo/plugin-x");
233+
mirror = {
234+
pluginName: "@bar/plugin-x",
235+
packageDir: otherPackage(),
236+
mirrorDir: mirror.mirrorDir,
237+
};
238+
239+
assert.deepStrictEqual(mirrorService.syncMirrors(mirrorsRoot, [mirror]), [
240+
"@bar/plugin-x",
241+
]);
242+
243+
assert.isFalse(fs.exists(builtAar()));
244+
assert.isFalse(fs.exists(mirrored("platforms", "android", "libs")));
245+
assert.equal(
246+
fs.readText(mirrored("platforms", "android", "include.gradle")),
247+
"b",
248+
);
249+
});
250+
251+
it("is reset when it does not record which package it holds", () => {
252+
fs.writeFile(builtAar(), "built by an unknown package");
253+
fs.writeJson(mirrored(".ns-mirror.json"), { files: {} });
254+
255+
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
256+
257+
assert.isFalse(fs.exists(builtAar()));
258+
assert.equal(
259+
fs.readText(mirrored("platforms", "android", "include.gradle")),
260+
"a",
261+
);
262+
});
263+
264+
it("keeps the files the CLI added while the package stays the same", () => {
265+
mirrorService.syncMirrors(mirrorsRoot, [mirror]);
266+
fs.writeFile(builtAar(), "built");
267+
268+
assert.deepStrictEqual(
269+
mirrorService.syncMirrors(mirrorsRoot, [mirror]),
270+
[],
271+
);
272+
273+
assert.equal(fs.readText(builtAar()), "built");
274+
});
275+
});
276+
212277
it("never writes into the package", () => {
213278
const before = fs.enumerateFilesInDirectorySync(packageDir).map((file) => ({
214279
file,

0 commit comments

Comments
 (0)