apache / cordova-fetch

Apache Cordova Fetch Library
https://cordova.apache.org/
Apache License 2.0
27 stars 27 forks source link

Cannot determine package name from spec git+https://github.com... #85

Closed liuxiaoy closed 4 years ago

liuxiaoy commented 4 years ago

Bug Report

Problem

fetch repeat the plugin whose repo like git+https://xxx when exec npm install && cordova prepare

What is expected to happen?

the plugin has been download when npm install no need to download it when cordova prepare

What does actually happen?

Discovered saved plugin "org.wbt11a.Canvas2ImagePlugin". Adding it to the project No scripts found for hook "before_plugin_add". Calling plugman.fetch on plugin "git+https://github.com/wbt11a/Canvas2ImagePlugin.git#6195584c2b" fetch: Installing git+https://github.com/wbt11a/Canvas2ImagePlugin.git#6195584c2b to /Users/me/app

Information

git clone a cordova app project npm install && cordova prepare

cordova-fetch.js

return pathToInstalledPackage(target, dest)
                .catch(_ => installPackage(target, dest, opts));
function pathToInstalledPackage (spec, dest) {
    return Promise.resolve().then(_ => {
        const { name, rawSpec } = npa(spec, dest);
        if (!name) {
            throw new CordovaError(`Cannot determine package name from spec ${spec}`);
        }
        return resolvePathToPackage(name, dest)
            .then(([pkgPath, { version }]) => {
                if (!semver.satisfies(version, rawSpec)) {
                    throw new CordovaError(`Installed package ${name}@${version} does not satisfy ${name}@${rawSpec}`);
                }
                return pkgPath;
            });
    });
}

npa could't resolve the plugin name Cannot determine package name from spec git+https://github.com...

Command or Code

Environment, Platform, Device

Version information

cordova@9.0.0 cordova-fetch@2.0.1

Checklist

timbru31 commented 4 years ago

Is git+ even still valid? I remember npm had some issue with it a time ago. Anyways, installing from Git just works like this: cordova plugin add https://github.com/wbt11a/Canvas2ImagePlugin.git\#6195584c2b

dpogue commented 4 years ago

I've fought variations of this for years, with most reliable format to install from github is using the same github: syntax as npm:

cordova plugin add github:wbt11a/Canvas2ImagePlugin#6195584c2b
liuxiaoy commented 4 years ago

thanks @timbru31 @dpogue. Npm install support github, https and git all. but this not my problem.

My problem is npa(npm-package-arg) won't resolve name of the plugin whose repo like https://,git://,GitHub:.So cordova-fetch will exec npm to install it, but it exists already.

breautek commented 4 years ago

@timbru31 NPM automatically converts git urls to git+

I've failed to reproduce this on:

9.0.0 (cordova-lib@9.0.1)
cordova-fetch@2.0.1

My output:

norman@norman-ThinkPad:/development/cordova/tests/android29$ cordova plugin add https://github.com/wbt11a/Canvas2ImagePlugin.git#6195584c2b
Installing "org.wbt11a.Canvas2ImagePlugin" for android
Adding org.wbt11a.Canvas2ImagePlugin to package.json
norman@norman-ThinkPad:/development/cordova/tests/android29$ cordova plugin add https://github.com/wbt11a/Canvas2ImagePlugin.git#6195584c2b
Plugin "org.wbt11a.Canvas2ImagePlugin" already installed on android.
Adding org.wbt11a.Canvas2ImagePlugin to package.json
norman@norman-ThinkPad:/development/cordova/tests/android29$ npm install && cordova prepare
npm WARN ajv-keywords@2.1.1 requires a peer of ajv@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN com.breautek.api29@1.0.0 No repository field.

audited 151 packages in 2.221s
found 1 low severity vulnerability
  run `npm audit fix` to fix them, or `npm audit` for details

norman@norman-ThinkPad:/development/cordova/tests/android29$ rm -rf node_modules/
norman@norman-ThinkPad:/development/cordova/tests/android29$ npm install && cordova prepare
npm WARN com.breautek.api29@1.0.0 No repository field.

added 402 packages from 282 contributors and audited 151 packages in 7.15s
found 1 low severity vulnerability
  run `npm audit fix` to fix them, or `npm audit` for details

I'm using Node 12.15.0 & NPM 6.13.4.

