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

Add canonical package location to SwiftPMCacheKey to differentiate the same target when its package is changed from remote one to local one (or vice-versa) #168

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 Sources/ScipioKit/Producer/Cache/CacheSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extension PinsStore.PinState: @retroactive Hashable {
}

public struct SwiftPMCacheKey: CacheKey {
public var canonicalPackageLocation: String
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about the remote URL being embedded. If it's used in a mirrored repository, all caches will be expired. I think it's better to be it Optional and it should have only for local packages.

Copy link
Collaborator Author

@ikesyo ikesyo Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, I addressed this in 0f6e237.

public var targetName: String
public var pin: PinsStore.PinState
var buildOptions: BuildOptions
Expand Down Expand Up @@ -230,6 +231,7 @@ struct CacheSystem: Sendable {
}

func calculateCacheKey(of target: CacheTarget) async throws -> SwiftPMCacheKey {
let canonicalPackageLocation = target.buildProduct.package.manifest.canonicalPackageLocation
let targetName = target.buildProduct.target.name
let pin = try retrievePin(package: target.buildProduct.package)
let buildOptions = target.buildOptions
Expand All @@ -240,6 +242,7 @@ struct CacheSystem: Sendable {
throw Error.xcodeVersionNotDetected
}
return SwiftPMCacheKey(
canonicalPackageLocation: canonicalPackageLocation.description,
targetName: targetName,
pin: pin.state,
buildOptions: buildOptions,
Expand Down
74 changes: 74 additions & 0 deletions Tests/ScipioKitTests/CacheSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class CacheSystemTests: XCTestCase {

func testEncodeCacheKey() throws {
let cacheKey = SwiftPMCacheKey(
canonicalPackageLocation: "/path/to/MyPackage",
targetName: "MyTarget",
pin: .revision("111111111"),
buildOptions: .init(
Expand Down Expand Up @@ -63,6 +64,7 @@ final class CacheSystemTests: XCTestCase {
"iOS"
]
},
"canonicalPackageLocation" : "\\/path\\/to\\/MyPackage",
"clangVersion" : "clang-1400.0.29.102",
"pin" : {
"revision" : "111111111"
Expand All @@ -78,6 +80,78 @@ final class CacheSystemTests: XCTestCase {
XCTAssertEqual(rawString, expected)
}

func testCacheKeyForRemoteAndLocalPackageDifference() async throws {
let fileSystem = localFileSystem

let tempDir = try fileSystem.tempDirectory.appending(#function)
try fileSystem.removeFileTree(tempDir)
try fileSystem.createDirectory(tempDir)

defer { try? fileSystem.removeFileTree(tempDir) }

let tempCacheKeyTestsDir = tempDir.appending(component: "CacheKeyTests").scipioAbsolutePath
try fileSystem.copy(
from: fixturePath.appending(component: "CacheKeyTests").absolutePath,
to: tempCacheKeyTestsDir
)

// For local package consumption
let executor = ProcessExecutor()
_ = try await executor.execute([
"git",
"clone",
"https://github.com/giginet/scipio-testing",
tempDir.appending(component: "scipio-testing").pathString,
"-b",
"3.0.0",
"--depth",
"1",
])

func scipioTestingCacheKey(fixture: String) async throws -> SwiftPMCacheKey {
let descriptionPackage = try DescriptionPackage(
packageDirectory: tempCacheKeyTestsDir.appending(component: fixture),
mode: .createPackage,
onlyUseVersionsFromResolvedFile: false
)
let package = descriptionPackage
.graph
.packages
.first { $0.manifest.displayName == "scipio-testing" }!
let target = package.modules.first { $0.name == "ScipioTesting" }!
let cacheTarget = CacheSystem.CacheTarget(
buildProduct: BuildProduct(
package: package,
target: target
),
buildOptions: BuildOptions(
buildConfiguration: .release,
isDebugSymbolsEmbedded: false,
frameworkType: .dynamic,
sdks: [.iOS, .iOSSimulator],
extraFlags: nil,
extraBuildParameters: nil,
enableLibraryEvolution: false,
keepPublicHeadersStructure: false,
customFrameworkModuleMapContents: nil
)
)

let cacheSystem = CacheSystem(
pinsStore: try descriptionPackage.workspace.pinsStore.load(),
outputDirectory: FileManager.default.temporaryDirectory.appendingPathComponent("XCFrameworks")
)
return try await cacheSystem.calculateCacheKey(of: cacheTarget)
}

let scipioTestingRemote = try await scipioTestingCacheKey(fixture: "AsRemotePackage")
let scipioTestingLocal = try await scipioTestingCacheKey(fixture: "AsLocalPackage")

XCTAssertNotEqual(scipioTestingRemote.canonicalPackageLocation, scipioTestingLocal.canonicalPackageLocation)
XCTAssertEqual(scipioTestingRemote.targetName, scipioTestingLocal.targetName)
XCTAssertEqual(scipioTestingRemote.pin, scipioTestingLocal.pin)
}

func testCacheKeyCalculationForRootPackageTarget() async throws {
let fileSystem = localFileSystem
let testingPackagePath = fixturePath.appendingPathComponent("TestingPackage")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "AsLocalPackage",
products: [
.library(
name: "Foo",
targets: ["Foo"]
),
],
dependencies: [
.package(path: "../../scipio-testing"),
],
targets: [
.target(
name: "Foo",
dependencies: [
.product(name: "ScipioTesting", package: "scipio-testing"),
]
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "AsRemotePackage",
products: [
.library(
name: "Foo",
targets: ["Foo"]
),
],
dependencies: [
.package(url: "https://github.com/giginet/scipio-testing.git", exact: "3.0.0"),
],
targets: [
.target(
name: "Foo",
dependencies: [
.product(name: "ScipioTesting", package: "scipio-testing"),
]
),
]
)