Skip to content

Commit 389aa01

Browse files
committed
fix(android): give plugins with clashing short names unique aar names
The CLI names a plugin's aar, its temp build project and the `org.nativescript.<name>` namespace fallback after the plugin's short name, which drops the npm scope. Two scoped plugins with the same name, such as `@foo/plugin-x` and `@bar/plugin-x`, therefore both build `plugin_x.aar`, share `platforms/tempPlugin/plugin_x` and get the same fallback namespace. Which plugin's code ends up linked depends on the order they are built in. Each native Android plugin now gets an aar name that is unique in the project. A plugin whose short name no other native plugin shares keeps it, so projects without a clash build exactly as before: same dependencies.json, same aar names and paths. Within a group of plugins that share a short name, scoped plugins get `<scope>_<short>` (the scope without `@`, with characters other than [A-Za-z0-9_] replaced by `_`), an unscoped plugin keeps its short name, and a name that would start with a digit gets a leading `_`. The result does not depend on the order of the dependencies. If two plugins still end up with the same name, prepare fails and names both packages. `ns plugin build` is unchanged. Old Gradle templates add every *.aar in a plugin's platforms/android to the app, so an aar the CLI built under the plugin's previous name would add duplicate classes. When the CLI builds a plugin (it has a manifest or Android sources), each aar in its platforms/android named after another of its candidate names is handled as follows: - the plain short name: earlier CLIs always copied the built aar to `<short>.aar`, overwriting anything the package shipped under that name, so the file is a leftover and is deleted; - the scope-qualified name: only this CLI builds it, so the file is a leftover only when `<tempPlugin>/<name>/build.gradle` sets `project.ext.PLUGIN_NAME` to this plugin's npm name and the temp project has a build output at `<tempPlugin>/<name>/build/outputs/aar/<name>-release.aar`. If the file is byte-identical to that output it is deleted; if it differs, prepare fails with both paths and asks to delete it or run `ns clean`. Otherwise the package ships it and it is left alone. Aars of plugins the CLI does not build are never deleted or flagged. The Android project service reports a plugin for reprepare while its aar is missing under the new name or a leftover is still there, so upgrading the CLI on a project with a clash rebuilds the renamed aars and deletes the old `<short>.aar` in each clashing plugin.
1 parent c77ff7d commit 389aa01

8 files changed

Lines changed: 1006 additions & 59 deletions

File tree

‎lib/common/helpers.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,71 @@ function sanitizePluginName(pluginName: string): string {
125125
return pluginName.split("/").pop();
126126
}
127127

