Skip to content

Commit f23e658

Browse files
committed
fix(ios): resolve NativeSource paths against the project
They were resolved against the working directory, so running from outside the project with --path found no files and left the group empty, and the build failed on the missing types.
1 parent 23c1020 commit f23e658

2 files changed

Lines changed: 69 additions & 8 deletions

File tree

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,11 @@ export class IOSProjectService
15321532
projectData: IProjectData,
15331533
): Promise<void> {
15341534
const project = this.createPbxProj(projectData);
1535-
const group = await this.getRootGroup(groupName, sourceFolderPath);
1535+
const group = await this.getRootGroup(
1536+
groupName,
1537+
sourceFolderPath,
1538+
projectData.projectDir,
1539+
);
15361540
// pin the sources to the main app target: without an explicit target the
15371541
// underlying xcode lib picks whichever "Sources" build phase it finds
15381542
// first, which can be an extension target (e.g. a widget) once one
@@ -1597,7 +1601,11 @@ export class IOSProjectService
15971601
}
15981602
}
15991603

1600-
private async getRootGroup(name: string, rootPath: string) {
1604+
private async getRootGroup(
1605+
name: string,
1606+
rootPath: string,
1607+
projectRoot: string,
1608+
) {
16011609
const filePathsArr: string[] = [];
16021610
const rootGroup: INativeSourceCodeGroup = {
16031611
name: name,
@@ -1606,17 +1614,19 @@ export class IOSProjectService
16061614
};
16071615

16081616
if (fastGlob.isDynamicPattern(rootPath)) {
1609-
const projectRoot = this.$projectDataService.getProjectData().projectDir;
1610-
const filePaths = await fastGlob(rootPath);
1617+
// Relative to the project: --path can run the CLI from anywhere.
1618+
const filePaths = await fastGlob(rootPath, { cwd: projectRoot });
16111619
for (const filePath of filePaths) {
16121620
const sourceFilePath = path.normalize(path.join(projectRoot, filePath));
16131621
filePathsArr.push(sourceFilePath);
16141622
}
16151623
} else {
1616-
if (this.$fs.exists(rootPath)) {
1617-
const stats = this.$fs.getFsStats(rootPath);
1618-
if (stats.isDirectory() && !this.$fs.isEmptyDir(rootPath)) {
1619-
this.$fs.readDirectory(rootPath).forEach((fileName) => {
1624+
const folderPath = path.resolve(projectRoot, rootPath);
1625+
rootGroup.path = folderPath;
1626+
if (this.$fs.exists(folderPath)) {
1627+
const stats = this.$fs.getFsStats(folderPath);
1628+
if (stats.isDirectory() && !this.$fs.isEmptyDir(folderPath)) {
1629+
this.$fs.readDirectory(folderPath).forEach((fileName) => {
16201630
const filePath = path.join(rootGroup.path, fileName);
16211631
filePathsArr.push(filePath);
16221632
});
@@ -1684,6 +1694,7 @@ export class IOSProjectService
16841694
const group = await this.getRootGroup(
16851695
pluginData.name,
16861696
pluginPlatformsFolderPath,
1697+
projectData.projectDir,
16871698
);
16881699
project.removePbxGroup(group.name, group.path);
16891700
project.removeFromHeaderSearchPaths(group.path);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { assert } from "chai";
2+
import * as fs from "fs";
3+
import * as os from "os";
4+
import * as path from "path";
5+
import { FileSystem } from "../lib/common/file-system";
6+
import { IOSProjectService } from "../lib/services/ios-project-service";
7+
8+
describe("iOSProjectService getRootGroup", () => {
9+
let projectDir: string;
10+
let sourcesDir: string;
11+
let cwd: string;
12+
13+
beforeEach(() => {
14+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "native-source-"));
15+
projectDir = path.join(root, "apps", "app");
16+
sourcesDir = path.join(root, "libs", "native");
17+
fs.mkdirSync(projectDir, { recursive: true });
18+
fs.mkdirSync(sourcesDir, { recursive: true });
19+
fs.writeFileSync(path.join(sourcesDir, "Source.swift"), "");
20+
cwd = process.cwd();
21+
process.chdir(root);
22+
});
23+
24+
afterEach(() => {
25+
process.chdir(cwd);
26+
});
27+
28+
const getFiles = async (rootPath: string): Promise<string[]> => {
29+
const { files } = await (<any>IOSProjectService.prototype).getRootGroup.call(
30+
{ $fs: new FileSystem(<any>{}) },
31+
"Native",
32+
rootPath,
33+
projectDir,
34+
);
35+
return files.map((file: string) => fs.realpathSync(file));
36+
};
37+
38+
const expectedSource = () =>
39+
fs.realpathSync(path.join(sourcesDir, "Source.swift"));
40+
41+
it("resolves a glob against the project, not the working directory", async () => {
42+
assert.deepEqual(await getFiles("../../libs/native/*.swift"), [
43+
expectedSource(),
44+
]);
45+
});
46+
47+
it("resolves a folder against the project, not the working directory", async () => {
48+
assert.deepEqual(await getFiles("../../libs/native"), [expectedSource()]);
49+
});
50+
});

0 commit comments

Comments
 (0)