question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] workspaces list result is not json

See original GitHub issue
  • I’d be willing to implement a fix

Describe the bug

run yarn workspaces list -v --json in a workspace, but the received output is not a valid JSON string

To Reproduce


Playground


const {promises: {writeFile, mkdir}} = require(`fs`);

await mkdir('packages');

await mkdir('packages/a');

await writeFile('packages/a/package.json', JSON.stringify({
name: 'a',
}));

await mkdir('packages/b');

await writeFile('packages/b/package.json', JSON.stringify({
name: 'b'
}));


await packageJsonAndInstall({
workspaces: {
packages: [
  "packages/*"
]
},
});

const output = await yarn(`workspaces`, `list`, `-v`, `--json`);

expect(output).not.toContain(`yarn`);
expect(output).toContain(`yarn`);


expect(() => JSON.parse(output)).not.toThrow();

Output:

Error: expect(received).toContain(expected) // indexOf

Expected substring: "yarn"
Received string:    "{\"location\":\".\",\"name\":null,\"workspaceDependencies\":[],\"mismatchedWorkspaceDependencies\":[]}
{\"location\":\"packages/a\",\"name\":\"a\",\"workspaceDependencies\":[],\"mismatchedWorkspaceDependencies\":[]}
{\"location\":\"packages/b\",\"name\":\"b\",\"workspaceDependencies\":[],\"mismatchedWorkspaceDependencies\":[]}
"
at module.exports (evalmachine.<anonymous>:31:16)
at process._tickCallback (internal/process/next_tick.js:68:7)

Screenshots

Environment if relevant (please complete the following information):

  • OS: OSX
  • Node version 12
  • Yarn version 2.4.0

Additional context

yarn 1 outputs a object text:

{
  "a": {
    location: "packages/a",
    workspaceDependencies: [],
    mismatchedWorkspaceDependencies: []
  },
  "b": {
    location: "packages/b",
    workspaceDependencies: [],
    mismatchedWorkspaceDependencies: []    
  }
}

IMO, yarn workspaces list -v --json should output either

  1. the same structure when it does in yarn 1 or 2. a valid JSON string, which can be safely JSON.parse(output) at least, even structure diff from yarn 1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
chengcybercommented, Jan 5, 2021

Thanks for the reply.

For those who run into this issue, try the following util function:

function getWorkspaceInfo() {
  const wsInfoNDJSON = require('child_process').execSync('yarn workspaces list -v --json').toString();
  const wsInfoList = wsInfoNDJSON;
    .split(require('os').EOL)
    .filter(Boolean)
    .map((x) => JSON.parse(x));
  return wsInfoList;
}
1reaction
merceyzcommented, Jan 5, 2021

The Project class has all the information about the workspaces, to list workspaces you can look at the code for the workspaces list command https://github.com/yarnpkg/berry/blob/9f8256807f406229333a6f45294ac2d5ca0a941c/packages/plugin-essentials/sources/commands/workspaces/list.ts#L24

If you need help consuming the API I suggest joining the discord channel and asking there

Read more comments on GitHub >

github_iconTop Results From Across the Web

My vscode workspace does not load properly: currently ...
I ran touch /workspaces/GITHUB_USERID/.devcontainer.json where GITHUB_USERID was my own ... However it says "No such file or directory".
Read more >
describe-workspaces — AWS CLI 1.27.37 Command Reference
Describes the specified WorkSpaces. You can filter the results by using the bundle identifier, directory identifier, or owner, but you can specify only...
Read more >
Angular workspace configuration
The angular.json file at the root level of an Angular workspace provides workspace-wide and project-specific configuration defaults.
Read more >
package.json - npm Docs
It must be actual JSON, not just a JavaScript object literal. ... instead of an object. If a url is provided, it will...
Read more >
Manage workspaces in portal or Python SDK (v2) - Azure ...
Learn how to manage Azure Machine Learning workspaces in the Azure portal or ... From parameters: There's no need to have a config.json...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found