128+
function getScopeQualifiedPluginName(pluginName: string): string | null {
129+
const parts = pluginName.split("/");
130+
if (parts.length < 2 || !parts[0].startsWith("@")) {
131+
return null;
132+
}
133+
134+
const scope = parts[0].slice(1).replace(/[^A-Za-z0-9_]/g, "_");
135+
return `${scope}_${getShortPluginName(pluginName)}`;
136+
}
137+
138+
// The aar name doubles as a Java package segment (the `org.nativescript.<name>`
139+
// namespace fallback), which cannot start with a digit.
140+
function withoutLeadingDigit(name: string): string {
141+
return /^[0-9]/.test(name) ? `_${name}` : name;
142+
}
143+
144+
/**
145+
* Maps each plugin name to a file-system and Java-package safe name for its aar
146+
* that is unique among `pluginNames`. A plugin whose short name is not shared
147+
* keeps `getShortPluginName(name)`, so projects without clashes keep their names.
148+
* Throws if two plugins still end up with the same name.
149+
*/
150+
export function getUniqueShortPluginNames(
151+
pluginNames: string[],
152+
): Map<string, string> {
153+
const sortedNames = _.sortBy(_.uniq(pluginNames));
154+
const namesByShortName = _.groupBy(sortedNames, getShortPluginName);
155+
const result = new Map<string, string>();
156+
const owners = new Map<string, string>();
157+
158+
for (const pluginName of sortedNames) {
159+
const shortName = getShortPluginName(pluginName);
160+
const aarName =
161+
namesByShortName[shortName].length === 1
162+
? shortName
163+
: withoutLeadingDigit(
164+
getScopeQualifiedPluginName(pluginName) ?? shortName,
165+
);
166+
167+
const owner = owners.get(aarName);
168+
if (owner !== undefined) {
169+
throw new Error(
170+
`The plugins "${owner}" and "${pluginName}" both need the Android aar name "${aarName}". Remove one of them from the project.`,
171+
);
172+
}
173+
174+
owners.set(aarName, pluginName);
175+
result.set(pluginName, aarName);
176+
}
177+
178+
return result;
179+
}
180+
181+
/**
182+
* Every aar name `getUniqueShortPluginNames` can give this plugin, depending on
183+
* which other plugins are installed.
184+
*/
185+
export function getCandidateAarNames(pluginName: string): string[] {
186+
const shortName = getShortPluginName(pluginName);
187+
return _.uniq([
188+
shortName,
189+
withoutLeadingDigit(getScopeQualifiedPluginName(pluginName) ?? shortName),
190+
]);
191+
}
192+
128193
export async function executeActionByChunks<T>(
129194
initialData: T[] | IDictionary<T>,
130195
chunkSize: number,

‎lib/common/test/unit-tests/helpers.ts‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,4 +1111,129 @@ const test = require("./test");`,
11111111
assert.deepStrictEqual(seen.sink, ["kept"]);
11121112
});
11131113
});
1114+
1115+
describe("getUniqueShortPluginNames", () => {
1116+
const toObject = (names: string[]) =>
1117+
Object.fromEntries(helpers.getUniqueShortPluginNames(names));
1118+
1119+
it("keeps the short name of plugins without a clash", () => {
1120+
assert.deepStrictEqual(
1121+
toObject([
1122+
"@nativescript/core",
1123+
"nativescript-plugin",
1124+
"@scope/my-plugin",
1125+
"9-lives",
1126+
]),
1127+
{
1128+
"@nativescript/core": "core",
1129+
"nativescript-plugin": "nativescript_plugin",
1130+
"@scope/my-plugin": "my_plugin",
1131+
"9-lives": "9_lives",
1132+
},
1133+
);
1134+
});
1135+
1136+
it("qualifies clashing scoped plugins with their scope", () => {
1137+
assert.deepStrictEqual(
1138+
toObject(["@foo/plugin-x", "@bar/plugin-x", "@nativescript/core"]),
1139+
{
1140+
"@foo/plugin-x": "foo_plugin_x",
1141+
"@bar/plugin-x": "bar_plugin_x",
1142+
"@nativescript/core": "core",
1143+
},
1144+
);
1145+
});
1146+
1147+
it("lets an unscoped plugin keep its name when it clashes with a scoped one", () => {
1148+
assert.deepStrictEqual(toObject(["plugin-x", "@foo/plugin-x"]), {
1149+
"plugin-x": "plugin_x",
1150+
"@foo/plugin-x": "foo_plugin_x",
1151+
});
1152+
});
1153+
1154+
it("replaces characters a scope may contain but a name may not", () => {
1155+
assert.deepStrictEqual(toObject(["@my.org~x/plugin", "@a/plugin"]), {
1156+
"@my.org~x/plugin": "my_org_x_plugin",
1157+
"@a/plugin": "a_plugin",
1158+
});
1159+
});
1160+
1161+
it("prefixes clashing names that would start with a digit", () => {
1162+
assert.deepStrictEqual(toObject(["@1password/plugin", "@a/plugin"]), {
1163+
"@1password/plugin": "_1password_plugin",
1164+
"@a/plugin": "a_plugin",
1165+
});
1166+
assert.deepStrictEqual(toObject(["9-lives", "@a/9-lives"]), {
1167+
"9-lives": "_9_lives",
1168+
"@a/9-lives": "a_9_lives",
1169+
});
1170+
});
1171+
1172+
it("fails when two plugins still share a name", () => {
1173+
assert.throws(
1174+
() =>
1175+
helpers.getUniqueShortPluginNames(["@foo/plugin-x", "@foo/plugin_x"]),
1176+
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
1177+
);
1178+
assert.throws(
1179+
() =>
1180+
helpers.getUniqueShortPluginNames([
1181+
"foo-plugin-x",
1182+
"@foo/plugin-x",
1183+
"@bar/plugin-x",
1184+
]),
1185+
/"@foo\/plugin-x" and "foo-plugin-x".*"foo_plugin_x"/,
1186+
);
1187+
});
1188+
1189+
it("does not depend on the order of the plugins", () => {
1190+
const names = ["@foo/plugin-x", "plugin-x", "@bar/plugin-x", "other"];
1191+
const expected = toObject(names);
1192+
assert.deepStrictEqual(toObject([...names].reverse()), expected);
1193+
assert.deepStrictEqual(
1194+
toObject([names[2], names[0], names[3], names[1]]),
1195+
expected,
1196+
);
1197+
assert.throws(
1198+
() =>
1199+
helpers.getUniqueShortPluginNames(["@foo/plugin_x", "@foo/plugin-x"]),
1200+
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
1201+
);
1202+
});
1203+
});
1204+
1205+
describe("getCandidateAarNames", () => {
1206+
it("lists the short and the scope-qualified name of a scoped plugin", () => {
1207+
assert.deepStrictEqual(helpers.getCandidateAarNames("@foo/plugin-x"), [
1208+
"plugin_x",
1209+
"foo_plugin_x",
1210+
]);
1211+
});
1212+
1213+
it("lists the short name of an unscoped plugin", () => {
1214+
assert.deepStrictEqual(helpers.getCandidateAarNames("plugin-x"), [
1215+
"plugin_x",
1216+
]);
1217+
assert.deepStrictEqual(helpers.getCandidateAarNames("9-lives"), [
1218+
"9_lives",
1219+
"_9_lives",
1220+
]);
1221+
});
1222+
1223+
it("covers every name getUniqueShortPluginNames gives", () => {
1224+
const projects = [
1225+
["@foo/plugin-x"],
1226+
["@foo/plugin-x", "@bar/plugin-x"],
1227+
["plugin-x", "@foo/plugin-x"],
1228+
["@1a/plugin", "@b/plugin", "plugin", "9-lives", "@c/9-lives"],
1229+
];
1230+
for (const project of projects) {
1231+
for (const [name, aarName] of helpers.getUniqueShortPluginNames(
1232+
project,
1233+
)) {
1234+
assert.include(helpers.getCandidateAarNames(name), aarName);
1235+
}
1236+
}
1237+
});
1238+
});
11141239
});

‎lib/definitions/android-plugin-migrator.d.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { IProjectDir } from "../common/declarations";
33

44
interface IPluginBuildOptions extends IAndroidBuildOptions {
55
projectDir?: string;
6+
/**
7+
* File name (without extension) of the built aar and of its temp build dir.
8+
* Defaults to the plugin's short name.
9+
*/
10+
aarName?: string;
611
}
712

813
interface IAndroidBuildOptions {
@@ -17,6 +22,11 @@ interface IAndroidBuildOptions {
1722
interface IAndroidPluginBuildService {
1823
buildAar(options: IPluginBuildOptions): Promise<boolean>;
1924
migrateIncludeGradle(options: IPluginBuildOptions): boolean;
25+
/**
26+
* Whether the plugin has sources to build but `aarOutputDir` lacks its aar
27+
* or still holds one the CLI built under another of its candidate names.
28+
*/
29+
hasOutdatedAar(options: IPluginBuildOptions): boolean;
2030
}
2131

2232
/**

0 commit comments

Comments
 (0)