Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle identical dependencies correctly #175

Merged
merged 3 commits into from
May 14, 2024
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: 3 additions & 0 deletions cli/cli_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ molt("mod.ts --no-resolve", { dir: "multiple_modules" });
molt("mod.ts", { dir: "jsr_with_path_in_import_map" });
molt("deno.json", { dir: "jsr_with_path_in_import_map" });

// identical dependencies with and without a range specifier
molt("deno.json", { dir: "import_map_duplicated_imports" });

//-----------------------
// Test implementation
//-----------------------
Expand Down
4 changes: 2 additions & 2 deletions cli/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@molt/cli",
"version": "0.18.3",
"version": "0.18.4",
"exports": {
".": "./main.ts"
},
Expand All @@ -21,7 +21,7 @@
"@core/unknownutil": "jsr:@core/unknownutil@^3.18.0",
"@david/dax": "jsr:@david/dax@^0.40.0",
"@molt/lib": "jsr:@molt/lib@^0.18.0",
"@molt/core": "jsr:@molt/core@^0.18.3",
"@molt/core": "jsr:@molt/core@^0.18.4",
"@molt/integration": "jsr:@molt/integration@^0.18.0",
"@std/assert": "jsr:@std/assert@^0.222.1",
"@std/collections": "jsr:@std/collections@^0.222.1",
Expand Down
2 changes: 1 addition & 1 deletion core/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@molt/core",
"version": "0.18.3",
"version": "0.18.4",
"exports": {
".": "./mod.ts"
},
Expand Down
14 changes: 7 additions & 7 deletions core/dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ export async function resolveLatestVersion(
dependency: Dependency,
options?: { cache?: boolean },
): Promise<UpdatedDependency | undefined> {
const constraint = dependency.version
? SemVer.tryParseRange(dependency.version)
: undefined;
// Do not update inequality ranges.
if (constraint && constraint.flat().length > 1) {
return;
}
using cache = options?.cache
? new LatestVersionCache(dependency.name)
: undefined;
Expand All @@ -177,13 +184,6 @@ export async function resolveLatestVersion(
// The dependency is already found to be up to date or unable to resolve.
return;
}
const constraint = dependency.version
? SemVer.tryParseRange(dependency.version)
: undefined;
// Do not update inequality ranges.
if (constraint && constraint.flat().length > 1) {
return;
}
const result = await _resolveLatestVersion(dependency);
cache?.set(dependency.name, result ?? null);
return result;
Expand Down
17 changes: 17 additions & 0 deletions core/import_map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ describe("resolve()", () => {
);
});

it("resolve a jsr specifier with a caret", async () => {
const dir = "../test/cases/import_map_duplicated_imports";
const importMap = await readFromJson(
new URL(`${dir}/deno.json`, import.meta.url),
);
assertExists(importMap);
const referrer = new URL(`${dir}/mod.ts`, import.meta.url);
assertEquals(
importMap.resolve("@std/testing", referrer),
{
resolved: "jsr:@std/testing@^0.210.0",
key: "@std/testing",
value: "jsr:@std/testing@^0.210.0",
},
);
});

it("resolve a jsr specifier with path", async () => {
const dir = "../test/cases/jsr_with_path_in_import_map";
const importMap = await readFromJson(
Expand Down
6 changes: 6 additions & 0 deletions test/cases/import_map_duplicated_imports/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"imports": {
"@std/testing": "jsr:@std/testing@^0.210.0",
"@std/testing/bdd": "jsr:@std/testing@0.210.0/bdd"
}
}
1 change: 1 addition & 0 deletions test/cases/import_map_duplicated_imports/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { describe } from "@std/testing/bdd";
12 changes: 11 additions & 1 deletion test/snapshots/cli_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Options:
snapshot[`cli - "molt --help" 2`] = `""`;

snapshot[`cli - "molt --version" 1`] = `
"0.18.3
"0.18.4
"
`;

Expand Down Expand Up @@ -454,3 +454,13 @@ snapshot[`cli - jsr_with_path_in_import_map - "molt deno.json" 2`] = `
"Checking for updates
"
`;

snapshot[`cli - import_map_duplicated_imports - "molt deno.json" 1`] = `
"📦 @std/testing 0.210.0 => 0.218.2
"
`;

snapshot[`cli - import_map_duplicated_imports - "molt deno.json" 2`] = `
"Checking for updates
"
`;
97 changes: 97 additions & 0 deletions test/snapshots/file_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,103 @@ snapshot[`write - import_map/mod.ts 2`] = `
]
`;

snapshot[`write - import_map_duplicated_imports/deno.json 1`] = `
[
{
dependencies: [
{
code: {
span: undefined,
specifier: "jsr:@std/testing@0.210.0/bdd",
},
from: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "0.210.0",
},
map: {
key: "@std/testing/bdd",
resolved: "jsr:@std/testing@0.210.0/bdd",
},
to: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "123.456.789",
},
},
],
kind: "import_map",
},
]
`;

snapshot[`write - import_map_duplicated_imports/deno.json 2`] = `
[
'{
"imports": {
"@std/testing": "jsr:@std/testing@^0.210.0",
"@std/testing/bdd": "jsr:@std/testing@123.456.789/bdd"
}
}
',
]
`;

snapshot[`write - import_map_duplicated_imports/mod.ts 1`] = `
[
{
dependencies: [
{
code: {
span: {
end: {
character: 43,
line: 0,
},
start: {
character: 25,
line: 0,
},
},
specifier: "@std/testing/bdd",
},
from: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "0.210.0",
},
map: {
key: "@std/testing/bdd",
resolved: "jsr:@std/testing@0.210.0/bdd",
},
to: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "123.456.789",
},
},
],
kind: "import_map",
},
]
`;

snapshot[`write - import_map_duplicated_imports/mod.ts 2`] = `
[
'{
"imports": {
"@std/testing": "jsr:@std/testing@^0.210.0",
"@std/testing/bdd": "jsr:@std/testing@123.456.789/bdd"
}
}
',
]
`;

snapshot[`write - npm.ts 1`] = `
[
{
Expand Down
55 changes: 55 additions & 0 deletions test/snapshots/update_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,61 @@ snapshot[`collect - import_map - mod.ts 1`] = `
]
`;

snapshot[`collect - import_map_duplicated_imports - deno.json 1`] = `
[
{
code: {
span: undefined,
specifier: "jsr:@std/testing@0.210.0/bdd",
},
from: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "0.210.0",
},
to: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "123.456.789",
},
},
]
`;

snapshot[`collect - import_map_duplicated_imports - mod.ts 1`] = `
[
{
code: {
span: {
end: {
character: 43,
line: 0,
},
start: {
character: 25,
line: 0,
},
},
specifier: "@std/testing/bdd",
},
from: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "0.210.0",
},
to: {
name: "@std/testing",
path: "/bdd",
protocol: "jsr:",
version: "123.456.789",
},
},
]
`;

snapshot[`collect - npm.ts 1`] = `
[
{
Expand Down