Do you want to request a feature or report a bug?
Bug
What is the current behavior?
I've just upgraded from v0.20.3 to v1.0.1. The behavior described by @meznaric in #3045 was what I was previously seeing.
When I run yarn config set save-prefix false (the previous recommendation, as noted in #3045), installed packages now have the string false placed in front of the version. For instance, when I run yarn add --dev rollup-plugin-uglify, this is what Yarn adds to my package.json.
"rollup-plugin-uglify": "false2.0.1"
If I run yarn config set save-prefix '' to try and remove the prefix, the option is ignored. Below is the string Yarn adds to package.json when I run the same command as above with the new config.
"rollup-plugin-uglify": "^2.0.1",
Note that Yarn lists my NPM config as having the same options (as shown by yarn config list).
Yarn config excerpt:
'save-prefix': '',
NPM config excerpt:
'save-exact': true,
'save-prefix': '',
I have no idea if I am the problem here, as I cannot find any documentation about this setting.
This is related to #1088 and #3320.
What is the expected behavior?
I should be able to configure Yarn such that the versions of packages being added are exact (unless there is a good reason I should not be doing this? - if so, a warning is in order).
Please mention your node.js, yarn and operating system version.
Node v6.11.2
Yarn v1.0.1
macOS 10.11
I cannot replicate this on latest master. What I tried:
yarn config set save-prefix ''
yarn add [email protected]
Checked package.json file and it was saved without a prefix. Am I missing a step?
@BYK if I run the following commands:
yarn config set save-prefix ''
yarn add left-pad
this is what Yarn adds to package.json:
"left-pad": "true1.1.3",
This is certainly a bug.
Please note that I did not specify the exact version of the dependency.
Please mention your node.js, yarn and operating system version.
Node v8.4.0
Yarn v1.0.1
Windows 10 15063
Hi @BYK,
Below is a (bash) script to demonstrate. Please note that the script assumes the existence of a git repo with no changes. I've included the output for the relevant commands.
$ yarn config set save-prefix false
$ yarn add --dev rollup-plugin-uglify
$ cat package.json | grep rollup-plugin-uglify
"rollup-plugin-uglify": "false2.0.1",
$ git checkout .
$ yarn config set save-prefix ''
$ yarn add --dev rollup-plugin-uglify
$ cat package.json | grep rollup-plugin-uglify
"rollup-plugin-uglify": "^2.0.1",
$ git checkout .
For good measure, here is the output of my config, with the registry token scrubbed.
$ yarn config list
yarn config v1.0.1
info yarn config
{ 'version-tag-prefix': 'v',
'version-git-tag': true,
'version-git-sign': false,
'version-git-message': 'v%s',
'init-version': '0.0.1',
'init-license': 'BSD-2-Clause',
'save-prefix': '',
'ignore-scripts': false,
'ignore-optional': false,
registry: 'https://registry.yarnpkg.com',
'strict-ssl': true,
'user-agent': 'yarn/1.0.1 npm/? node/v6.11.2 darwin x64',
lastUpdateCheck: 1504807978160 }
info npm config
{ 'init-author-name': 'Andrew Pinkham',
'init-author-url': 'https://www.jambonsw.com/',
'init-license': 'BSD-2-Clause',
'init-version': '0.0.1',
'save-exact': true,
'save-prefix': '' }
✨ Done in 0.13s.
I think this is due to a quirk in bash or how we use config. If you go ahead and set this manually in the .yarnrc file, you'll see that the empty string will be used as the prefix. I'm using a different shell which passes the empty string properly.
I think passing an empty string with bash as the last argument is pretty quirky so I'll see what I can do.
What are you setting in .yarnrc? Mine is pasted below.
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
init-license BSD-2-Clause
init-version "0.0.1"
lastUpdateCheck 1504807978160
save-prefix ""
save-prefix ""
This one. That said it looks correct. That's very weird. I can't reproduce this with the config you provide using Yarn 1.0.1 :(
What other information can I provide to try and track this down?
I am going to install from master to see if perhaps that's the difference.
@jambonrose so here's what happens to me, with that .yarnrc file in my test folder:
yarn add left-pad
package.json:
{
"dependencies": {
"left-pad": "^1.1.3"
}
}
yarn add [email protected]
md5-fb649b2cc62c88e7e21975d475c4c9ce
{
"dependencies": {
"left-pad": "1.1.3"
}
}
Can there be another .yarnrc file that is overriding this value? Or may be we have a bug and you have a file upper in the hierarchy (such as ~/.yarnrc) that sets the prefix to true or something?
Adding the --debug flag and sharing the output may be helpful.
Sorry if I've misunderstood, but is that first case you're seeing not a bug? Should the ^ be there?
To clarify, the behavior I'm expecting is, when save-prefix is the empty string (or false), that dependencies will be set without _any_ prefixes, even when I do not specify a version. When you install leftpad with the command yarn add left-pad, I expect your package.json to read:
{
"dependencies": {
"left-pad": "1.1.3"
}
}
Instead your package.json has a ^ in front of it.
I confirm that I am seeing this behavior with a build from master.
I've tried adding the --debug flag at the end and the beginning of the command, but my output doesn't appear any different. 😕
The problem stems from line 89 of src/cli/commands/add.js, where the option is set to a string. The empty string is falsy, meaning that the code '' || '^' always yields ^.
// src/cli/commands/add.js
let prefix;
if (tilde) {
prefix = '~';
} else if (exact) {
prefix = '';
} else {
prefix = String(this.config.getOption('save-prefix')) || '^';
}
Rather than trying to add logic around save-prefix, I would prefer to introduce a new option save-exact. This helps avoid some of the behavior seen in #4104 and #2653. It doesn't solve #1088 (as, AFAICT, yarn doesn't rely on info from ~/.npmrc ever), but it does allow for parity between the two package managers on this subject matter.
By having this option, the logic above could become something like:
// src/cli/commands/add.js
let prefix;
if (tilde) {
prefix = '~';
} else if (exact || Boolean(this.config.getOption('save-exact'))) {
prefix = '';
} else {
prefix = String(this.config.getOption('save-prefix')) || '^';
}
Feedback welcome.
@jambonrose Yarn actually respects .npmrc. I agree with your assessment and also with your solution proposal with one important distinction: we should support '' as save-prefix too.
Are you up for a PR?
I was coming back around for this, but it looks like @ahmedelgabri got to it before me. Nice work!
Most helpful comment
The problem stems from line 89 of
src/cli/commands/add.js, where the option is set to a string. The empty string is falsy, meaning that the code'' || '^'always yields^.Rather than trying to add logic around
save-prefix, I would prefer to introduce a new optionsave-exact. This helps avoid some of the behavior seen in #4104 and #2653. It doesn't solve #1088 (as, AFAICT, yarn doesn't rely on info from~/.npmrcever), but it does allow for parity between the two package managers on this subject matter.By having this option, the logic above could become something like:
Feedback welcome.