Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properties accessed on singleton now reflect current state of instance #1366

Merged
merged 3 commits into from Jul 13, 2019
Merged

fix: properties accessed on singleton now reflect current state of instance #1366

merged 3 commits into from Jul 13, 2019

Conversation

mleguen
Copy link
Member

@mleguen mleguen commented Jun 19, 2019

singletonify() used to set Argv's properties to inst's properties values at the time of Argv's last call.

So Argv's properties and the inst's ones could diverge afterwards, such as parsed after a call to parse(), still being false in Argv.parsed, not in inst.parse.

singletonify() now adds setters to Argv instead, looking for the value of the corresponding property in inst at the time the property is used.

singletonify() used to set Argv's properties to the singleton's
properties values _at the time of Argv's last call_.

So Argv's properties and the singleton's ones diverged
afterwards, such as parsed after a call to parse, still false
in Argv.parsed, not in singleton.parse.

singletonify() now adds setters to Argv instead, looking for the
value of the corresponding property in the singleton _at the time
the property is used_.
Copy link
Member

@bcoe bcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a pretty reasonable fix...

On my mind regarding #1137 however, is...

Could we figure out a way to make singleton mode safer (perhaps as soon as someone calls parse() or argv(), as an example, we re-initialize the singleton as if someone had created a new yargs instance?).

Part of me wishes we could retire the singleton API, but it's very widely used... perhaps the next best thing would be trying to eliminate state.

@@ -25,8 +25,10 @@ function singletonify (inst) {
Object.keys(inst).forEach((key) => {
if (key === 'argv') {
Argv.__defineGetter__(key, inst.__lookupGetter__(key))
} else if (typeof inst[key] === 'function') {
Argv[key] = inst[key].bind(inst)
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we make this slightly more explicit:

if (['$0', 'parsed'].indexOf(key) !== -1) {

and we should be mindful about leaking any more stateful variables on the yargs object going forward.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the point. If we support singleton mode, shouldn't every property/method call on Argv have the same effect as if directly called on the singleton?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair 👍 I just noticed that, I think, argv, $0, and parsed are the only properties that leak ... and honestly, I don't think we meant to leak parsed as part of the API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! I did not realize this... However, parsed is already leaking from the API when not using the singleton. So, does it have a sense to filter properties we accept to leak from the singleton, and not to do it when not using the singleton?

@@ -837,6 +837,17 @@ describe('yargs dsl tests', () => {
})
})

describe('parsed', () => {
it('should be false before parsing', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could figure out the specific exception that was happening with showHelp and put a test around this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. A simple "showHelp should not fail" should do the job

@bcoe
Copy link
Member

bcoe commented Jun 23, 2019

@mleguen what are your feelings regarding us eventually fixing yargs, such that you can use it in singelton mode, but call parse() multiple times without bumping into edge-cases?

I'm happy to land this fix, but think we might want to have the above goal eventually in mind?

@mleguen
Copy link
Member Author

mleguen commented Jun 23, 2019

@bcoe We are trying to deal here with 3 different issues:

  1. fixing the singleton for its behavior not to be different from the "regular" API (even if it allows accessing properties not meant to be accessed, as the regular API does) : this PR answers this point in its current state
  2. fixing exception thrown when yargs.parsed.newAliases is undefined #1144 which this PR, as you pointed, does not yet prove to do (at least one test is missing... and maybe more as I may have wrongly related this issue to the previous one)
  3. fixing parse() to be callable several times, which is yet another problem, probably the more preoccupying.

My opinion is: we should do 1 and 2 in this PR, and 3 in another one (I should be able to spend some time on it next Wednesday).

@bcoe
Copy link
Member

bcoe commented Jun 24, 2019

@mleguen sounds good to me, I'm comfortable landing this as soon as we have the test for #1144.

@mleguen mleguen changed the title Fix/1144 require yargs properties frozen by singletonify (Fix) require("yargs") properties frozen by singletonify Jun 26, 2019
@mleguen
Copy link
Member Author

mleguen commented Jun 26, 2019

I am unable to reproduce #1144 and was indeed wrong when thinking it linked to the singletonify issue.

So this PR no longer closes #1144 (I renamed it) and I did not add tests, as we already extensively test showHelp().

@bcoe bcoe changed the title (Fix) require("yargs") properties frozen by singletonify fix: properties accessed on singleton now reflect current state of instance Jul 13, 2019
@bcoe bcoe merged commit 409d35b into yargs:master Jul 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants