Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When running yarn init yarn iterates through the keys listed here: https://github.com/yarnpkg/yarn/blob/58a4fa79d9ef92f640af5c90977d2e81c58c4ad6/src/cli/commands/init.js#L49
When it reaches the repository section it attempts to print out the default value for the repository from the package.json for the prompt. This is an object:
{ type: 'git', url: '[email protected]:git/repo.git' }.
When this value is output into the template string: https://github.com/yarnpkg/yarn/blob/58a4fa79d9ef92f640af5c90977d2e81c58c4ad6/src/cli/commands/init.js#L106 it errors with the following:
error An unexpected error occurred: "Cannot convert object to primitive value".
Manually patching this line in my local init.js to:
question += ` (${ JSON.stringify(def) })`;
Fixes the issue and allows the initialisation to finish.
Please mention your node.js, yarn and operating system version.
Yarn v0.17.3
Node v6.9.1
OS X 10.12.1
Let me know if there is anything else you want me to do to debug this issue. It sounds similarly related to https://github.com/yarnpkg/yarn/commit/58a4fa79d9ef92f640af5c90977d2e81c58c4ad6 but the latest version on homebrew is v0.17.3 so i've not tested to see if this fixes it.
Trace from the yarn-error.log file:
TypeError: Cannot convert object to primitive value
at Object.<anonymous> (/usr/local/Cellar/yarn/0.17.3/libexec/lib/node_modules/yarn/lib/cli/commands/init.js:93:27)
at next (native)
at step (/usr/local/Cellar/yarn/0.17.3/libexec/lib/node_modules/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
at /usr/local/Cellar/yarn/0.17.3/libexec/lib/node_modules/yarn/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
at process._tickCallback (internal/process/next_tick.js:103:7)
The commit you mentioned seems to have broken backwards compatibility with existing package.json files. This commit was already present in 0.17, so it does not fix your issue.
This works perfectly fine:
{
"name": "my-awesome-package",
"version": "1.0.0",
"description": "The best package you will ever find.",
"main": "index.js",
"repository": "[email protected]:git/repo.git",
"author": "Yarn Contributor",
"license": "MIT"
}
This results in an error:
{
"name": "my-awesome-package",
"version": "1.0.0",
"description": "The best package you will ever find.",
"main": "index.js",
"repository": {
"url": "[email protected]:git/repo.git",
"type": "git"
},
"author": "Yarn Contributor",
"license": "MIT"
}
I'm not quite sure what the correct course of action is or if this method is considered deprecated in Yarn. I can imagine people preferring to write out fields like the repository field the old way and then running yarn init.
package.json files? It does however restrict the user but on the positive side it does encourage clarity.If the decision is taken to continue on the path of 58a4fa7 then the documentation needs some updating as well. It now mentions Yarn generates a package.json similair to the following:
{
"name": "my-awesome-package",
"version": "1.0.0",
"description": "The best package you will ever find.",
"main": "index.js",
"repository": {
"url": "https://github.com/yarnpkg/example-yarn-package",
"type": "git"
},
"author": "Yarn Contributor",
"license": "MIT"
}
Which it does not, it actually generates:
{
"name": "my-awesome-package",
"version": "1.0.0",
"description": "The best package you will ever find.",
"main": "index.js",
"repository": "https://github.com/yarnpkg/example-yarn-package",
"author": "Yarn Contributor",
"license": "MIT"
}
EDIT: Just noticed that the documentation currently online says v0.16.1, not sure if above is relevant.
Please correct me if I'm wrong!
I think the documentation is slightly confusing as to whether you actually need to run yarn init or not. I went through the "getting started" journey on your website and then ended up at this page: https://yarnpkg.com/en/docs/usage. I already have an existing project setup using npm, i just wanted to switch to yarn - so i dont think i even needed to run yarn init.
I'm not sure if anyone else has had the question of "do i need to start a new project to use yarn?", it is maybe a little unclear that if you have a package.json already, you probably don't need to.
If i start a new project in a new folder, yarn init works correctly and defaults the git repository to nothing:
{
"name": "tmp",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}
If I add a git repo URL, you are correct it generates it just with a string:
{
"name": "a",
"version": "1.0.0",
"main": "index.js",
"repository": "gitrepo",
"license": "MIT"
}
Are you trying to be completely compatible with npm's package.json format? Because if so then yarn should probably attempt to support both https://docs.npmjs.com/files/package.json#repository
So is it the problem of documentation instead of a bug?
In a new project, I ran
npm init;
yarn init;
and face the same problem in a new project.
There is a few options off my head:
1) extend the question list to include 'type of repository' and 'repository url' https://github.com/gilbertwat/yarn/tree/1924_fix_init_repository
2) Add check for the current repository field for package.json
3) Update the documentations
Which way should we take?
You shouldn't have to run npm init AND yarn init; the latter is a replacement afaik.
I think there are a couple of issues here:
yarn init on an already existing npm project.Same problem, fixed with "repository": "https://github.com/yarnpkg/example-yarn-package",
Guess this is the same bug:
$ yarn init
yarn init v0.19.1
question name (snap):
question version (1.0.0):
question description: Snap!
question entry point (index.js):
question repository url ([object Object]):
question author (G. Kay Lee <[email protected]>):
question license (MIT):
success Saved package.json
✨ Done in 928.03s.
$ cat package.json
{
"name": "snap",
"version": "1.0.0",
"description": "Snap!",
"main": "index.js",
"repository": {},
"author": "G. Kay Lee <[email protected]>",
"license": "MIT"
}
$ yarn init
yarn init v0.19.1
question name (snap):
question version (1.0.0):
question description (Snap!):
question entry point (index.js):
error An unexpected error occurred: "Cannot convert object to primitive value".
info If you think this is a bug, please open a bug report with the information provided in "/Users/gsklee/Projects/GitHub/gsklee/snap/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/init for documentation about this command.
[object Object] during the initial yarn inityarn init
Most helpful comment
Guess this is the same bug:
[object Object]during the initialyarn inityarn init