Skip to content

Commit 7c8b883

Browse files
committed
Fix: bug in semver check on installProjectDependencies
1 parent 942d9af commit 7c8b883

File tree

2 files changed

+69
-2
lines changed
  • v-next/hardhat

2 files changed

+69
-2
lines changed

v-next/hardhat/src/internal/cli/init/init.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ export async function installProjectDependencies(
484484
const workspaceVersion = workspaceDependencies[name];
485485
return (
486486
workspaceVersion !== undefined &&
487-
!semver.satisfies(version, workspaceVersion) &&
488-
!semver.intersects(version, workspaceVersion)
487+
!semver.satisfies(workspaceVersion, version) &&
488+
!semver.intersects(workspaceVersion, version)
489489
);
490490
})
491491
.map(([name, version]) => `${name}@${version}`);

v-next/hardhat/test/internal/cli/init/init.ts

+67
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Template } from "../../../../src/internal/cli/init/template.js";
12
import type { PackageJson } from "@nomicfoundation/hardhat-utils/package";
23

34
import assert from "node:assert/strict";
@@ -311,6 +312,72 @@ describe("installProjectDependencies", async () => {
311312
}
312313
},
313314
);
315+
316+
it(
317+
"should not update dependencies if they are up-to-date and the user opts-in to the update (specific version)",
318+
{
319+
skip: process.env.HARDHAT_DISABLE_SLOW_TESTS === "true",
320+
},
321+
async () => {
322+
const template: Template = {
323+
name: "test",
324+
packageJson: {
325+
name: "test",
326+
version: "0.0.1",
327+
devDependencies: { hardhat: "^3.0.0-next.0" }, // <-- required version
328+
},
329+
path: process.cwd(),
330+
files: [],
331+
};
332+
333+
await writeUtf8File(
334+
"package.json",
335+
JSON.stringify({
336+
type: "module",
337+
devDependencies: { hardhat: "3.0.0-next.0" }, // <-- specific version
338+
}),
339+
);
340+
await installProjectDependencies(process.cwd(), template, false, true);
341+
342+
assert.ok(
343+
!(await exists("node_modules")),
344+
"no modules should have been installed",
345+
);
346+
},
347+
);
348+
349+
it(
350+
"should not update dependencies if they are up-to-date and the user opts-in to the update (version range)",
351+
{
352+
skip: process.env.HARDHAT_DISABLE_SLOW_TESTS === "true",
353+
},
354+
async () => {
355+
const template: Template = {
356+
name: "test",
357+
packageJson: {
358+
name: "test",
359+
version: "0.0.1",
360+
devDependencies: { hardhat: "^3.0.0-next.0" }, // <-- required version
361+
},
362+
path: process.cwd(),
363+
files: [],
364+
};
365+
366+
await writeUtf8File(
367+
"package.json",
368+
JSON.stringify({
369+
type: "module",
370+
devDependencies: { hardhat: ">= 3.0.0-next.0" }, // <-- version range
371+
}),
372+
);
373+
await installProjectDependencies(process.cwd(), template, false, true);
374+
375+
assert.ok(
376+
!(await exists("node_modules")),
377+
"no modules should have been installed",
378+
);
379+
},
380+
);
314381
});
315382

316383
describe("initHardhat", async () => {

0 commit comments

Comments
 (0)