You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by TimBarham <gi...@git.apache.org> on 2015/03/27 08:49:51 UTC

[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

GitHub user TimBarham opened a pull request:

    https://github.com/apache/cordova-lib/pull/194

    CB-8754 Auto-restoring a plugin fails when adding a platform.

    Start with a blank Cordova app, then enter the following:
    
        cordova plugin add org.apache.cordova.camera --save
        cordova plugin remove org.apache.cordova.camera
        cordova platform add browser
    
    The final step should restore the camera plugin, but it fails with the following exception:
    
        TypeError: Cannot read property 'latest' of undefined
            at next (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:694:35)
            at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:682:5
            at RegClient.get_ (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:105:14)
            at RegClient.<anonymous> (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:41:12)
            at fs.js:336:14
            at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:103:5
            at FSReqWrap.oncomplete (fs.js:99:15)
    
    The core problem here is an issue with how we work with npm. Every time we are going to use npm (when dealing with plugins), we call `npm.load()` passing it our settings. But `npm.load()` can only be called once per session - subsequent calls are ignored. The correct approach is to call `npm.load()` without any settings (to make sure npm is loaded), then call `npm.config.set()` for each setting. This change had been made for platforms, but not plugins.
    
    I also wanted to make sure each time we worked with npm we had a clean config (for example, if we get an npm package from the Cordova Plugin Respository, then later from the npm repository, it would try to get the second package from the CPR because that setting would still be around and not get overridden). So now any code that wants to load and init npm I pass through a central "load and restore" method. You pass this method your npm settings and the chain of promises you want to execute with those settings applied. It loads and initializes npm, executes the promises, then restores npm's configuration to what it was before we started.
    
    Finally, once that problem was fixed there was an additional problem - we were adding plugins too early and the platform wasn't fully initialized, so we'd get an error about not being able to find the platform's `config.xml` file.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/MSOpenTech/cordova-lib CB-8754

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/cordova-lib/pull/194.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #194
    
----
commit 0df6b800c6013bcf53045040ade81345fd9b47ec
Author: Tim Barham <ti...@microsoft.com>
Date:   2015-03-26T11:54:37Z

    CB-8754 Auto-restoring a plugin fails when adding a platform.
    
    Start with a blank Cordova app, then enter the following:
    
    cordova plugin add org.apache.cordova.camera --save
    cordova plugin remove org.apache.cordova.camera
    cordova platform add browser
    
    The final step should restore the camera plugin, but it fails with the following exception:
    
    TypeError: Cannot read property 'latest' of undefined
        at next (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:694:35)
        at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\lib\cache.js:682:5
        at RegClient.get_ (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:105:14)
        at RegClient.<anonymous> (D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\npm-registry-client\lib\get.js:41:12)
        at fs.js:336:14
        at D:\GIT\Cordova\cordova-lib\cordova-lib\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:103:5
        at FSReqWrap.oncomplete (fs.js:99:15)
    
    The core problem here is an issue with how we work with npm. Every time we are going to use npm (when dealing with plugins), we call npm.load() passing it our settings. But npm.load() can only be called once per session - subsequent calls are ignored. The correct approach is to call npm.load() without any settings (to make sure npm is loaded), then call npm.config.set() for each setting.
    
    This change had been made for platforms, but not plugins. I also wanted to make sure each time we worked with npm we had a clean config (for example, if we get an npm package from the Cordova plugin respository, then later from the npm repository, it would try to get the second package from the CPR because that setting would still be around and not get overridden). So now any code that wants to load and init npm I pass through a central "load and restore" method. You pass this method your npm settings and the chain of promises you want to execute with those settings applied. It loads and initializes npm, executes the promises, then restores npm's configuration to what it was before we started.
    
    Finally, once that problem was fixed there was an additional problem - we were adding plugins too early and the platform wasn't fully initialized, so we'd get an error about not being able to find the platform's config.xml file.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by TimBarham <gi...@git.apache.org>.
Github user TimBarham commented on the pull request:

    https://github.com/apache/cordova-lib/pull/194#issuecomment-86940734
  
    AppVeyor failed because it couldn't connect to github (a problem I was having today too) - is there a way to kickstart it going again?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by TimBarham <gi...@git.apache.org>.
Github user TimBarham commented on the pull request:

    https://github.com/apache/cordova-lib/pull/194#issuecomment-86962408
  
    Thanks Andrew. Changes made!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by TimBarham <gi...@git.apache.org>.
Github user TimBarham commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/194#discussion_r27298469
  
    --- Diff: cordova-lib/src/plugman/registry/registry.js ---
    @@ -223,11 +211,13 @@ module.exports = {
      * @param {Boolean} determines if we are using the npm registry
      * @return {Promise.<Object>} Promised settings.
      */
    -function initSettings() {
    +function initSettings(returnEmptySettings) {
         var settings = module.exports.settings;
     
         // check if settings already set
    -    if(settings !== null) return Q(settings);
    +    if(settings !== null) {
    --- End diff --
    
    No it should not. Good catch, thanks!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by TimBarham <gi...@git.apache.org>.
Github user TimBarham commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/194#discussion_r27299568
  
    --- Diff: cordova-lib/src/util/npm-helper.js ---
    @@ -0,0 +1,76 @@
    +/**
    + Licensed to the Apache Software Foundation (ASF) under one
    + or more contributor license agreements.  See the NOTICE file
    + distributed with this work for additional information
    + regarding copyright ownership.  The ASF licenses this file
    + to you under the Apache License, Version 2.0 (the
    + "License"); you may not use this file except in compliance
    + with the License.  You may obtain a copy of the License at
    +
    + http://www.apache.org/licenses/LICENSE-2.0
    +
    + Unless required by applicable law or agreed to in writing,
    + software distributed under the License is distributed on an
    + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + KIND, either express or implied.  See the License for the
    + specific language governing permissions and limitations
    + under the License.
    + */
    +
    +// Helper methods to help keep npm operations separated.
    +
    +var npm = require('npm'),
    +    Q = require('q'),
    +    cachedSettings = null,
    +    cachedSettingsValues = null;
    +
    +/**
    + * @description Calls npm.load, then initializes npm.config with the specified settings. Then executes a chain of
    + * promises that rely on those npm settings, then restores npm settings back to their previous value. Use this rather
    + * than passing settings to npm.load, since that only works the first time you try to load npm.
    + * @param {Object} settings
    + * @param {Function} promiseChain
    + */
    +function loadWithSettingsThenRestore(settings, promiseChain) {
    +    return loadWithSettings(settings).then(promiseChain).then(restoreSettings);
    --- End diff --
    
    Yes, indeed it should ... good point :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/cordova-lib/pull/194


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by agrieve <gi...@git.apache.org>.
Github user agrieve commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/194#discussion_r27298165
  
    --- Diff: cordova-lib/src/plugman/registry/registry.js ---
    @@ -223,11 +211,13 @@ module.exports = {
      * @param {Boolean} determines if we are using the npm registry
      * @return {Promise.<Object>} Promised settings.
      */
    -function initSettings() {
    +function initSettings(returnEmptySettings) {
         var settings = module.exports.settings;
     
         // check if settings already set
    -    if(settings !== null) return Q(settings);
    +    if(settings !== null) {
    --- End diff --
    
    This returns settings even when returnEmptySettings is true. should it?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by kamrik <gi...@git.apache.org>.
Github user kamrik commented on the pull request:

    https://github.com/apache/cordova-lib/pull/194#issuecomment-86971194
  
    Thanks, merging.
    Tests pass ok. Appveyor or GitHub were flaky for the last couple of days.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by agrieve <gi...@git.apache.org>.
Github user agrieve commented on the pull request:

    https://github.com/apache/cordova-lib/pull/194#issuecomment-86956650
  
    Nice clean-up! Let me know when comments are addressed and I'll merge it in.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-lib pull request: CB-8754 Auto-restoring a plugin fails wh...

Posted by agrieve <gi...@git.apache.org>.
Github user agrieve commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/194#discussion_r27298069
  
    --- Diff: cordova-lib/src/util/npm-helper.js ---
    @@ -0,0 +1,76 @@
    +/**
    + Licensed to the Apache Software Foundation (ASF) under one
    + or more contributor license agreements.  See the NOTICE file
    + distributed with this work for additional information
    + regarding copyright ownership.  The ASF licenses this file
    + to you under the Apache License, Version 2.0 (the
    + "License"); you may not use this file except in compliance
    + with the License.  You may obtain a copy of the License at
    +
    + http://www.apache.org/licenses/LICENSE-2.0
    +
    + Unless required by applicable law or agreed to in writing,
    + software distributed under the License is distributed on an
    + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + KIND, either express or implied.  See the License for the
    + specific language governing permissions and limitations
    + under the License.
    + */
    +
    +// Helper methods to help keep npm operations separated.
    +
    +var npm = require('npm'),
    +    Q = require('q'),
    +    cachedSettings = null,
    +    cachedSettingsValues = null;
    +
    +/**
    + * @description Calls npm.load, then initializes npm.config with the specified settings. Then executes a chain of
    + * promises that rely on those npm settings, then restores npm settings back to their previous value. Use this rather
    + * than passing settings to npm.load, since that only works the first time you try to load npm.
    + * @param {Object} settings
    + * @param {Function} promiseChain
    + */
    +function loadWithSettingsThenRestore(settings, promiseChain) {
    +    return loadWithSettings(settings).then(promiseChain).then(restoreSettings);
    --- End diff --
    
    nit: should restoreSettings be within a .finally() rather than a .then()?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org