Are you able to provide a sample minimal reproduction app?

liuxiaoy commented 4 years ago

@breautek

rm -r platforms plugins

before exec "cordova prepare"

no need "rm -rf node_modules/"

breautek commented 4 years ago

Strange... when I do that, I'm seeing a completely different error...

norman@norman-ThinkPad:/development/cordova/tests/android29$ npm install && cordova prepare
npm WARN com.breautek.api29@1.0.0 No repository field.

added 56 packages from 39 contributors and audited 68 packages in 4.177s

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Discovered platform "android@8.1.0" in config.xml or package.json. Adding it to the project
Using cordova-fetch for cordova-android@8.1.0
Adding android project...
Creating Cordova project for the Android platform:
    Path: platforms/android
    Package: com.breautek.api29
    Name: HelloCordova
    Activity: MainActivity
    Android target: android-28
Subproject Path: CordovaLib
Subproject Path: app
Android project created with cordova-android@8.1.0
Discovered saved plugin "cordova-plugin-whitelist". Adding it to the project
Installing "cordova-plugin-whitelist" for android
Discovered saved plugin "org.wbt11a.Canvas2ImagePlugin". Adding it to the project
Failed to restore plugin "org.wbt11a.Canvas2ImagePlugin" from config.xml. You might need to try adding it again. Error: Error: npm: Command failed with exit code 1 Error output:
npm ERR! code E404
npm ERR! 404 'org.wbt11a.Canvas2ImagePlugin' is not in the npm registry.
npm ERR! 404 name can no longer contain capital letters
npm ERR! 404 
npm ERR! 404  'org.wbt11a.Canvas2ImagePlugin@latest' is not in the npm registry.
npm ERR! 404 Your package name is not valid, because 
npm ERR! 404  1. name can no longer contain capital letters
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/norman/.npm/_logs/2020-04-13T13_29_53_098Z-debug.log

For me it looks like it's attempting to install by name (so for me I think it is determining the package name properly), but then it fails because it's not an actual NPM package because well, it should be installing from git... not from the NPM repository.

The relevant parts of my package.json file looks like:

"dependencies": {
    "canvas2imageplugin": "git+https://github.com/wbt11a/Canvas2ImagePlugin.git#6195584c2b",
    "cordova-android": "8.1.0"
  },
  "devDependencies": {
    "cordova-plugin-whitelist": "^1.3.4"
  },
  "cordova": {
    "plugins": {
      "cordova-plugin-whitelist": {},
      "org.wbt11a.Canvas2ImagePlugin": {}
    },
    "platforms": [
      "android"
    ]
  }
liuxiaoy commented 4 years ago

@breautek It loos like cordova is executing npm install org.wbt11a.Canvas2ImagePlugin. But this plugin should has been download by npm install. This step is unnecessary.

To fix this question above,use

"dependencies": {
    "org.wbt11a.Canvas2ImagePlugin": "git+https://github.com/wbt11a/Canvas2ImagePlugin.git#6195584c2b"
  },
raphinesse commented 4 years ago

@liuxiaoy I am 90% sure that the weird Error that @breautek (and myself) observed when trying to prepare a project with the plugin, stems from the fact that the ID in the plugin.xml (org.wbt11a.Canvas2ImagePlugin) differs from the name in package.json (canvas2imageplugin). I don't know if we support this, but I think we should not.

Anyway, this does not seem to be what this issue is about. You write

the plugin has been download when npm install no need to download it when cordova prepare

Unfortunately, the package caching done by cordova-fetch only works for packages referenced by name and version. cordova-fetch will always download a package when it is given an URL. That is because we cannot translate an URL into a package name to check if we already have the package installed.

If I understood you correctly, I would say everything works as intended. Thus I am closing this issue. If you feel that I misunderstood the issue, please provide additional detail and clear step by step instructions to reproduce your issue.

liuxiaoy commented 4 years ago

@raphinesse Yes, my problem is “cordova-fetch will always download a package when it is given an URL”.

So "cordova plugin add cordova-plugin-camera@2.0.0" differs from "cordova plugin add https://github.com/apache/cordova-plugin-camera.git#2.0.0". Maybe it would be better to show this difference On the official guide website.

liuxiaoy commented 4 years ago

@raphinesse Or Maybe we can translate an URL into a package name by the package installed.I'll have a try.