Skip to content

Commit c6e10a7

Browse files
committed
fix(ios): look for a NativeSource glob's module map next to its headers
For a glob, the module map check looked inside the pattern itself, which never exists, so headers without a module map went unreported. The check now looks in the header search paths, and the folder of any module.modulemap the glob matches is added to them.
1 parent 564f570 commit c6e10a7

2 files changed

Lines changed: 72 additions & 20 deletions

File tree

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,6 @@ export class IOSProjectService
15371537
sourceFolderPath,
15381538
projectData.projectDir,
15391539
);
1540-
const sourceFolder = path.resolve(projectData.projectDir, sourceFolderPath);
15411540
// pin the sources to the main app target: without an explicit target the
15421541
// underlying xcode lib picks whichever "Sources" build phase it finds
15431542
// first, which can be an extension target (e.g. a widget) once one
@@ -1548,27 +1547,38 @@ export class IOSProjectService
15481547
filesRelativeToProject: true,
15491548
target: project.getFirstTarget()?.uuid,
15501549
});
1551-
// A glob is no use as a search path, the folders of its headers are.
1552-
const headerSearchPaths = fastGlob.isDynamicPattern(sourceFolderPath)
1553-
? _.uniq(
1554-
group.files
1555-
.filter((file) => path.extname(file) === ".h")
1556-
.map((file) => path.dirname(file)),
1557-
)
1558-
: [sourceFolder];
1550+
let headerFiles: string[];
1551+
let headerSearchPaths: string[];
1552+
if (fastGlob.isDynamicPattern(sourceFolderPath)) {
1553+
// A glob is no use as a search path, the folders of its headers and
1554+
// module maps are.
1555+
headerFiles = group.files.filter((file) => path.extname(file) === ".h");
1556+
const moduleMaps = group.files.filter(
1557+
(file) => path.basename(file) === "module.modulemap",
1558+
);
1559+
headerSearchPaths = _.uniq(
1560+
headerFiles.concat(moduleMaps).map((file) => path.dirname(file)),
1561+
);
1562+
} else {
1563+
const sourceFolder = path.resolve(
1564+
projectData.projectDir,
1565+
sourceFolderPath,
1566+
);
1567+
headerFiles = this.$fs.exists(sourceFolder)
1568+
? this.$fs.enumerateFilesInDirectorySync(
1569+
sourceFolder,
1570+
(file, stat) => stat.isDirectory() || path.extname(file) === ".h",
1571+
)
1572+
: [];
1573+
headerSearchPaths = [sourceFolder];
1574+
}
15591575
headerSearchPaths.forEach((searchPath) =>
15601576
project.addToHeaderSearchPaths(searchPath),
15611577
);
1562-
const headerFiles = this.$fs.exists(sourceFolder)
1563-
? this.$fs.enumerateFilesInDirectorySync(
1564-
sourceFolder,
1565-
(file, stat) => stat.isDirectory() || path.extname(file) === ".h",
1566-
)
1567-
: [];
1568-
if (
1569-
headerFiles.length > 0 &&
1570-
!this.$fs.exists(path.join(sourceFolder, "module.modulemap"))
1571-
) {
1578+
const hasModuleMap = headerSearchPaths.some((searchPath) =>
1579+
this.$fs.exists(path.join(searchPath, "module.modulemap")),
1580+
);
1581+
if (headerFiles.length > 0 && !hasModuleMap) {
15721582
this.$logger.warn(
15731583
`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`,
15741584
);

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ describe("iOSProjectService getRootGroup", () => {
6161
const groupPaths: string[] = [];
6262
const groupFiles: string[] = [];
6363
const searchPaths: string[] = [];
64+
const warnings: string[] = [];
6465
const proto = <any>IOSProjectService.prototype;
6566
await proto.prepareNativeSourceCode.call(
6667
{
6768
$fs: new FileSystem(<any>{}),
68-
$logger: { warn: () => {} },
69+
$logger: { warn: (message: string) => warnings.push(message) },
6970
getRootGroup: proto.getRootGroup,
7071
savePbxProj: () => {},
7172
createPbxProj: () => ({
@@ -86,6 +87,7 @@ describe("iOSProjectService getRootGroup", () => {
8687
groupPaths,
8788
groupFiles: groupFiles.map((file) => fs.realpathSync(file)),
8889
searchPaths: searchPaths.map((searchPath) => fs.realpathSync(searchPath)),
90+
warnings,
8991
};
9092
};
9193

@@ -128,4 +130,44 @@ describe("iOSProjectService getRootGroup", () => {
128130
]);
129131
});
130132
});
133+
134+
describe("module map", () => {
135+
let headers: string;
136+
137+
beforeEach(() => {
138+
headers = path.join(sourcesDir, "headers");
139+
fs.mkdirSync(headers);
140+
fs.writeFileSync(path.join(headers, "Header.h"), "");
141+
});
142+
143+
it("warns when the headers a glob matches have no module map", async () => {
144+
const { warnings } = await prepare("../../libs/native/**/*.h");
145+
146+
assert.lengthOf(warnings, 1);
147+
});
148+
149+
it("finds a module map next to the headers a glob matches", async () => {
150+
fs.writeFileSync(path.join(headers, "module.modulemap"), "");
151+
152+
const { warnings } = await prepare("../../libs/native/**/*.h");
153+
154+
assert.deepEqual(warnings, []);
155+
});
156+
157+
it("adds the folder of a module map a glob matches to the search paths", async () => {
158+
const modules = path.join(sourcesDir, "modules");
159+
fs.mkdirSync(modules);
160+
fs.writeFileSync(path.join(modules, "module.modulemap"), "");
161+
162+
const { searchPaths, warnings } = await prepare(
163+
"../../libs/native/**/*.{h,modulemap}",
164+
);
165+
166+
assert.deepEqual(searchPaths, [
167+
fs.realpathSync(headers),
168+
fs.realpathSync(modules),
169+
]);
170+
assert.deepEqual(warnings, []);
171+
});
172+
});
131173
});

0 commit comments

Comments
 (0)