Skip to content

Fix crash with absolute path to binary artifact #3737

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 1 commit into from
Sep 13, 2021
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: 1 addition & 2 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1777,8 +1777,7 @@ extension Workspace {
for target in manifest.targets where target.type == .binary {
if let path = target.path {
// TODO: find a better way to get the base path (not via the manifest)
// the target path is validated earlier to be within the package directory
let absolutePath = manifest.path.parentDirectory.appending(RelativePath(path))
let absolutePath = try manifest.path.parentDirectory.appending(RelativePath(validating: path))
localArtifacts.append(
.local(
packageRef: packageReference,
Expand Down
39 changes: 39 additions & 0 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6811,6 +6811,45 @@ final class WorkspaceTests: XCTestCase {
}
}
}

func testBinaryArtifactsInvalidPath() throws {
let fs = localFileSystem
try testWithTemporaryDirectory { path in
let foo = path.appending(component: "foo")

try fs.writeFileContents(foo.appending(component: "Package.swift")) {
$0 <<<
"""
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "Best",
targets: [
.binaryTarget(name: "best", path: "/best.xcframework")
]
)
"""
}

let manifestLoader = ManifestLoader(toolchain: ToolchainConfiguration.default)
let sandbox = path.appending(component: "ws")
let workspace = try Workspace(
fileSystem: fs,
location: .init(forRootPackage: sandbox, fileSystem: fs),
customManifestLoader: manifestLoader,
delegate: MockWorkspaceDelegate()
)

do {
let diagnostics = DiagnosticsEngine()
try workspace.resolve(root: .init(packages: [foo]), diagnostics: diagnostics)
} catch {
XCTAssertEqual(error.localizedDescription, "invalid relative path '/best.xcframework'; relative path should not begin with '/' or '~'")
return
}
XCTFail("unexpected success")
}
}
}

struct DummyError: LocalizedError, Equatable {
Expand Down