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: print errors when --json option used #5718

Merged
merged 4 commits into from Dec 10, 2022

Conversation

await-ovo
Copy link
Member

fix #5710

@zkochan
Copy link
Member

zkochan commented Nov 30, 2022

Shouldn't the error be in JSON format?

@await-ovo
Copy link
Member Author

await-ovo commented Dec 1, 2022

Shouldn't the error be in JSON format?

Maybe we should use console.error in printErrors function? so that error info will print to stderr,here's an example of npm when package.json is parsed error:

const { spawnSync } = require('child_process');

const {stderr, stdout} = spawnSync('npm', 'publish --dry-run --json'.split(' '), {
  cwd: 'xxxx/test-pnpm/packages/foo'
});

console.log(`stdout --> \n`, stdout?.toString())

console.log(`stderr --> \n`, stderr?.toString())


// output
stdout --> 
 
stderr --> 
 npm ERR! code EJSONPARSE
npm ERR! path xxxx/test-pnpm/packages/foo/package.json
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected string in JSON at position 20 while parsing '{
npm ERR! JSON.parse   "name": "foo"
npm ERR! JSON.parse   "dependencies": {
npm ERR! JSON.parse   '
npm ERR! JSON.parse Failed to parse JSON data.
npm ERR! JSON.parse Note: package.json must be actual JSON, not just JavaScript.
{
  "error": {
    "code": "EJSONPARSE",
    "summary": "Failed to parse json\nUnexpected string in JSON at position 20 while parsing '{\n  \"name\": \"foo\"\n  \"dependencies\": {\n  '",
    "detail": "Failed to parse JSON data.\nNote: package.json must be actual JSON, not just JavaScript."
  }
}

npm ERR! A complete log of this run can be found in:
npm ERR!     /xxxx/.npm/_logs/2022-12-01T02_37_06_751Z-debug-0.log


@zkochan
Copy link
Member

zkochan commented Dec 5, 2022

I think let's keep the way error are logged now. Don't change it to stderr. We just need to print the json error.

@await-ovo
Copy link
Member Author

await-ovo commented Dec 5, 2022

I think let's keep the way error are logged now. Don't change it to stderr. We just need to print the json error.

@zkochan Thanks for your suggestion, does it mean that the error is output in json format, like follows:

{
  "error": {
    "code": "",
    "message": ""
  }
}

I also noticed that some warnings or errors may be logged when getConfig, should these also be in json format?

@zkochan
Copy link
Member

zkochan commented Dec 5, 2022

For now I would only change the publish command's json output on unhandled exception.

@await-ovo
Copy link
Member Author

await-ovo commented Dec 8, 2022

For now I would only change the publish command's json output on unhandled exception.

If --json or --parseable is present, a unhandled error will output looks like the following:

$ pd publish --dry-run --json
{
  "error": {
    "code": "ERR_PNPM_GIT_UNCLEAN",
    "message": "Unclean working tree. Commit or stash changes first."
  }
}

$ pd add nanoid -p
{
  "error": {
    "code": "ERR_PNPM_ADDING_TO_ROOT",
    "message": "Running this command will add the dependency to the workspace root, which might not be what you want - if you really meant it, make it explicit by running this command again with the -w flag (or --workspace-root). If you don't want to see this warning anymore, you may set the ignore-workspace-root-check setting to true."
  }
}

@zkochan
Copy link
Member

zkochan commented Dec 9, 2022

Tests are needed

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.

pnpm publish --json does not report error
2 participants