Skip to content

Commit 5a9a505

Browse files
authored
fix(android): give plugins with clashing short names unique aar names (#6217)
The CLI named 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, so @foo/plugin-x and @bar/plugin-x both built plugin_x.aar in the same platforms/tempPlugin/plugin_x, and which one the app linked depended on build order. Every package the CLI builds an aar for (a `nativescript` package whose platforms/android has a manifest or Android sources) now gets an aar name that is unique in the project. A short name no other such package shares is kept, so projects without a clash build exactly as before. Within a clash, scoped plugins get <scope>_<short> (with a leading _ if it would start with a digit) and an unscoped plugin keeps its short name; names that still collide fail prepare. Packages that only ship prebuilt aars aren't renamed and don't count toward a clash. For a plugin the CLI builds, an aar under its old <short> name is always a leftover (earlier CLIs overwrote that file on every build); a scope-qualified one is a leftover only if its temp project was built for this plugin, and prepare stops if it differs from that build. Leftovers are removed only once the new aar is in place, so a failed build keeps the old one.
1 parent 5f4030a commit 5a9a505

8 files changed

Lines changed: 1222 additions & 70 deletions

File tree

‎lib/common/helpers.ts‎

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

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

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

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,4 +1111,143 @@ const test = require("./test");`,
11111111
assert.deepStrictEqual(seen.sink, ["kept"]);
11121112
});
11131113
});
1114+
1115+
describe("getUniqueAarNames", () => {
1116+
const toObject = (names: string[]) =>
1117+
Object.fromEntries(helpers.getUniqueAarNames(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 scope-qualified 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(["@9a/x", "@b/x"]), {
1167+
"@9a/x": "_9a_x",
1168+
"@b/x": "b_x",
1169+
});
1170+
});
1171+
1172+
it("lets an unscoped plugin starting with a digit keep its name in a clash", () => {
1173+
assert.deepStrictEqual(toObject(["9-lives", "@a/9-lives"]), {
1174+
"9-lives": "9_lives",
1175+
"@a/9-lives": "a_9_lives",
1176+
});
1177+
});
1178+
1179+
it("fails when two plugins still share a name", () => {
1180+
assert.throws(
1181+
() => helpers.getUniqueAarNames(["@foo/plugin-x", "@foo/plugin_x"]),
1182+
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
1183+
);
1184+
assert.throws(
1185+
() =>
1186+
helpers.getUniqueAarNames([
1187+
"foo-plugin-x",
1188+
"@foo/plugin-x",
1189+
"@bar/plugin-x",
1190+
]),
1191+
/"@foo\/plugin-x" and "foo-plugin-x".*"foo_plugin_x"/,
1192+
);
1193+
assert.throws(
1194+
() => helpers.getUniqueAarNames(["_9a_x", "@9a/x", "x"]),
1195+
/"@9a\/x" and "_9a_x".*"_9a_x"/,
1196+
);
1197+
});
1198+
1199+
it("does not depend on the order of the plugins", () => {
1200+
const names = ["@foo/plugin-x", "plugin-x", "@bar/plugin-x", "other"];
1201+
const expected = toObject(names);
1202+
assert.deepStrictEqual(toObject([...names].reverse()), expected);
1203+
assert.deepStrictEqual(
1204+
toObject([names[2], names[0], names[3], names[1]]),
1205+
expected,
1206+
);
1207+
assert.throws(
1208+
() => helpers.getUniqueAarNames(["@foo/plugin_x", "@foo/plugin-x"]),
1209+
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
1210+
);
1211+
});
1212+
});
1213+
1214+
describe("getCandidateAarNames", () => {
1215+
it("lists the short and the scope-qualified name of a scoped plugin", () => {
1216+
assert.deepStrictEqual(helpers.getCandidateAarNames("@foo/plugin-x"), [
1217+
"plugin_x",
1218+
"foo_plugin_x",
1219+
]);
1220+
});
1221+
1222+
it("lists the short name of an unscoped plugin", () => {
1223+
assert.deepStrictEqual(helpers.getCandidateAarNames("plugin-x"), [
1224+
"plugin_x",
1225+
]);
1226+
assert.deepStrictEqual(helpers.getCandidateAarNames("9-lives"), [
1227+
"9_lives",
1228+
]);
1229+
});
1230+
1231+
it("prefixes a scope-qualified name that would start with a digit", () => {
1232+
assert.deepStrictEqual(helpers.getCandidateAarNames("@9a/x"), [
1233+
"x",
1234+
"_9a_x",
1235+
]);
1236+
});
1237+
1238+
it("covers every name getUniqueAarNames gives", () => {
1239+
const projects = [
1240+
["@foo/plugin-x"],
1241+
["@foo/plugin-x", "@bar/plugin-x"],
1242+
["plugin-x", "@foo/plugin-x"],
1243+
["@1a/plugin", "@b/plugin", "plugin", "9-lives", "@c/9-lives"],
1244+
["@9a/x", "x"],
1245+
];
1246+
for (const project of projects) {
1247+
for (const [name, aarName] of helpers.getUniqueAarNames(project)) {
1248+
assert.include(helpers.getCandidateAarNames(name), aarName);
1249+
}
1250+
}
1251+
});
1252+
});
11141253
});

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

Lines changed: 15 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,16 @@ interface IAndroidBuildOptions {
1722
interface IAndroidPluginBuildService {
1823
buildAar(options: IPluginBuildOptions): Promise<boolean>;
1924
migrateIncludeGradle(options: IPluginBuildOptions): boolean;
25+
/**
26+
* Whether `buildAar` builds an aar from this platforms/android directory,
27+
* i.e. it has an AndroidManifest.xml or Android source directories.
28+
*/
29+
hasAarSources(platformsAndroidDirPath: string): boolean;
30+
/**
31+
* Whether the plugin has sources to build but `aarOutputDir` lacks its aar
32+
* or still holds one the CLI built under another of its candidate names.
33+
*/
34+
hasOutdatedAar(options: IPluginBuildOptions): boolean;
2035
}
2136

2237
/**

0 commit comments

Comments
 (0)