Skip to content

Commit 1072ba3

Browse files
fix: support running from outside the project with --path (#6182)
* fix: don't pass the CLI's own options to package managers ensureAllDependenciesAreInstalled passes path, frameworkPath, disableNpmInstall and ignoreScripts as install options, and getFlagsString turned them into flags. npm 12 fails on unknown flags, so ns commands that install with --path, like ns debug --path app --force, stopped with EUNKNOWNCONFIG. * 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. * fix(ios): use the folders of a NativeSource glob's headers as search paths The glob itself ended up in HEADER_SEARCH_PATHS, where it matches nothing, relative to the Xcode project rather than the app. Folder paths now use the path resolved against the project for the search path and for the module map check as well. * test: remove the NativeSource tests' temp folders * 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. * fix(ios): don't nest absolute NativeSource glob matches under the project fast-glob returns absolute matches for an absolute pattern, and joining them onto the project folder produced paths like /proj/proj/src/File.swift. Resolving them against the project keeps absolute matches as they are. * 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. * refactor(pnpm): drop option deletes covered by CLI_ONLY_OPTIONS getFlagsString already skips ignoreScripts, path and frameworkPath for every package manager. * test: cover --ignore-scripts installs for npm, yarn and bun Also give the test package manager the base constructor's five arguments, so it is really npm rather than an object. --------- Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
1 parent c7e0c5b commit 1072ba3

5 files changed

Lines changed: 313 additions & 27 deletions

File tree

‎lib/base-package-manager.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import {
1313
IHostInfo,
1414
} from "./common/declarations";
1515

16+
// Options for the CLI itself. npm 12 fails on flags it doesn't know.
17+
const CLI_ONLY_OPTIONS = [
18+
"path",
19+
"frameworkPath",
20+
"disableNpmInstall",
21+
"ignoreScripts",
22+
];
23+
1624
export abstract class BasePackageManager implements INodePackageManager {
1725
public abstract install(
1826
packageName: string,
@@ -136,6 +144,10 @@ export abstract class BasePackageManager implements INodePackageManager {
136144
protected getFlagsString(config: any, asArray: boolean): any {
137145
const array: Array<string> = [];
138146
for (const flag in config) {
147+
if (CLI_ONLY_OPTIONS.includes(flag)) {
148+
continue;
149+
}
150+
139151
if (
140152
flag === "global" &&
141153
this.packageManager !== "yarn" &&

‎lib/pnpm-package-manager.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ export class PnpmPackageManager extends BasePackageManager {
4444
if (config.ignoreScripts) {
4545
config["ignore-scripts"] = true;
4646
}
47-
// CLI-internal options must never reach the command line: pnpm, unlike
48-
// npm, hard-fails on unknown options.
49-
delete config.ignoreScripts;
50-
delete config.path;
51-
delete config.frameworkPath;
5247

5348
const packageJsonPath = path.join(pathToSave, "package.json");
5449
const jsonContentBefore = this.$fs.readJson(packageJsonPath);

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

Lines changed: 54 additions & 22 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
@@ -1543,17 +1547,38 @@ export class IOSProjectService
15431547
filesRelativeToProject: true,
15441548
target: project.getFirstTarget()?.uuid,
15451549
});
1546-
project.addToHeaderSearchPaths(group.path);
1547-
const headerFiles = this.$fs.exists(sourceFolderPath)
1548-
? this.$fs.enumerateFilesInDirectorySync(
1549-
sourceFolderPath,
1550-
(file, stat) => stat.isDirectory() || path.extname(file) === ".h",
1551-
)
1552-
: [];
1553-
if (
1554-
headerFiles.length > 0 &&
1555-
!this.$fs.exists(path.join(sourceFolderPath, "module.modulemap"))
1556-
) {
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+
}
1575+
headerSearchPaths.forEach((searchPath) =>
1576+
project.addToHeaderSearchPaths(searchPath),
1577+
);
1578+
const hasModuleMap = headerSearchPaths.some((searchPath) =>
1579+
this.$fs.exists(path.join(searchPath, "module.modulemap")),
1580+
);
1581+
if (headerFiles.length > 0 && !hasModuleMap) {
15571582
this.$logger.warn(
15581583
`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`,
15591584
);
@@ -1597,27 +1622,33 @@ export class IOSProjectService
15971622
}
15981623
}
15991624

1600-
private async getRootGroup(name: string, rootPath: string) {
1625+
private async getRootGroup(
1626+
name: string,
1627+
rootPath: string,
1628+
projectRoot: string,
1629+
) {
16011630
const filePathsArr: string[] = [];
16021631
const rootGroup: INativeSourceCodeGroup = {
16031632
name: name,
16041633
files: filePathsArr,
1634+
// nativescript-dev-xcode looks the group up by this exact string. Keep it
1635+
// as configured: resolving it duplicates the group in prepared projects.
16051636
path: rootPath,
16061637
};
16071638

16081639
if (fastGlob.isDynamicPattern(rootPath)) {
1609-
const projectRoot = this.$projectDataService.getProjectData().projectDir;
1610-
const filePaths = await fastGlob(rootPath);
1640+
// Relative to the project: --path can run the CLI from anywhere.
1641+
const filePaths = await fastGlob(rootPath, { cwd: projectRoot });
16111642
for (const filePath of filePaths) {
1612-
const sourceFilePath = path.normalize(path.join(projectRoot, filePath));
1613-
filePathsArr.push(sourceFilePath);
1643+
filePathsArr.push(path.resolve(projectRoot, filePath));
16141644
}
16151645
} 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) => {
1620-
const filePath = path.join(rootGroup.path, fileName);
1646+
const folderPath = path.resolve(projectRoot, rootPath);
1647+
if (this.$fs.exists(folderPath)) {
1648+
const stats = this.$fs.getFsStats(folderPath);
1649+
if (stats.isDirectory() && !this.$fs.isEmptyDir(folderPath)) {
1650+
this.$fs.readDirectory(folderPath).forEach((fileName) => {
1651+
const filePath = path.join(folderPath, fileName);
16211652
filePathsArr.push(filePath);
16221653
});
16231654
}
@@ -1684,6 +1715,7 @@ export class IOSProjectService
16841715
const group = await this.getRootGroup(
16851716
pluginData.name,
16861717
pluginPlatformsFolderPath,
1718+
projectData.projectDir,
16871719
);
16881720
project.removePbxGroup(group.name, group.path);
16891721
project.removeFromHeaderSearchPaths(group.path);

‎test/base-package-manager.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { assert } from "chai";
2+
import { BasePackageManager } from "../lib/base-package-manager";
3+
import { BunPackageManager } from "../lib/bun-package-manager";
4+
import { IInjector } from "../lib/common/definitions/yok";
5+
import { Yok } from "../lib/common/yok";
6+
import { NodePackageManager } from "../lib/node-package-manager";
7+
import { YarnPackageManager } from "../lib/yarn-package-manager";
8+
import * as stubs from "./stubs";
9+
10+
class TestPackageManager extends (<any>BasePackageManager) {
11+
constructor() {
12+
super({}, {}, {}, {}, "npm");
13+
}
14+
}
15+
16+
describe("BasePackageManager", () => {
17+
const flags = (config: object): string[] =>
18+
(<any>new TestPackageManager()).getFlagsString(config, true);
19+
20+
it("doesn't pass the CLI's own options to the package manager", () => {
21+
assert.deepEqual(
22+
flags({
23+
path: "apps/app",
24+
frameworkPath: "runtime.tgz",
25+
disableNpmInstall: true,
26+
ignoreScripts: true,
27+
}),
28+
[],
29+
);
30+
});
31+
32+
it("passes the package manager's options on", () => {
33+
assert.deepEqual(flags({ "ignore-scripts": true, "save-dev": true }), [
34+
"--ignore-scripts",
35+
"--save-dev",
36+
]);
37+
});
38+
39+
[
40+
{ name: "npm", packageManager: NodePackageManager },
41+
{ name: "yarn", packageManager: YarnPackageManager },
42+
{ name: "bun", packageManager: BunPackageManager },
43+
].forEach(({ name, packageManager }) => {
44+
it(`maps ignoreScripts to --ignore-scripts when installing with ${name}`, async () => {
45+
const injector: IInjector = new Yok();
46+
injector.register("hostInfo", { isWindows: false });
47+
injector.register("errors", stubs.ErrorsStub);
48+
injector.register("logger", stubs.LoggerStub);
49+
injector.register("childProcess", stubs.ChildProcessStub);
50+
injector.register("httpClient", {});
51+
injector.register("fs", stubs.FileSystemStub);
52+
injector.register("pacoteService", {});
53+
injector.register(name, packageManager);
54+
const projectDir = "/some-project";
55+
56+
await injector
57+
.resolve<BasePackageManager>(name)
58+
.install(projectDir, projectDir, <any>{
59+
ignoreScripts: true,
60+
path: "apps/app",
61+
frameworkPath: "runtime.tgz",
62+
});
63+
64+
const args =
65+
injector.resolve<stubs.ChildProcessStub>(
66+
"childProcess",
67+
).lastCommandArgs;
68+
assert.include(args, "--ignore-scripts");
69+
assert.notInclude(args, "--ignoreScripts");
70+
assert.notInclude(args, "--path");
71+
assert.notInclude(args, "--frameworkPath");
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)