Skip to content

Commit 78825ba

Browse files
authored
fix(integrity): dedupe resolved packageIds at the source (#506) (#509)
Fixes #506. Alternative to #507. ## What changes for users `mops install` (and any flow that runs `--lock check`) no longer fails with `Mismatched number of resolved packages: N vs M` on projects whose resolved deps include multiple aliases (e.g. `base`, `base@0`, `base@0.16`) pinned to the same `name@version`. Previously the only workaround was `--lock ignore`. ## Root cause `getResolvedMopsPackageIds` returned a list with duplicates: `resolvePackages()` keeps each alias as its own entry, then `getPackageId` collapses the alias suffix via `getDepName`, so two aliases pinning the same `name@version` produced the same id twice. `mops.lock`'s `hashes` is keyed by packageId and naturally dedup'd, so the count comparison in `checkLockFile` mismatched. ## Why this approach over #507 #507 dedupes only at the count comparison. This PR dedupes inside `getResolvedMopsPackageIds`, which (a) makes the function's contract match its name, and (b) stops sending duplicate ids to the canister's `getFileHashesByPackageIds`. Downstream membership checks (`includes`, `in`) were already dedupe-safe, so no other call site needed changes. ## Test plan Added a regression fixture pinning `core` and `core@1` to `1.0.0` plus a test asserting `mops install` exits clean and stderr doesn't contain the mismatch error. Verified that reverting the fix makes the Jest test fail with the expected error string.
1 parent 37bd049 commit 78825ba

4 files changed

Lines changed: 27 additions & 1 deletion

File tree

cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Mops CLI Changelog
22

33
## Next
4+
- Fix `mops install` (and any `--lock check` flow) failing with "Mismatched number of resolved packages" when a project's resolved dependencies include multiple aliases (e.g. `base`, `base@0`, `base@0.16`) that pin to the same `name@version`
45

56
## 2.12.1
67
- `mops check`/`build`/`check-stable` skip migration staging when only the pending `next` migration is needed, so `moc` diagnostics reference the real `next-migration/<file>` path.

cli/integrity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ async function getResolvedMopsPackageIds(): Promise<string[]> {
6363
let packageIds = Object.entries(resolvedPackages)
6464
.filter(([_, version]) => getDependencyType(version) === "mops")
6565
.map(([name, version]) => getPackageId(name, version));
66-
return packageIds;
66+
// dedupe: aliases like `base@0`, `base@0.16` collapse to the same packageId
67+
return [...new Set(packageIds)];
6768
}
6869

6970
// get hash of local file from '.mops' dir by fileId

cli/tests/cli.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,25 @@ describe("install", () => {
7171

7272
// mops add/remove/update/sync are not separately tested here because they
7373
// all route through the same checkIntegrity code path tested above.
74+
75+
// Regression: aliases pinning the same package@version (e.g. `core` and
76+
// `core@1` both at "1.0.0") inflated the resolved-packageIds count and
77+
// tripped the lockfile integrity check with a spurious
78+
// "Mismatched number of resolved packages" error. See issue #506.
79+
test("integrity check passes when aliases resolve to the same package@version", async () => {
80+
const cwd = path.join(import.meta.dirname, "install/aliases");
81+
const lockFile = path.join(cwd, "mops.lock");
82+
rmSync(lockFile, { force: true });
83+
try {
84+
const result = await cli(["install"], { cwd, env: { CI: undefined } });
85+
expect(result.stderr).not.toMatch(
86+
/Mismatched number of resolved packages/,
87+
);
88+
expect(result.exitCode).toBe(0);
89+
expect(existsSync(lockFile)).toBe(true);
90+
} finally {
91+
rmSync(lockFile, { force: true });
92+
rmSync(path.join(cwd, ".mops"), { recursive: true, force: true });
93+
}
94+
});
7495
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[dependencies]
2+
core = "1.0.0"
3+
"core@1" = "1.0.0"

0 commit comments

Comments
 (0)