Skip to content

Commit 93acf52

Browse files
authored
Show "unused catalog protocols warnings/errors" only if the unused catalog is one of the workspace's default catalogs (#30)
* not warn when adding a dependency not in the default alias group * edit test * remove unnecessary default track specification * add default: max test case
1 parent 6dda85e commit 93acf52

File tree

6 files changed

+70
-18
lines changed

6 files changed

+70
-18
lines changed

bundles/@yarnpkg/plugin-catalogs.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sources/__tests__/validation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect, afterEach } from "vitest";
22
import {
33
createTestWorkspace,
4-
TestWorkspace,
4+
type TestWorkspace,
55
extractDependencies,
66
} from "./utils";
77

sources/__tests__/warnings.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe("warnings and recommendations", () => {
9393
expect(dependencies).includes("react@npm:18.0.0");
9494
});
9595

96-
it("should warn when adding a dependency not in the default alias group", async () => {
96+
it("should not warn when adding a dependency not in the default alias group", async () => {
9797
workspace = await createTestWorkspace();
9898

9999
await workspace.writeYarnrc({
@@ -115,7 +115,7 @@ describe("warnings and recommendations", () => {
115115

116116
await workspace.yarn.add("react");
117117
const { stderr } = await workspace.yarn.add("lodash");
118-
expect(stderr).toContain("lodash@catalog:stable");
118+
expect(stderr).not.toContain("lodash@catalog:stable");
119119

120120
const { stdout: listOutput } = await workspace.yarn.info();
121121
const dependencies = extractDependencies(listOutput);
@@ -178,4 +178,43 @@ describe("warnings and recommendations", () => {
178178
const dependencies = extractDependencies(listOutput);
179179
expect(dependencies).includes("react@npm:17.0.0");
180180
});
181+
182+
it("should us default alias group without validation error (default: max)", async () => {
183+
workspace = await createTestWorkspace();
184+
185+
await workspace.writeYarnrc({
186+
catalogs: {
187+
options: {
188+
default: "max",
189+
},
190+
list: {
191+
beta: {
192+
react: "npm:18.0.0",
193+
lodash: "npm:3.0.0",
194+
},
195+
stable: {
196+
react: "npm:17.0.0",
197+
lodash: "npm:2.0.0",
198+
},
199+
},
200+
},
201+
});
202+
203+
await workspace.writeJson("package.json", {
204+
name: "test-workspace",
205+
version: "1.0.0",
206+
private: true,
207+
dependencies: {
208+
react: "catalog:stable",
209+
lodash: "catalog:stable",
210+
},
211+
});
212+
213+
const { stderr } = await workspace.yarn.add("lodash");
214+
expect(stderr).toBe("");
215+
216+
const { stdout: listOutput } = await workspace.yarn.info();
217+
const dependencies = extractDependencies(listOutput);
218+
expect(dependencies).includes("lodash@npm:2.0.0");
219+
});
181220
});

sources/utils/default.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export async function fallbackDefaultAliasGroup(
6060
/**
6161
* Get the default alias group from the configuration if it exists
6262
*/
63-
async function getDefaultAliasGroups(workspace: Workspace): Promise<string[]> {
63+
export async function getDefaultAliasGroups(
64+
workspace: Workspace,
65+
): Promise<string[]> {
6466
const config = await configReader.readConfiguration(workspace.project);
6567

6668
if (config.options) {

sources/utils/resolution.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ export async function resolveCatalogDependency(
7070
*/
7171
export async function findAllGroupsWithSpecificDependency(
7272
project: Project,
73-
dependency: Descriptor,
73+
packageName: string,
7474
): Promise<Array<{ groupName: string; version: string }>> {
75-
const dependencyString = structUtils.stringifyIdent(dependency);
7675
const config = await configReader.readConfiguration(project);
7776
const results: Array<{ groupName: string; version: string }> = [];
7877

@@ -81,7 +80,7 @@ export async function findAllGroupsWithSpecificDependency(
8180
const resolvedVersion = resolveInheritedRange(
8281
config,
8382
groupName,
84-
dependencyString,
83+
packageName,
8584
);
8685

8786
if (resolvedVersion) {

sources/utils/validation.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getInheritanceChain } from "./functions";
44
import { configReader } from "../configuration";
55
import { findAllGroupsWithSpecificDependency } from "./resolution";
66
import { CATALOG_PROTOCOL } from "../constants";
7+
import { getDefaultAliasGroups } from "./default";
78

89
/**
910
* Check if a package can be used with the catalog protocol
@@ -25,18 +26,29 @@ export async function validateCatalogUsability(
2526
return null;
2627
}
2728

29+
const defaultAliasGroups = await getDefaultAliasGroups(workspace);
30+
2831
// Find all groups that can access this package
29-
const accessibleGroups = (
30-
await findAllGroupsWithSpecificDependency(workspace.project, descriptor)
31-
).map(({ groupName }) => groupName);
32+
const packageName = structUtils.stringifyIdent(descriptor);
33+
const groupsWithDependency = await findAllGroupsWithSpecificDependency(
34+
workspace.project,
35+
packageName,
36+
);
37+
const accessibleGroups = groupsWithDependency.flatMap(({ groupName }) =>
38+
defaultAliasGroups.length === 0 || defaultAliasGroups.includes(groupName)
39+
? [groupName]
40+
: [],
41+
);
3242

33-
// Return null if no applicable groups found
3443
if (accessibleGroups.length === 0) {
3544
return null;
3645
}
3746

3847
// Get validation level for the package
39-
const validationLevel = await getPackageVaidationLevel(workspace, descriptor);
48+
const validationLevel = await getPackageVaidationLevel(
49+
workspace,
50+
packageName,
51+
);
4052

4153
return {
4254
validationLevel,
@@ -114,10 +126,10 @@ async function getGroupValidationLevel(
114126
*/
115127
async function getPackageVaidationLevel(
116128
workspace: Workspace,
117-
descriptor: Descriptor,
129+
packageName: string,
118130
): Promise<ValidationLevel> {
119131
const accessibleGroups = (
120-
await findAllGroupsWithSpecificDependency(workspace.project, descriptor)
132+
await findAllGroupsWithSpecificDependency(workspace.project, packageName)
121133
).map(({ groupName }) => groupName);
122134

123135
if (accessibleGroups.length === 0) {

0 commit comments

Comments
 (0)