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

Use "-v" to display version number instead of "-V" #3395

Merged
merged 1 commit into from
Jul 1, 2017
Merged

Use "-v" to display version number instead of "-V" #3395

merged 1 commit into from
Jul 1, 2017

Conversation

g-plane
Copy link
Contributor

@g-plane g-plane commented May 13, 2017

Summary

Before, we run yarn -V or yarn --version to display version number of Yarn. But we are used to using -v to display it, like running node -v and npm -v.
Maybe caused by commander.js, flags of command for displaying version number cannot be '-v, -V, --version'. In other word, we cannot run yarn -V to display version number any more.

Test plan

Just run yarn -v. Note that do NOT use yarn -V any more.

$ yarn -v
0.25.0-0
$ yarn --version
0.25.0-0

@bestander
Copy link
Member

Any idea how to make it non breaking?
Would be great to support -V

@g-plane
Copy link
Contributor Author

g-plane commented May 14, 2017

Now is OK.
sp20170514_084947

@arcanis
Copy link
Member

arcanis commented May 15, 2017

I don't really agree; -v is either --version or --verbose, depending on the tool you use. Since the version can already be extracted via -V and/or --version, I would prefer for -v to stay as the verbose short option. It would also allow us to use the -vv / -vvv syntax to control the level of verbosity.

@g-plane
Copy link
Contributor Author

g-plane commented May 15, 2017

But it seems that at current version running with -v, it will run install actually, not --verbose. For most people, after installed Yarn or other tools with CLI, they may run -v to test if the tool has been installed successfully, not just to get the version number.

@bestander
Copy link
Member

Ok, considering that opinions are split regarding -v/-V I propose to get rid of both and use only long version for arguments.

I agree that it is annoying when running yarn -v it executes yarn install, even worse it saves an empty lockfile.
Let's discuss fixing that effect then?

@g-plane
Copy link
Contributor Author

g-plane commented May 15, 2017

Do you mean that do not execute install if current directory doesn't have lockfile?

@bestander
Copy link
Member

Maybe if there is not package.json.
Missing yarn.lock can happen

@arcanis
Copy link
Member

arcanis commented May 15, 2017

Or we could just make yarn print Yarn's usage (ie require the command name instead of fallbacking on install). By doing this, we could actually support both -v as --version and --verbose:


$> yarn
Usage: yarn [command] [... options]
Commands: ...

$> yarn -v
0.24.3

$> yarn -V
0.24.3

$> yarn install -v
[info] Fetching http://...

$> yarn install -vv
[info] Fetching http://...
[debug] Receiving HTTP 200 from ...

@bestander
Copy link
Member

I quite like that install is the default command of yarn, I use it a lot and it would be a noticeable breaking change IMHO

@g-plane
Copy link
Contributor Author

g-plane commented May 15, 2017

I like install as a default command too.

@g-plane
Copy link
Contributor Author

g-plane commented May 15, 2017

@arcanis I think the example you showed is a good idea, except the first usage.

@bestander
Copy link
Member

Any thoughts on how to proceed?

@g-plane
Copy link
Contributor Author

g-plane commented May 24, 2017

Do you prefer to making Yarn not execute install if missing package.json? If so, I will try doing.

@bestander
Copy link
Member

This is nuanced.
Running yarn add in a folder without package.json is fine.
Running yarn install is not.
However we work on workspaces where a project can have multiple package.json files, in that case Yarn should find the top level folder and run install from there.

@bestander
Copy link
Member

Here we have 2 issues, I's like us to get somewhere about -v/-V.
@arcanis, @g-plane, how about we phase out ambiguous -v/-V completely?
And print usage message, e.g. Use --version to print version and --verbose to install verbously

@g-plane
Copy link
Contributor Author

g-plane commented May 24, 2017

I think using -v is convenient to check if yarn has been installed successfully, not only display version number. Maybe using -v to check that for any tools is a habit, I think.

@bestander
Copy link
Member

bestander commented May 24, 2017 via email

@g-plane
Copy link
Contributor Author

g-plane commented May 24, 2017

How about making running yarn -v only it will display version and running yarn install -v it will actually execute yarn install --verbose?

@g-plane
Copy link
Contributor Author

g-plane commented Jun 1, 2017

How should I add tests?

@bestander
Copy link
Member

bestander commented Jun 15, 2017

