Skip to content

Calculate V1 resolved package identity correctly #435

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

Merged
merged 3 commits into from
Oct 27, 2022
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
16 changes: 16 additions & 0 deletions assets/test/identity-different/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "cmark",
"repositoryURL": "https://github.com/apple/swift-cmark.git",
"state": {
"branch": "main",
"revision": "9c8096a23f44794bde297452d87c455fc4f76d42",
"version": null
}
}
]
},
"version": 1
}
25 changes: 25 additions & 0 deletions assets/test/identity-different/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "identity-different",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "identity-different",
targets: ["identity-different"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "cmark", url: "https://github.com/apple/swift-cmark.git", .branch("gfm")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "identity-different",
dependencies: ["cmark"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public struct identity_different {
public private(set) var text = "Hello, World!"

public init() {
}
}
14 changes: 13 additions & 1 deletion src/SwiftPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import * as vscode from "vscode";
import * as fs from "fs/promises";
import * as path from "path";
import {
buildDirectoryFromWorkspacePath,
execSwift,
Expand Down Expand Up @@ -69,7 +70,11 @@ export class PackageResolved {
const v1Json = json as PackageResolvedFileV1;
this.pins = v1Json.object.pins.map(
pin =>
new PackageResolvedPin(pin.package.toLowerCase(), pin.repositoryURL, pin.state)
new PackageResolvedPin(
this.identity(pin.repositoryURL),
pin.repositoryURL,
pin.state
)
);
} else if (this.version === 2) {
const v2Json = json as PackageResolvedFileV2;
Expand All @@ -80,6 +85,13 @@ export class PackageResolved {
throw Error("Unsupported Package.resolved version");
}
}

// Copied from `PackageIdentityParser.computeDefaultName` in
// https://github.com/apple/swift-package-manager/blob/main/Sources/PackageModel/PackageIdentity.swift
identity(url: string): string {
const file = path.basename(url, ".git");
return file.toLowerCase();
}
}

/** Swift Package.resolved file */
Expand Down
9 changes: 9 additions & 0 deletions test/suite/SwiftPackage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ suite("SwiftPackage Test Suite", () => {
assert.strictEqual(spmPackage.resolved.pins.length, 1);
assert.strictEqual(spmPackage.resolved.pins[0].identity, "yams");
}).timeout(10000);

test("Identity different from name", async () => {
const spmPackage = await SwiftPackage.create(testAssetUri("identity-different"));
assert.strictEqual(spmPackage.isValid, true);
assert.strictEqual(spmPackage.dependencies.length, 1);
assert(spmPackage.resolved !== undefined);
assert.strictEqual(spmPackage.resolved.pins.length, 1);
assert.strictEqual(spmPackage.resolved.pins[0].identity, "swift-cmark");
}).timeout(10000);
});