Skip to content

Commit c426c06

Browse files
authored
Calculate V1 resolved package identity correctly (#435)
1 parent 0a5aee6 commit c426c06

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"object": {
3+
"pins": [
4+
{
5+
"package": "cmark",
6+
"repositoryURL": "https://github.com/apple/swift-cmark.git",
7+
"state": {
8+
"branch": "main",
9+
"revision": "9c8096a23f44794bde297452d87c455fc4f76d42",
10+
"version": null
11+
}
12+
}
13+
]
14+
},
15+
"version": 1
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// swift-tools-version:5.4
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "identity-different",
8+
products: [
9+
// Products define the executables and libraries a package produces, and make them visible to other packages.
10+
.library(
11+
name: "identity-different",
12+
targets: ["identity-different"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
.package(name: "cmark", url: "https://github.com/apple/swift-cmark.git", .branch("gfm")),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21+
.target(
22+
name: "identity-different",
23+
dependencies: ["cmark"]),
24+
]
25+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public struct identity_different {
2+
public private(set) var text = "Hello, World!"
3+
4+
public init() {
5+
}
6+
}

src/SwiftPackage.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import * as vscode from "vscode";
1616
import * as fs from "fs/promises";
17+
import * as path from "path";
1718
import {
1819
buildDirectoryFromWorkspacePath,
1920
execSwift,
@@ -69,7 +70,11 @@ export class PackageResolved {
6970
const v1Json = json as PackageResolvedFileV1;
7071
this.pins = v1Json.object.pins.map(
7172
pin =>
72-
new PackageResolvedPin(pin.package.toLowerCase(), pin.repositoryURL, pin.state)
73+
new PackageResolvedPin(
74+
this.identity(pin.repositoryURL),
75+
pin.repositoryURL,
76+
pin.state
77+
)
7378
);
7479
} else if (this.version === 2) {
7580
const v2Json = json as PackageResolvedFileV2;
@@ -80,6 +85,13 @@ export class PackageResolved {
8085
throw Error("Unsupported Package.resolved version");
8186
}
8287
}
88+
89+
// Copied from `PackageIdentityParser.computeDefaultName` in
90+
// https://github.com/apple/swift-package-manager/blob/main/Sources/PackageModel/PackageIdentity.swift
91+
identity(url: string): string {
92+
const file = path.basename(url, ".git");
93+
return file.toLowerCase();
94+
}
8395
}
8496

8597
/** Swift Package.resolved file */

test/suite/SwiftPackage.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,13 @@ suite("SwiftPackage Test Suite", () => {
7272
assert.strictEqual(spmPackage.resolved.pins.length, 1);
7373
assert.strictEqual(spmPackage.resolved.pins[0].identity, "yams");
7474
}).timeout(10000);
75+
76+
test("Identity different from name", async () => {
77+
const spmPackage = await SwiftPackage.create(testAssetUri("identity-different"));
78+
assert.strictEqual(spmPackage.isValid, true);
79+
assert.strictEqual(spmPackage.dependencies.length, 1);
80+
assert(spmPackage.resolved !== undefined);
81+
assert.strictEqual(spmPackage.resolved.pins.length, 1);
82+
assert.strictEqual(spmPackage.resolved.pins[0].identity, "swift-cmark");
83+
}).timeout(10000);
7584
});

0 commit comments

Comments
 (0)