Skip to content

Commit 43270f0

Browse files
committed
fix(ios): keep the configured NativeSource path as the pbxgroup key
nativescript-dev-xcode finds an existing group by the exact (name, path) stored in .ns-build-pbxgroup-data.json. A relative ios.NativeSource folder was stored as written, so passing the resolved absolute path added a second group on the first prepare after upgrading, with every source compiled twice. The configured path stays the key; the files and header search path still use the path resolved against the project.
1 parent 3d737b2 commit 43270f0

2 files changed

Lines changed: 63 additions & 34 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ export class IOSProjectService
15371537
sourceFolderPath,
15381538
projectData.projectDir,
15391539
);
1540+
const sourceFolder = path.resolve(projectData.projectDir, sourceFolderPath);
15401541
// pin the sources to the main app target: without an explicit target the
15411542
// underlying xcode lib picks whichever "Sources" build phase it finds
15421543
// first, which can be an extension target (e.g. a widget) once one
@@ -1554,19 +1555,19 @@ export class IOSProjectService
15541555
.filter((file) => path.extname(file) === ".h")
15551556
.map((file) => path.dirname(file)),
15561557
)
1557-
: [group.path];
1558+
: [sourceFolder];
15581559
headerSearchPaths.forEach((searchPath) =>
15591560
project.addToHeaderSearchPaths(searchPath),
15601561
);
1561-
const headerFiles = this.$fs.exists(group.path)
1562+
const headerFiles = this.$fs.exists(sourceFolder)
15621563
? this.$fs.enumerateFilesInDirectorySync(
1563-
group.path,
1564+
sourceFolder,
15641565
(file, stat) => stat.isDirectory() || path.extname(file) === ".h",
15651566
)
15661567
: [];
15671568
if (
15681569
headerFiles.length > 0 &&
1569-
!this.$fs.exists(path.join(group.path, "module.modulemap"))
1570+
!this.$fs.exists(path.join(sourceFolder, "module.modulemap"))
15701571
) {
15711572
this.$logger.warn(
15721573
`warning: Directory ${sourceFolderPath} with native iOS source code doesn't contain a modulemap file. Metadata for it will not be generated and it will not be accessible from JavaScript. To learn more see https://docs.nativescript.org/guides/ios-source-code`,
@@ -1620,6 +1621,8 @@ export class IOSProjectService
16201621
const rootGroup: INativeSourceCodeGroup = {
16211622
name: name,
16221623
files: filePathsArr,
1624+
// nativescript-dev-xcode looks the group up by this exact string. Keep it
1625+
// as configured: resolving it duplicates the group in prepared projects.
16231626
path: rootPath,
16241627
};
16251628

@@ -1632,12 +1635,11 @@ export class IOSProjectService
16321635
}
16331636
} else {
16341637
const folderPath = path.resolve(projectRoot, rootPath);
1635-
rootGroup.path = folderPath;
16361638
if (this.$fs.exists(folderPath)) {
16371639
const stats = this.$fs.getFsStats(folderPath);
16381640
if (stats.isDirectory() && !this.$fs.isEmptyDir(folderPath)) {
16391641
this.$fs.readDirectory(folderPath).forEach((fileName) => {
1640-
const filePath = path.join(rootGroup.path, fileName);
1642+
const filePath = path.join(folderPath, fileName);
16411643
filePathsArr.push(filePath);
16421644
});
16431645
}

‎test/ios-project-service-native-source.ts‎

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ describe("iOSProjectService getRootGroup", () => {
2828
});
2929

3030
const getFiles = async (rootPath: string): Promise<string[]> => {
31-
const { files } = await (<any>IOSProjectService.prototype).getRootGroup.call(
31+
const proto = <any>IOSProjectService.prototype;
32+
const { files } = await proto.getRootGroup.call(
3233
{ $fs: new FileSystem(<any>{}) },
3334
"Native",
3435
rootPath,
@@ -50,47 +51,73 @@ describe("iOSProjectService getRootGroup", () => {
5051
assert.deepEqual(await getFiles("../../libs/native"), [expectedSource()]);
5152
});
5253

53-
describe("header search paths", () => {
54-
const prepare = async (rootPath: string): Promise<string[]> => {
55-
const searchPaths: string[] = [];
56-
const proto = <any>IOSProjectService.prototype;
57-
await proto.prepareNativeSourceCode.call(
58-
{
59-
$fs: new FileSystem(<any>{}),
60-
$logger: { warn: () => {} },
61-
getRootGroup: proto.getRootGroup,
62-
savePbxProj: () => {},
63-
createPbxProj: () => ({
64-
addPbxGroup: () => {},
65-
getFirstTarget: () => ({ uuid: "target" }),
66-
addToHeaderSearchPaths: (searchPath: string) =>
67-
searchPaths.push(searchPath),
68-
}),
69-
},
70-
"Native",
71-
rootPath,
72-
{ projectDir },
73-
);
74-
return searchPaths.map((searchPath) => fs.realpathSync(searchPath));
54+
const prepare = async (rootPath: string) => {
55+
const groupPaths: string[] = [];
56+
const groupFiles: string[] = [];
57+
const searchPaths: string[] = [];
58+
const proto = <any>IOSProjectService.prototype;
59+
await proto.prepareNativeSourceCode.call(
60+
{
61+
$fs: new FileSystem(<any>{}),
62+
$logger: { warn: () => {} },
63+
getRootGroup: proto.getRootGroup,
64+
savePbxProj: () => {},
65+
createPbxProj: () => ({
66+
addPbxGroup: (files: string[], name: string, groupPath: string) => {
67+
groupPaths.push(groupPath);
68+
groupFiles.push(...files);
69+
},
70+
getFirstTarget: () => ({ uuid: "target" }),
71+
addToHeaderSearchPaths: (searchPath: string) =>
72+
searchPaths.push(searchPath),
73+
}),
74+
},
75+
"Native",
76+
rootPath,
77+
{ projectDir },
78+
);
79+
return {
80+
groupPaths,
81+
groupFiles: groupFiles.map((file) => fs.realpathSync(file)),
82+
searchPaths: searchPaths.map((searchPath) => fs.realpathSync(searchPath)),
7583
};
84+
};
85+
86+
describe("pbxgroup", () => {
87+
[".", path.join("apps", "app")].forEach((workingDir) => {
88+
it(`keeps the configured folder as its key, run from ${workingDir}`, async () => {
89+
process.chdir(path.join(root, workingDir));
90+
91+
const { groupPaths, groupFiles } = await prepare("../../libs/native");
92+
93+
assert.deepEqual(groupPaths, ["../../libs/native"]);
94+
assert.deepEqual(groupFiles, [expectedSource()]);
95+
});
96+
});
97+
});
98+
99+
describe("header search paths", () => {
100+
const searchPathsFor = async (rootPath: string) =>
101+
(await prepare(rootPath)).searchPaths;
76102

77103
it("uses the folders of the headers a glob matches", async () => {
78104
const nested = path.join(sourcesDir, "nested");
79105
fs.mkdirSync(nested);
80106
fs.writeFileSync(path.join(nested, "One.h"), "");
81107
fs.writeFileSync(path.join(nested, "Two.h"), "");
82108

83-
assert.deepEqual(await prepare("../../libs/native/**/*.{h,swift}"), [
84-
fs.realpathSync(nested),
85-
]);
109+
assert.deepEqual(
110+
await searchPathsFor("../../libs/native/**/*.{h,swift}"),
111+
[fs.realpathSync(nested)],
112+
);
86113
});
87114

88115
it("adds nothing for a glob without headers", async () => {
89-
assert.deepEqual(await prepare("../../libs/native/*.swift"), []);
116+
assert.deepEqual(await searchPathsFor("../../libs/native/*.swift"), []);
90117
});
91118

92119
it("uses the folder itself, against the project", async () => {
93-
assert.deepEqual(await prepare("../../libs/native"), [
120+
assert.deepEqual(await searchPathsFor("../../libs/native"), [
94121
fs.realpathSync(sourcesDir),
95122
]);
96123
});

0 commit comments

Comments
 (0)