Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@
"@types/semver": "^7",
"@typescript-eslint/eslint-plugin": "^8.7.0",
"@typescript-eslint/parser": "^8.7.0",
"@yarnpkg/cli": "^4.5.3",
"@yarnpkg/core": "^4.1.6",
"@yarnpkg/fslib": "^3.1.1",
"@yarnpkg/types": "^4.0.0",
"babel-jest": "^29.7.0",
"chalk": "^4.1.2",
Expand Down
44 changes: 22 additions & 22 deletions scripts/lint-teams-json.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { readJsonFile } from '@metamask/utils/node';
import { getPluginConfiguration } from '@yarnpkg/cli';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove these dependencies? This was the only place they were used

import { Configuration, Project, structUtils } from '@yarnpkg/core';
import { ppath } from '@yarnpkg/fslib';
import execa from 'execa';
import path from 'path';

main().catch(console.error);
type Workspace = {
location: string;
name: string;
};

// Run the script immediately.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

/**
* The entrypoint to this script.
Expand All @@ -16,17 +23,9 @@ main().catch(console.error);
async function main() {
const releaseableWorkspaces = await getPublicWorkspaces();
const releaseablePackageNames = releaseableWorkspaces.map((workspace) => {
const packageName = workspace.manifest.name;
if (packageName === null) {
throw new Error(
`${structUtils.stringifyIdent(
workspace.anchoredDescriptor,
)} has no name in its manifest`,
);
}
// The package names in teams.json omit the leading "@", so we do that here
// too in order to be consistent
return structUtils.stringifyIdent(packageName).slice(1);
return workspace.name.slice(1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Unchecked Assumptions: Silent Data Corruption

The code assumes all package names start with "@" and blindly calls .slice(1) without validation. If a workspace has a package name that doesn't start with "@", the first character will be incorrectly removed, causing silent data corruption. The old code had explicit null checking and error handling that was removed.

Fix in Cursor Fix in Web

});

const teams = await readJsonFile<Record<string, string>>(
Expand All @@ -52,18 +51,19 @@ async function main() {
}

/**
* Uses the Yarn API to gather the Yarn workspaces inside of this project (the
* packages that are matched by the `workspaces` field inside of
* Uses the `yarn` executable to gather the Yarn workspaces inside of this
* project (the packages that are matched by the `workspaces` field inside of
* `package.json`).
*
* @returns The list of workspaces.
*/
async function getPublicWorkspaces() {
const cwd = ppath.resolve('..', ppath.cwd());
const configuration = await Configuration.find(cwd, getPluginConfiguration());
const { project } = await Project.find(configuration, cwd);
async function getPublicWorkspaces(): Promise<Workspace[]> {
const { stdout } = await execa('yarn', [
'workspaces',
'list',
'--json',
'--no-private',
]);

return project.workspaces.filter((workspace) => {
return !workspace.manifest.private;
});
return stdout.split('\n').map((line) => JSON.parse(line));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Trailing Newlines Break JSON Parsing

The getPublicWorkspaces function splits yarn workspaces list stdout by newlines and parses each line as JSON. Command-line tools often output trailing newlines or empty lines, which causes JSON.parse('') to throw a SyntaxError and crash the script. This is a regression in error handling.

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose so. We use this in other scripts though and it hasn't been a problem there, so I'm okay with this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Robust JSON Parsing for Command Output

The stdout.split('\n').map((line) => JSON.parse(line)) pattern will fail if the command output includes a trailing newline, which would create an empty string that causes JSON.parse to throw a SyntaxError. The code should filter out empty lines before parsing, such as using stdout.split('\n').filter(line => line.trim()).map((line) => JSON.parse(line)).

Fix in Cursor Fix in Web

}
Loading
Loading