{"action":"created","issue":{"url":"https://api.github.com/repos/lorenwest/node-config/issues/266","repository_url":"https://api.github.com/repos/lorenwest/node-config","labels_url":"https://api.github.com/repos/lorenwest/node-config/issues/266/labels{/name}","comments_url":"https://api.github.com/repos/lorenwest/node-config/issues/266/comments","events_url":"https://api.github.com/repos/lorenwest/node-config/issues/266/events","html_url":"https://github.com/lorenwest/node-config/issues/266","id":118114383,"number":266,"title":"Bug: defer'ed values can end up in the final configuration object depending resolution order","user":{"login":"markstos","id":25829,"avatar_url":"https://avatars.githubusercontent.com/u/25829?v=3","gravatar_id":"","url":"https://api.github.com/users/markstos","html_url":"https://github.com/markstos","followers_url":"https://api.github.com/users/markstos/followers","following_url":"https://api.github.com/users/markstos/following{/other_user}","gists_url":"https://api.github.com/users/markstos/gists{/gist_id}","starred_url":"https://api.github.com/users/markstos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markstos/subscriptions","organizations_url":"https://api.github.com/users/markstos/orgs","repos_url":"https://api.github.com/users/markstos/repos","events_url":"https://api.github.com/users/markstos/events{/privacy}","received_events_url":"https://api.github.com/users/markstos/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-11-20T19:54:10Z","updated_at":"2016-03-04T13:26:39Z","closed_at":null,"body":"This is closely related to #265.\r\n\r\nConsider the following configuration, where one deferred configuration value refers to another configuration value:\r\n\r\n```javascript\r\n// using defer functions is optional. See example and docs below.\r\n var defer = require('config/defer').deferConfig\r\n\r\n\r\nmodule.exports = {\r\n  a : \"value a\",\r\n  b : defer(function () {\r\n    return this.a;\r\n  }),\r\n  c : defer(function () {\r\n    return this.b;\r\n  }),\r\n};\r\n```\r\n\r\nRegardless of whether #265 is fixed, it's possible that a deferred value will be referenced in another deferred value.   \r\n\r\nThis happened to us in today in a complex configuration which wasn't easy to spot right away and where it's desirable to have these values to deferred, to eliminate configuration repetition.\r\n\r\nThe current result is a kind of a \"silent failure\" where the the deferred function becomes the value of one of the functions, which later becomes a bug in the application. \r\n\r\nSome possible improvements would be:\r\n\r\n 1. Fail explicitly. Throw an exception noting that nested defer'ed values have been encountered and aren't supported.\r\n 2. Resolve-on-demand. When an unresolved is encountered in a defer function, we would resolve it immediately. \r\n\r\nResolve-on-demand sounds attractive, but has challenges. Right now raw objects are used in the defer functions. We use an explicit method in the functions to access config properties, which would involve changing the syntax of in defer functions (at least if you want this safety check). \r\n\r\nOther possibility might be to modify the 'this' available to defer functions using a custom `get`  key for the properties. When properties were attempted to be read, we could resolve-on-demand.  This custom 'getters' would not be needed on the final object. \r\n\r\nFailing explicitly in this case is simpler."},"comment":{"url":"https://api.github.com/repos/lorenwest/node-config/issues/comments/192282841","html_url":"https://github.com/lorenwest/node-config/issues/266#issuecomment-192282841","issue_url":"https://api.github.com/repos/lorenwest/node-config/issues/266","id":192282841,"user":{"login":"markstos","id":25829,"avatar_url":"https://avatars.githubusercontent.com/u/25829?v=3","gravatar_id":"","url":"https://api.github.com/users/markstos","html_url":"https://github.com/markstos","followers_url":"https://api.github.com/users/markstos/followers","following_url":"https://api.github.com/users/markstos/following{/other_user}","gists_url":"https://api.github.com/users/markstos/gists{/gist_id}","starred_url":"https://api.github.com/users/markstos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markstos/subscriptions","organizations_url":"https://api.github.com/users/markstos/orgs","repos_url":"https://api.github.com/users/markstos/repos","events_url":"https://api.github.com/users/markstos/events{/privacy}","received_events_url":"https://api.github.com/users/markstos/received_events","type":"User","site_admin":false},"created_at":"2016-03-04T13:26:39Z","updated_at":"2016-03-04T13:26:39Z","body":"@mhsekhavat Would you be interested in putting together a pull request for your proposed fix? Ideally it would include code, tests and docs for the feature.  The documentation for the `defer()` system is here:\r\n\r\nhttps://github.com/lorenwest/node-config/wiki/Configuration-Files#javascript-module---js\r\n\r\nPerhaps there's an additional place it should be documented as well. "}}