apache / cordova-fetch

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

Private repository and UNMET DEPENDENCY #53

Closed Jule- closed 6 years ago

Jule- commented 6 years ago

I ran into an issue which was hard to figured it out so I want to share it for others. Maybe something should be written somewhere in the documentation of cordova-fetch or cordova-cli or even better this should be consolidated.

My use case:

I develop a cordova plugin for my app in a bitbucket private repository and obviously I want to cordova plugin add it in my project.

The issue:

First time I add my plugin on working project with the command:

$ cordova plugin add git+ssh://git@bitbucket.org/myteam/cordova-plugin-my-plugin.git

All goes fine (nb: did not investigate why it goes fine on the first time). Problems happen when I was trying to start a fresh clone of my project.

$ cordova prepare --verbose
...
Discovered plugin "cordova-plugin-my-plugin" in config.xml. Adding it to the project
No scripts found for hook "before_plugin_add".
Calling plugman.fetch on plugin "git+ssh://git@bitbucket.org/myteam/cordova-plugin-my-plugin.git"
Running command: npm install git+ssh://git@bitbucket.org/myteam/cordova-plugin-my-plugin.git --production --no-save
Command finished with error code 0: npm install,git+ssh://git@bitbucket.org/myteam/cordova-plugin-my-plugin.git,--production,--no-save
Failed to restore plugin "cordova-plugin-my-plugin" from config.xml. You might need to try adding it again. Error: Failed to fetch plugin git+ssh://git@bitbucket.org/myteam/cordova-plugin-my-plugin.git via registry.
Probably this is either a connection problem, or plugin spec is incorrect.
Check your connection and plugin name/version/URL.
Failed to get absolute path to installed module
...

As you can see Command finished with error code 0: npm install (OK) and then Failed to restore plugin "cordova-plugin-my-plugin" from config.xml.... Failed to get absolute path to installed module 🤔

When I realized that, I started looking for code root issue: cordova-cli > cordova-lib > cordova-fetch And investigating in dependency-ls, I understood how cordova-fetch is checking npm install execution success and how custom repository (out of registry) are handle as 'UNMET DEPENDENCY'.

  1. snapshot npm dependency root tree (npm ls --depth=0 parsed by dependency-ls under the hood)
  2. invoke npm install
  3. snapshot npm dependency root tree again
  4. diff trees and expecting 1 line diff as the new installed package
  5. search for the new installed package directory in node_modules 5.1. try in node_modules/${package_id_issued_from_dependency_ls} 5.2. if failed try loading all node_modules/*/package.json and try to find givenPackageUrl === pkg.repository.url

At start I thought my case matches 5.1. case... Wrong! dependency-ls in the particular case of out of registry packages output my package id has: UNMET DEPENDENCY cordova-plugin-my-plugin... Then trying to find node_modules/UNMET DEPENDENCY cordova-plugin-my-plugin/... 🤪 So, go to 5.2.! And here I have made the most insidious mistake... In Bitbucket you can clone your repository with two different

git+ssh://git@bitbucket.org:myteam/cordova-plugin-my-plugin.git
                           ^

and

git+ssh://git@bitbucket.org/myteam/cordova-plugin-my-plugin.git
                           ^

And unfortunately I've added my plugin with one form while the other was in my package.json.

Conclusion

While all is going well on the install, cordova-fetch wasn't able to realize it. And I really didn't expect cordova relying on the package.json repository url... I don't know why by design cordova-fetch does not rely on npm exit code? And at least may be you should remove UNMET DEPENDENCY from the package name in 5.1. so that directory exist test succeed.

HTH, cheers!

Jule- commented 6 years ago

I have not investigating more but there is another side effect: if you have issues adding a plugin while the npm install worked under the hood, next time you try to cordova prepare you will have an error. The only way to get it work again is to remove the involved plugin folder in node_modules and cordova prepare again.

raphinesse commented 6 years ago

The problems you describe have already been resolved but the fixes have not yet been released. Take a look at the current code in the master branch and feel free to test it using cordova@nightly. The PR that fixed it is #18.

I'm closing this now, but if you feel this was in error, please feel free to say so.

Jule- commented 6 years ago

@raphinesse oh great! I will try that and come back if issue still persists. Thanks for the info!