Skip to content

Commit ef59312

Browse files
authored
Merge pull request #7115 from NomicFoundation/change-the-type-of-package-name
Change type of packageName
2 parents b69bb7f + b8a5881 commit ef59312

File tree

8 files changed

+81
-5
lines changed

8 files changed

+81
-5
lines changed

.changeset/wicked-lies-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Change the type of the `npmPackage` plugin property ([7058](https://github.com/NomicFoundation/hardhat/issues/7058))

v-next/hardhat/src/internal/core/config-validation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ export function validatePluginsConfig(
603603

604604
if (
605605
plugin.npmPackage !== undefined &&
606-
typeof plugin.npmPackage !== "string"
606+
typeof plugin.npmPackage !== "string" &&
607+
plugin.npmPackage !== null
607608
) {
608609
validationErrors.push({
609610
path: [...path, "plugins", index, "npmPackage"],

v-next/hardhat/src/internal/core/plugins/detect-plugin-npm-dependency-problems.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ export async function detectPluginNpmDependencyProblems(
2727
basePathForNpmResolution: string,
2828
plugin: HardhatPlugin,
2929
): Promise<void> {
30-
if (plugin.npmPackage === undefined) {
30+
if (plugin.npmPackage === null) {
3131
return;
3232
}
3333

3434
const pluginPackageJsonPath = await findDependencyPackageJson(
3535
basePathForNpmResolution,
36-
plugin.npmPackage,
36+
// When npmPackage is undefined, we use the id as the package name instead
37+
plugin.npmPackage ?? plugin.id,
3738
);
3839

3940
if (pluginPackageJsonPath === undefined) {

v-next/hardhat/src/types/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface HardhatPlugin {
3232
/**
3333
* The npm package where the plugin is located, if any.
3434
*/
35-
npmPackage?: string;
35+
npmPackage?: string | null;
3636

3737
/**
3838
* An array of plugins that this plugin depends on.

v-next/hardhat/test/internal/core/config-validation.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,39 @@ describe("config validation", function () {
14321432
});
14331433

14341434
describe("validatePluginsConfig", function () {
1435+
it("should not throw when the npmPackage is a string", function () {
1436+
const plugins: HardhatPlugin[] = [
1437+
{
1438+
id: "plugin-id",
1439+
npmPackage: "npm-package",
1440+
},
1441+
];
1442+
1443+
assert.deepEqual(validatePluginsConfig(plugins, []), []);
1444+
});
1445+
1446+
it("should not throw when the npmPackage is null", function () {
1447+
const plugins: HardhatPlugin[] = [
1448+
{
1449+
id: "plugin-id",
1450+
npmPackage: null,
1451+
},
1452+
];
1453+
1454+
assert.deepEqual(validatePluginsConfig(plugins, []), []);
1455+
});
1456+
1457+
it("should not throw when the npmPackage is undefined", function () {
1458+
const plugins: HardhatPlugin[] = [
1459+
{
1460+
id: "plugin-id",
1461+
npmPackage: undefined,
1462+
},
1463+
];
1464+
1465+
assert.deepEqual(validatePluginsConfig(plugins, []), []);
1466+
});
1467+
14351468
it("should return an error if a plugin is not a non-null object", function () {
14361469
const plugins: HardhatPlugin[] = [
14371470
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- testing validations for js users who can bypass type checks */

v-next/hardhat/test/internal/core/hook-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ describe("HookManager", () => {
428428
hookHandlers: {
429429
config: import.meta.resolve("./non-existent.js"),
430430
},
431+
npmPackage: null,
431432
};
432433

433434
const manager = new HookManagerImplementation(projectRoot, [

v-next/hardhat/test/internal/core/plugins/detect-plugin-npm-dependency-problems.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ describe("Plugins - detect npm dependency problems", () => {
1313
npmPackage: "example",
1414
};
1515

16+
const pluginWithNpmPackageUndefined: HardhatPlugin = {
17+
id: "example",
18+
npmPackage: undefined,
19+
};
20+
1621
it("should skip validation if the plugin is not from an npm package", async () => {
1722
const peerDepWithWrongVersionFixture = import.meta.resolve(
1823
"./fixture-projects/peer-dep-with-wrong-version",
1924
);
2025

2126
await detectPluginNpmDependencyProblems(peerDepWithWrongVersionFixture, {
2227
...plugin,
23-
npmPackage: undefined,
28+
npmPackage: null,
2429
});
2530
});
2631

@@ -36,6 +41,17 @@ describe("Plugins - detect npm dependency problems", () => {
3641
);
3742
});
3843

44+
it("should pass validation if the package has been installed and the npmPackage property is undefined but the id matches the installed package", async () => {
45+
const installedPackageProjectFixture = import.meta.resolve(
46+
"./fixture-projects/installed-package",
47+
);
48+
49+
await detectPluginNpmDependencyProblems(
50+
installedPackageProjectFixture,
51+
pluginWithNpmPackageUndefined,
52+
);
53+
});
54+
3955
it("should fail validation if the npm package has not been installed", async () => {
4056
const nonInstalledPackageProjectFixture = import.meta.resolve(
4157
"./fixture-projects/not-installed-package",
@@ -53,6 +69,24 @@ describe("Plugins - detect npm dependency problems", () => {
5369
},
5470
);
5571
});
72+
73+
it("should fail validation if the package has been installed and the npmPackage property is undefined but the id has a value that does not match the installed package", async () => {
74+
const nonInstalledPackageProjectFixture = import.meta.resolve(
75+
"./fixture-projects/installed-package",
76+
);
77+
78+
await assertRejectsWithHardhatError(
79+
async () =>
80+
detectPluginNpmDependencyProblems(nonInstalledPackageProjectFixture, {
81+
...pluginWithNpmPackageUndefined,
82+
id: "does-not-match-installed-plugin",
83+
}),
84+
HardhatError.ERRORS.CORE.PLUGINS.PLUGIN_NOT_INSTALLED,
85+
{
86+
pluginId: "does-not-match-installed-plugin",
87+
},
88+
);
89+
});
5690
});
5791

5892
describe("when the plugin has peer deps", () => {

v-next/hardhat/test/internal/core/tasks/task-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,7 @@ describe("TaskManagerImplementation", () => {
20962096
.setAction("file://not-a-module")
20972097
.build(),
20982098
],
2099+
npmPackage: null,
20992100
},
21002101
],
21012102
},

0 commit comments

Comments
 (0)