Repository navigation
Protect against runtimePackage.version returning null #5444
Description
Activity
Followed it a bit further, but haven't put any debug in yet.
runtimePackageis determined by callingthis.$projectDataService.getRuntimePackage().So things could be going wrong either in
getRuntimePackage()orgetInstalledRuntimePackage():nativescript-cli/lib/services/project-data-service.ts
Lines 567 to 655 in 6de0c3e
public getRuntimePackage( projectDir: string, platform: constants.SupportedPlatform ): IBasePluginData { platform = platform.toLowerCase() as constants.SupportedPlatform; const packageJson = this.$fs.readJson( path.join(projectDir, constants.PACKAGE_JSON_FILE_NAME) ); const runtimeName = platform === PlatformTypes.android ? constants.TNS_ANDROID_RUNTIME_NAME : constants.TNS_IOS_RUNTIME_NAME; if ( packageJson && packageJson.nativescript && packageJson.nativescript[runtimeName] && packageJson.nativescript[runtimeName].version ) { // if we have a nativescript key with a runtime version in package.json // that means we are dealing with a legacy project, and should respect // that information return { name: runtimeName, version: packageJson.nativescript[runtimeName].version, }; } return this.getInstalledRuntimePackage(projectDir, platform); } private getInstalledRuntimePackage( projectDir: string, platform: constants.SupportedPlatform ): IBasePluginData { const runtimePackage = this.$pluginsService .getDependenciesFromPackageJson(projectDir) .devDependencies.find((d) => { if (platform === constants.PlatformTypes.ios) { return [ constants.SCOPED_IOS_RUNTIME_NAME, constants.TNS_IOS_RUNTIME_NAME, ].includes(d.name); } else if (platform === constants.PlatformTypes.android) { return [ constants.SCOPED_ANDROID_RUNTIME_NAME, constants.TNS_ANDROID_RUNTIME_NAME, ].includes(d.name); } }); if (runtimePackage) { // in case we are using a local tgz for the runtime // if (runtimePackage.version.includes("tgz")) { try { const runtimePackageJsonPath = require.resolve( `${runtimePackage.name}/package.json`, { paths: [projectDir], } ); runtimePackage.version = this.$fs.readJson( runtimePackageJsonPath ).version; } catch (err) { runtimePackage.version = null; } } return runtimePackage; } // default to the scoped runtimes this.$logger.trace( "Could not find an installed runtime, falling back to default runtimes" ); if (platform === constants.PlatformTypes.ios) { return { name: constants.SCOPED_IOS_RUNTIME_NAME, version: null, }; } else if (platform === constants.PlatformTypes.android) { return { name: constants.SCOPED_ANDROID_RUNTIME_NAME, version: null, }; } } Getting closer. It strictly expects the runtimes to be in devDependencies. Why is that?
nativescript-cli/lib/services/project-data-service.ts
Lines 602 to 616 in 6de0c3e
const runtimePackage = this.$pluginsService .getDependenciesFromPackageJson(projectDir) .devDependencies.find((d) => { if (platform === constants.PlatformTypes.ios) { return [ constants.SCOPED_IOS_RUNTIME_NAME, constants.TNS_IOS_RUNTIME_NAME, ].includes(d.name); } else if (platform === constants.PlatformTypes.android) { return [ constants.SCOPED_ANDROID_RUNTIME_NAME, constants.TNS_ANDROID_RUNTIME_NAME, ].includes(d.name); } }); Moving them into devDependencies, my error is now refreshingly different:
Invalid Version: file:../../node_modules/@nativescript/android
This is pretty fragile. Now I see why the plugin seed doesn't hoist dependencies.
Could we not have CLI find the version more robustly, e.g. by doing proper node module resolution and utilising the filesystem instead?
By fixing this, we'd likely become able to hoist the runtime dependencies in monorepos, saving many megabytes for everyone.
Reacted by Rob@shirakaba just stumbled upon this and wanted to react on your point about saving data with monorepo.
you can already "achieve" that with pnpm. I have been using it with N for years and it saves me literally Gb of data! thus saving my hard drive too!Reacted by Jamie Birch
Environment
Provide version numbers for the following components (information can be retrieved by running
tns infoin your project folder or by inspecting thepackage.jsonof the project):tns infoandtns doctorsay "is not installed")Describe the bug
Build-time error, as documented fully, along with my environment setup, here: #4451 (comment)
In a demo app made from the official NativeScript plugin seed, whenever I run
tns plugin add ../../dist/packages/my-cool-pluginon my demo app, I get the nondescript error:... with no stack trace. I determined that this was from the semver package, as it was in the previous issue. Here's the output of a
console.trace()a line before the thrown error:As I document in this comment, I think the root of the problem is that CLI can't find
@nativescript/androideven though I have it installed. Thus,runtimePackagereturns{ name: '@nativescript/android', version: null }here:nativescript-cli/lib/services/plugins-service.ts
Line 805 in 6de0c3e
To Reproduce
From a demo project in the official NativeScript plugin seed, run
tns plugin add ../../dist/packages/my-cool-plugin.I'm not confident that it will reproduce for users with different environments to my own, however.
Expected behavior
Plugin should install successfully and no error should be thrown at all. If an error is thrown, at least a stack trace should be presented. Above all, CLI needs to be able to find
@nativescript/androidwhen it is clearly present innode_modules.Sample project
Any project using the plugin seed.
Additional context