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

Yarn save-prefix option changed/ignored #4343

Closed
jambonrose opened this issue Sep 7, 2017 · 17 comments · Fixed by #4471
Closed

Yarn save-prefix option changed/ignored #4343

jambonrose opened this issue Sep 7, 2017 · 17 comments · Fixed by #4471
Assignees

Comments

@jambonrose
Copy link
Contributor

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

@BYK
Copy link
Member

BYK commented Sep 8, 2017

I cannot replicate this on latest master. What I tried:

yarn config set save-prefix ''
yarn add left-pad@1.0.0

Checked package.json file and it was saved without a prefix. Am I missing a step?

@vkrol
Copy link
Contributor

vkrol commented Sep 8, 2017

@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

@jambonrose
Copy link
Contributor Author

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 .

@jambonrose
Copy link
Contributor Author

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.

@BYK
Copy link
Member

BYK commented Sep 8, 2017

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.

@BYK BYK self-assigned this Sep 8, 2017
@jambonrose
Copy link
Contributor Author

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 ""

@BYK
Copy link
Member

BYK commented Sep 8, 2017

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 :(

@jambonrose
Copy link
Contributor Author

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.

@BYK
Copy link
Member

BYK commented Sep 8, 2017

@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 left-pad@1.1.3

package.json:

{
  "dependencies": {
    "left-pad": "1.1.3"
  }
}

@BYK
Copy link
Member

BYK commented Sep 8, 2017

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?

@BYK
Copy link
Member

BYK commented Sep 8, 2017

Adding the --debug flag and sharing the output may be helpful.

@jambonrose
Copy link
Contributor Author

Sorry if I've misunderstood, but is that first case you're seeing not a bug? Should the ^ be there?

@jambonrose
Copy link
Contributor Author

jambonrose commented Sep 8, 2017

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.

@jambonrose
Copy link
Contributor Author

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. 😕

@jambonrose
Copy link
Contributor Author

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.

@BYK
Copy link
Member

BYK commented Sep 12, 2017

@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?

@BYK BYK closed this as completed in #4471 Sep 17, 2017
BYK pushed a commit that referenced this issue Sep 17, 2017
**Summary**

Fixes #4343. Currently there is no way to remove the package prefix inside `.yarnrc` file, this PR add support for `save-exact` in `.yarnrc` as discussed in #4343. Full credit goes to @jambonrose

```
save-exact true
```

One small thing, should `yarn` be backwards compatible with the old behavior which is `save-prefix ''`? We can just add an extra check here for do this. What do you think @BYK?


```js
} else if (exact || Boolean(this.config.getOption('save-exact')) || Boolean(this.config.getOption('save-prefix'))) {

```

**Test plan**

New unit test.
@jambonrose
Copy link
Contributor Author

I was coming back around for this, but it looks like @ahmedelgabri got to it before me. Nice work!

joaolucasl pushed a commit to joaolucasl/yarn that referenced this issue Oct 27, 2017
**Summary**

Fixes yarnpkg#4343. Currently there is no way to remove the package prefix inside `.yarnrc` file, this PR add support for `save-exact` in `.yarnrc` as discussed in yarnpkg#4343. Full credit goes to @jambonrose

```
save-exact true
```

One small thing, should `yarn` be backwards compatible with the old behavior which is `save-prefix ''`? We can just add an extra check here for do this. What do you think @BYK?


```js
} else if (exact || Boolean(this.config.getOption('save-exact')) || Boolean(this.config.getOption('save-prefix'))) {

```

**Test plan**

New unit test.
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 a pull request may close this issue.

3 participants