diff --git a/src/core-open/scanners/swift-scanner.ts b/src/core-open/scanners/swift-scanner.ts index 3fb1b027..563d1bde 100644 --- a/src/core-open/scanners/swift-scanner.ts +++ b/src/core-open/scanners/swift-scanner.ts @@ -72,45 +72,159 @@ interface SwiftDependency { /** * Parse Package.swift to extract dependencies. - * - * Example: + * + * Remote and registry dependencies: * .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0") * .package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0")) * .package(url: "https://github.com/realm/realm-swift.git", exact: "10.0.0") + * .package(url: "https://github.com/apple/swift-argument-parser", "1.2.0"..<"2.0.0") + * .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", "1.10.0"..."1.17.0") + * .package(name: "Kingfisher", url: "https://github.com/onevcat/Kingfisher.git", from: "7.0.0") + * .package(id: "mona.LinkedList", from: "1.0.0") + * + * Local `.package(path:)` dependencies are ignored: they have no version to drift. */ function parsePackageSwift(content: string): SwiftDependency[] { const deps: SwiftDependency[] = []; - - // Match .package(url: "...", ...) - const packageRegex = /\.package\s*\(\s*url:\s*"([^"]+)"[^)]*?(from|exact|upToNextMajor|revision|branch):\s*"([^"]+)"/g; - + const callRe = /\.package\s*\(/g; let match: RegExpExecArray | null; - while ((match = packageRegex.exec(content)) !== null) { - const url = match[1]!; - const versionType = match[2]!; - const versionValue = match[3]!; - - // Extract package name from URL - const nameMatch = url.match(/\/([^/]+?)(\.git)?$/); - const name = nameMatch?.[1] ?? url; - - let type: SwiftDependency['type'] = 'range'; - let version = versionValue; - - if (versionType === 'exact') { - type = 'exact'; - } else if (versionType === 'branch') { - type = 'branch'; - } else if (versionType === 'revision') { - type = 'revision'; + while ((match = callRe.exec(content)) !== null) { + const openParen = match.index + match[0].length - 1; + const extracted = extractPackageCall(content, openParen); + if (!extracted) { + callRe.lastIndex = openParen + 1; + continue; } - - deps.push({ name, url, version, type }); + callRe.lastIndex = extracted.end; + const dep = dependencyFromPackageArgs(extracted.args); + if (dep) deps.push(dep); } - return deps; } +/** Args of one `.package(` call, starting at the opening parenthesis. */ +function extractPackageCall(content: string, openParen: number): { args: string; end: number } | null { + let depth = 0; + let inString = false; + let escaped = false; + for (let i = openParen; i < content.length; i++) { + const c = content[i]!; + if (inString) { + if (escaped) { + escaped = false; + continue; + } + if (c === '\\') { + escaped = true; + continue; + } + if (c === '"') inString = false; + continue; + } + if (c === '"') { + inString = true; + continue; + } + if (c === '(') depth++; + else if (c === ')') { + depth--; + if (depth === 0) return { args: content.slice(openParen + 1, i), end: i + 1 }; + } + } + return null; +} + +/** Drop `//` and block comments without eating `//` inside string literals. */ +function stripSwiftComments(input: string): string { + let out = ''; + let i = 0; + while (i < input.length) { + const c = input[i]!; + if (c === '"') { + const start = i; + i++; + while (i < input.length) { + if (input[i] === '\\') { + i += 2; + continue; + } + if (input[i] === '"') { + i++; + break; + } + i++; + } + out += input.slice(start, i); + continue; + } + if (c === '/' && input[i + 1] === '/') { + while (i < input.length && input[i] !== '\n') i++; + continue; + } + if (c === '/' && input[i + 1] === '*') { + i += 2; + while (i < input.length && !(input[i] === '*' && input[i + 1] === '/')) i++; + i = Math.min(input.length, i + 2); + continue; + } + out += c; + i++; + } + return out; +} + +function packageNameFromUrl(url: string): string { + const nameMatch = url.match(/\/([^/]+?)(\.git)?$/); + return nameMatch?.[1] ?? url; +} + +/** + * Version requirement on a `.package` call. + * `from:` / `.upToNextMajor(from:)` / `.upToNextMinor(from:)` stay ranges whose + * current spec is the declared lower bound, matching the labeled forms. + * `"lo"..<"hi"` and `"lo"..."hi"` use that same lower bound. + */ +function versionFromPackageArgs(args: string): { value: string; type: SwiftDependency['type'] } | null { + const exact = args.match(/\bexact\s*:\s*"([^"]+)"/); + if (exact?.[1]) return { value: exact[1], type: 'exact' }; + const branch = args.match(/\bbranch\s*:\s*"([^"]+)"/); + if (branch?.[1]) return { value: branch[1], type: 'branch' }; + const revision = args.match(/\brevision\s*:\s*"([^"]+)"/); + if (revision?.[1]) return { value: revision[1], type: 'revision' }; + const from = args.match(/\bfrom\s*:\s*"([^"]+)"/); + if (from?.[1]) return { value: from[1], type: 'range' }; + const halfOpen = args.match(/"([^"]+)"\s*\.\.<\s*"[^"]*"/); + if (halfOpen?.[1]) return { value: halfOpen[1], type: 'range' }; + const closed = args.match(/"([^"]+)"\s*\.\.\.\s*"[^"]*"/); + if (closed?.[1]) return { value: closed[1], type: 'range' }; + return null; +} + +function dependencyFromPackageArgs(rawArgs: string): SwiftDependency | null { + const args = stripSwiftComments(rawArgs); + // Local packages (`.package(path:)`, including `name:` + `path:`) have no registry version. + const withoutStrings = args.replace(/"(?:\\.|[^"\\])*"/g, '""'); + if (/\bpath\s*:/.test(withoutStrings)) return null; + + const version = versionFromPackageArgs(args); + if (!version) return null; + + const urlMatch = args.match(/\burl\s*:\s*"([^"]+)"/); + if (urlMatch?.[1]) { + const url = urlMatch[1]; + // Identity follows the URL, including when the deprecated `name:` label comes first. + // Package.resolved pins that identity, not the manifest's name label. + return { name: packageNameFromUrl(url), url, version: version.value, type: version.type }; + } + + const idMatch = args.match(/\bid\s*:\s*"([^"]+)"/); + if (idMatch?.[1]) { + const id = idMatch[1]; + return { name: id, url: id, version: version.value, type: version.type }; + } + return null; +} + /** * Parse Package.resolved to get exact resolved versions. * diff --git a/test/swift-package-swift.test.ts b/test/swift-package-swift.test.ts new file mode 100644 index 00000000..add7e519 --- /dev/null +++ b/test/swift-package-swift.test.ts @@ -0,0 +1,244 @@ +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { afterEach, describe, expect, it, vi } from 'vitest'; + +import { scanSwiftProjects } from '../src/core-open/scanners/swift-scanner.js'; +import { SwiftCache } from '../src/core-open/scanners/swift-cache.js'; +import { Semaphore } from '../src/core-open/utils/semaphore.js'; +import type { DependencyRow } from '../src/core-open/types.js'; + +let root: string; +afterEach(() => { + if (root) fs.rmSync(root, { recursive: true, force: true }); +}); + +function offlineCache(): SwiftCache { + const cache = new SwiftCache(new Semaphore(1), undefined, true); + vi.spyOn(cache, 'get').mockResolvedValue({ + latest: null, + stableVersions: [], + latestStableOverall: null, + }); + return cache; +} + +function manifest(dependencies: string, targets = ''): string { + return [ + '// swift-tools-version:5.9', + 'import PackageDescription', + 'let package = Package(', + ' name: "app",', + ' dependencies: [', + dependencies, + ' ]', + targets, + ')', + ].join('\n'); +} + +async function scan(dependencies: string, targets = '', resolved?: string): Promise { + root = fs.mkdtempSync(path.join(os.tmpdir(), 'swift-pkg-')); + fs.writeFileSync(path.join(root, 'Package.swift'), manifest(dependencies, targets)); + if (resolved !== undefined) fs.writeFileSync(path.join(root, 'Package.resolved'), resolved); + const projects = await scanSwiftProjects(root, offlineCache()); + expect(projects).toHaveLength(1); + return projects[0]!.dependencies; +} + +function row(deps: DependencyRow[], name: string): DependencyRow { + const found = deps.find((d) => d.package === name); + expect(found, name).toBeDefined(); + return found!; +} + +describe('Package.swift dependency forms', () => { + it.each([ + [ + 'half-open range', + '.package(url: "https://github.com/apple/swift-argument-parser", "1.2.0"..<"2.0.0"),', + 'swift-argument-parser', + '1.2.0', + ], + [ + 'half-open range with spaces', + '.package(url: "https://github.com/apple/swift-argument-parser", "1.2.0" ..< "2.0.0"),', + 'swift-argument-parser', + '1.2.0', + ], + [ + 'closed range', + '.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", "1.10.0"..."1.17.0"),', + 'swift-snapshot-testing', + '1.10.0', + ], + [ + 'closed range with spaces', + '.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", "1.10.0" ... "1.17.0"),', + 'swift-snapshot-testing', + '1.10.0', + ], + [ + 'name before url', + '.package(name: "Kingfisher", url: "https://github.com/onevcat/Kingfisher.git", from: "7.0.0"),', + 'Kingfisher', + '7.0.0', + ], + [ + 'registry id', + '.package(id: "mona.LinkedList", from: "1.0.0"),', + 'mona.LinkedList', + '1.0.0', + ], + [ + 'registry id exact', + '.package(id: "mona.LinkedList", exact: "1.1.1"),', + 'mona.LinkedList', + '1.1.1', + ], + [ + 'registry id range', + '.package(id: "mona.LinkedList", "1.0.0"..<"2.0.0"),', + 'mona.LinkedList', + '1.0.0', + ], + [ + 'from', + '.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),', + 'vapor', + '4.0.0', + ], + [ + 'exact', + '.package(url: "https://github.com/realm/realm-swift.git", exact: "10.0.0"),', + 'realm-swift', + '10.0.0', + ], + [ + 'exact with build metadata', + '.package(url: "https://github.com/example/build-meta.git", exact: "1.2.3+build.7"),', + 'build-meta', + '1.2.3+build.7', + ], + [ + 'upToNextMajor', + '.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0")),', + 'swift-nio', + '2.0.0', + ], + [ + 'upToNextMinor', + '.package(url: "https://github.com/apple/swift-log.git", .upToNextMinor(from: "1.5.0")),', + 'swift-log', + '1.5.0', + ], + [ + 'branch', + '.package(url: "https://github.com/apple/swift-collections.git", branch: "main"),', + 'swift-collections', + 'main', + ], + [ + 'revision', + '.package(url: "https://github.com/apple/swift-atomics.git", revision: "abcdef"),', + 'swift-atomics', + 'abcdef', + ], + ])('%s yields one row', async (_label, call, name, spec) => { + const deps = await scan(call); + expect(deps).toHaveLength(1); + expect(deps[0]!.package).toBe(name); + expect(deps[0]!.currentSpec).toBe(spec); + }); + + it('reads every form from one manifest and skips local path packages', async () => { + const deps = await scan([ + '.package(url: "https://github.com/apple/swift-argument-parser", "1.2.0"..<"2.0.0"),', + '.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", "1.10.0"..."1.17.0"),', + '.package(name: "Kingfisher", url: "https://github.com/onevcat/Kingfisher.git", from: "7.0.0"),', + '.package(id: "mona.LinkedList", from: "1.0.0"),', + '.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),', + '.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0")),', + '.package(url: "https://github.com/realm/realm-swift.git", exact: "10.0.0"),', + '.package(url: "https://github.com/apple/swift-collections.git", branch: "main"),', + '.package(url: "https://github.com/apple/swift-atomics.git", revision: "abcdef"),', + '.package(path: "../LocalLib"),', + '.package(name: "AnotherLocal", path: "../AnotherLocal"),', + ].join('\n'), [ + ',', + ' targets: [', + ' .target(name: "App", dependencies: [', + ' .package(name: "Kingfisher"),', + ' .product(name: "Vapor", package: "vapor"),', + ' ])', + ' ]', + ].join('\n')); + + expect(deps.map((d) => d.package).sort()).toEqual([ + 'Kingfisher', + 'mona.LinkedList', + 'realm-swift', + 'swift-argument-parser', + 'swift-atomics', + 'swift-collections', + 'swift-nio', + 'swift-snapshot-testing', + 'vapor', + ]); + expect(row(deps, 'swift-argument-parser').currentSpec).toBe('1.2.0'); + expect(row(deps, 'swift-snapshot-testing').currentSpec).toBe('1.10.0'); + expect(row(deps, 'Kingfisher').currentSpec).toBe('7.0.0'); + expect(row(deps, 'mona.LinkedList').currentSpec).toBe('1.0.0'); + expect(row(deps, 'vapor').currentSpec).toBe('4.0.0'); + expect(row(deps, 'swift-nio').currentSpec).toBe('2.0.0'); + expect(row(deps, 'realm-swift').currentSpec).toBe('10.0.0'); + expect(row(deps, 'swift-collections').currentSpec).toBe('main'); + expect(row(deps, 'swift-atomics').currentSpec).toBe('abcdef'); + expect(deps.some((d) => d.package === 'LocalLib' || d.package === 'AnotherLocal')).toBe(false); + }); + + it('keeps a multiline name-first dependency and a comment inside the call', async () => { + const deps = await scan([ + '.package(', + ' name: "Kingfisher",', + ' url: "https://github.com/onevcat/Kingfisher.git", // pinned below', + ' from: "7.0.0"', + '),', + ].join('\n')); + expect(deps).toHaveLength(1); + expect(deps[0]!.package).toBe('Kingfisher'); + expect(deps[0]!.currentSpec).toBe('7.0.0'); + }); + + it('pins resolved versions by URL identity, not the deprecated name label', async () => { + const deps = await scan( + '.package(name: "CustomName", url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),', + '', + JSON.stringify({ pins: [{ identity: 'swift-nio', state: { version: '2.65.0' } }], version: 2 }), + ); + expect(deps).toHaveLength(1); + expect(deps[0]!.package).toBe('swift-nio'); + expect(deps[0]!.currentSpec).toBe('2.0.0'); + expect(deps[0]!.resolvedVersion).toBe('2.65.0'); + }); + + it('pins a registry identity from Package.resolved', async () => { + const deps = await scan( + '.package(id: "mona.LinkedList", from: "1.0.0"),', + '', + JSON.stringify({ pins: [{ identity: 'mona.linkedlist', state: { version: '1.2.0' } }], version: 2 }), + ); + expect(deps).toHaveLength(1); + expect(deps[0]!.package).toBe('mona.LinkedList'); + expect(deps[0]!.currentSpec).toBe('1.0.0'); + expect(deps[0]!.resolvedVersion).toBe('1.2.0'); + }); + + it('emits no dependency rows for a path-only manifest', async () => { + const deps = await scan([ + '.package(path: "../LocalLib"),', + '.package(name: "AnotherLocal", path: "../AnotherLocal"),', + ].join('\n')); + expect(deps).toEqual([]); + }); +});