@g-plane, sorry for a delay, I was in a bug crunch and then a time off.
Are you still available to do more?

  1. I think there is not need to add automated tests.
    Showing screenshots of all possible scenarios in the PR description should be enough.

  2. How about making running yarn -v only it will display version and running yarn install -v it will actually execute yarn install --verbose?

Sounds non consistent, people are used to have install command as default, e.g. yarn --force, so -v behaving differently may be more confusing.

On that note, when you do yarn -v it outputs

yarn -v
yarn install v0.24.3
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...

success Saved lockfile.
✨  Done in 0.05s.

Which shows version of Yarn.
The only annoying side effect is that it adds an empty yarn.lock file.
Can we fix just that, e.g. don't save the lock file when there are no packages?

@g-plane
Copy link
Contributor Author

g-plane commented Jun 16, 2017

@bestander Well, I have done something at the last commit, which make yarn won't save lock file. Here is the code: ff00684

The first line if (Object.getPrototypeOf(this) === Install.prototype) is to check the current command. If it is install, it won't save lock file. But if the command is add, it will do what yarn add should do and save yarn.lock.

Anything should be changed? I will do.

Screenshots:
sp20170616_124051

sp20170616_124751

sp20170616_125454

src/cli/index.js Outdated
@@ -29,7 +29,7 @@ const args = process.argv.slice(2, doubleDashIndex === -1 ? process.argv.length
const endArgs = doubleDashIndex === -1 ? [] : process.argv.slice(doubleDashIndex + 1, process.argv.length);

// set global options
commander.version(version);
commander.version(version, '-v, -V, --version');
Copy link
Member

Choose a reason for hiding this comment

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

let's not alias -v/-V to --version yet because we have not agreed whether people will confuse it with --verbose.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only use --version ?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we avoid ambiguous shorthands in Yarn.


// `fs.exists()` is deprecated since nodejs v8
try {
await fs.access(loc);
Copy link
Member

Choose a reason for hiding this comment

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

looks a bit radical, I think this may be done coming from another angle.
If package.json does not exist then patterns from fetchRequestFromCwd call would be empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, how should I change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean I should call the function fetchRequestFromCwd?

Copy link
Member

Choose a reason for hiding this comment

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

No, it is called on line 421.
depRequests contains all the patterns from a package.json.
If it is empty and it is not an Add command then installation should stop without saving a lockfile.

@g-plane
Copy link
Contributor Author

g-plane commented Jun 17, 2017

@bestander I have done a force push for the commit: 93ddc5d . You can review the code. If something should be changed, I will do.

Screenshot:

sp20170617_162838

@g-plane
Copy link
Contributor Author

g-plane commented Jun 17, 2017

It seems there are some conflicts with unit test.

@bestander
Copy link
Member

Yeah, a few tests were relying on previous behavior

@g-plane
Copy link
Contributor Author

g-plane commented Jun 21, 2017

So should I change the test?

@@ -431,6 +431,10 @@ export class Install {
this.scripts.setArtifacts(artifacts);
}

if (Object.getPrototypeOf(this) === Install.prototype && depRequests.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Look at code on line 367, it saves explicitly when there is nothing to install.
Maybe that logic should be there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe I should move the code to there?

Copy link
Member

Choose a reason for hiding this comment

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

I think you can just enhance the code there, you probably don't need to check for Install.prototype.
The logic should be "don't save lockfile if there are no dependencies"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I will try.

@@ -29,7 +29,7 @@ const args = process.argv.slice(2, doubleDashIndex === -1 ? process.argv.length
const endArgs = doubleDashIndex === -1 ? [] : process.argv.slice(doubleDashIndex + 1, process.argv.length);

// set global options
commander.version(version);
commander.version(version, '--version');
Copy link
Member

Choose a reason for hiding this comment

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

what this command changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bestander
Copy link
Member

So should I change the test?

Yes, as long as the change is reasonable.

@bestander
Copy link
Member

Ok, this looks good now, thanks @g-plane.
Could you rebase please?
And could you confirm what message is written when there is no package.json and user types yarn -v?

@g-plane
Copy link
Contributor Author

g-plane commented Jun 23, 2017

How about: No package.json found. ?

@bestander
Copy link
Member

bestander commented Jun 23, 2017 via email

@g-plane
Copy link
Contributor Author

g-plane commented Jun 23, 2017

image

@bestander
Copy link
Member

Yeah, then no package.json found sounds good.

@g-plane
Copy link
Contributor Author

g-plane commented Jun 23, 2017

Sorry for delay.

@bestander
Copy link
Member

Can you run yarn prettier to make code comply with the styleguide?

await this.saveLockfileAndIntegrity(topLevelPatterns, workspaceLayout);
if (topLevelPatterns.length || await fs.exists(path.join(this.config.cwd, constants.LOCKFILE_FILENAME))) {
await this.saveLockfileAndIntegrity(topLevelPatterns, workspaceLayout);
}
Copy link
Member

Choose a reason for hiding this comment

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

how about

} else {
  reporter.info(reporter.lang('noPackageJsonFound'));
}

Then the the block at line 843-846 can be removed

Copy link
Member

Choose a reason for hiding this comment

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

I think I missed this one, do you need to check await fs.exists(path.join(this.config.cwd, constants.LOCKFILE_FILENAME)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check what? I am sorry that I do not quite understand what you mean.

Copy link
Member

Choose a reason for hiding this comment

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

Why are you doing this

if (topLevelPatterns.length || await fs.exists(path.join(this.config.cwd, constants.LOCKFILE_FILENAME))) {

instead of this?

if (topLevelPatterns.length) {

}

What does the second condition mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When running remove command, yarn will update lock file by running install.
https://github.com/yarnpkg/yarn/blob/master/src/cli/commands/remove.js#L74

Copy link
Member

Choose a reason for hiding this comment

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

I see, good thinking.
Yes, makes sense in that case.
Would you add a comment for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One more thing, fs.exists() is deprecated since node 8. Should I make a change?
https://nodejs.org/dist/latest-v8.x/docs/api/fs.html#fs_fs_exists_path_callback

Copy link
Member

Choose a reason for hiding this comment

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

no, we use our own wrapper of fs.exists, so it is fine

@@ -181,6 +181,8 @@ const messages = {
foundWarnings: 'Found $0 warnings.',
foundErrors: 'Found $0 errors.',

noPackageJsonFound: 'No package.json found.',
Copy link
Member

Choose a reason for hiding this comment

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

How about

notSavedLockfileNoDependencies: 'Lockfile not saved, no dependencies.',

We are not focusing on a package.json existence but on the packages that were installed

@bestander
Copy link
Member

Good job, @g-plane, just a few nits.

@g-plane
Copy link
Contributor Author

g-plane commented Jun 24, 2017

I have run yarn prettier and yarn lint before committing, but the tests still failed. I don't know why.

@bestander
Copy link
Member

Try rebasing to master, yarn install && yarn prettier and see if there are any file changes

@g-plane
Copy link
Contributor Author

g-plane commented Jun 27, 2017

Is that OK?

@bestander bestander merged commit e0e119e into yarnpkg:master Jul 1, 2017
@bestander
Copy link
Member

Great job, @g-plane!
Looking forward for more PRs.

@g-plane
Copy link
Contributor Author

g-plane commented Jul 1, 2017

Thanks!

@g-plane
Copy link
Contributor Author

g-plane commented Jul 1, 2017

Should #2710 and #3329 be closed?

GulajavaMinistudio added a commit to GulajavaMinistudio/yarn that referenced this pull request Jul 1, 2017
Do not save lockfile when no dependencies (yarnpkg#3395)
jimmyhchan added a commit to jimmyhchan/yarn that referenced this pull request May 21, 2018
…encies

this reverts most of yarnpkg#3395 992b5c9
which appears only necessary to work around yarnpkg#3329 (requesting the yarn
version with -v triggered install)
BYK pushed a commit that referenced this pull request May 23, 2018
…5843)

**Summary**

this reverts most of #3395 992b5c9 which appears only necessary to work around #3329 (requesting the yarn version with -v triggered install). As noted in #5839, `yarn -v` and `yarn check` no longer have `install` as a side effect and IMO, the original behavior where an empty lock file is generated as part of install is the correct behavior.

Many tools currently sniff for a yarn.lock file to determine if yarn is being used. It seems more consistent with `yarn import` which generates a lockfile even if the existing node_modules is empty.

fixes #5839 

**Test plan**

`yarn run test` passes
`yarn run lint` passes
`yarn-local install`  saves an empty lockfile
`yarn-local import`  continues to save an empty lockfile
`yarn-local -v` will not trigger install and will not generate a lock file. correctly displays a version.
`yarn-local check` will not trigger install and will not generate a lock file
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.

3 participants