'no such file' errors with ArgumentParser package

I'm trying to create a command line tool, so obviously want to use ArgumentParser. I've added the Package to my project, (as far as I can tell), and initially, when I build, I get these errors:

dyld[20971]: Library not loaded: @rpath/ArgumentParser.framework/Versions/A/ArgumentParser
Referenced from: <5CBCAC0B-63B2-3F9B-AAD2-D77C82D3BD1F> /Users/ben/Library/Developer/Xcode/DerivedData/pdfutil-cpoebwvnlxwjfxgnkrbkoskgcfhz/Build/Products/Debug/pdfutil
Reason: tried: '/System/Volumes/Preboot/Cryptexes/OS@rpath/ArgumentParser.framework/Versions/A/ArgumentParser' (no such file), '/Library/Frameworks/ArgumentParser.framework/Versions/A/ArgumentParser' (no such file), '/System/Library/Frameworks/ArgumentParser.framework/Versions/A/ArgumentParser' (no such file, not in dyld cache)

If I set the Build Options to Disable Library Validation, then the tool runs in Xcode, but the compiled executable still gives the same errors in Terminal.

I've modified very few build settings from the defaults. The project is signed locally.

Am I missing some other build setting, or something else? Thanks for any help.

I'm on Ventura 13.2, Xcode 14.1.

Have you tried developing the command line tool as a SwiftPM package, with a standalone Package.swift? I don't think these errors are reproducible with a package with an executable target created with swift init --type executabe and build with swift build. I assume you're currently developing it with an .xcodeproj and targets declared within it?

Thanks for the reply -- you mean 'don't use Xcode'...? :rofl:

OK, I'll try that and see if I can get any further.

That's not what I mean, you can still open Package.swift with Xcode, which will open not just that manifest file, but the whole package for development, building, and testing. Diagnosing issues with your Package.swift is much easier though, as you'll be able to just paste a snippet here if something goes wrong, and you won't need to manage build settings separately.

Sorry, then I don't know what you mean. My tool itself should be a Package? Or I should create my own 'standalone' version of the ArgumentParser package?

My project is very basic -- I've got a main.swift file, and this package. That's it.

Navigate to an empty directory, then run swift package init --type executable, and xed Package.swift after that. Add ArgumentParser dependency in Package.swift. After package resolution has completed successfully, you can paste your code into a file with @main declaration of this new package to replace "Hello, World!" sample code.

Like this?
The button to build is unavailable.

I get an error "Source files for target ArgumentParser should be located under 'Sources/ArgumentParser', or a custom sources path can be set with the 'path' property in Package.swift"

I've got no target, and File > New doesn't offer Target as a choice.

Surely, creating a new Command Line tool in Xcode and Adding the Package should ... 'just work'?

Ah: the errors go away if I copy the Frameworks from the Build folder to /Library/Frameworks.

Any way to make them part of my executable, so that users don't have install stuff?

Can anyone confirm whether the same thing happens to them, or whether this is a unique problem at my end?

I tried @Max_Desiatov 's command line method again. But it doesn't actually add the ArgumentParser package into the project it creates. And if I try to Add the Package from the Package Manager, it won't let me.

If I try to copy over the Packages.swift file or Sources > ArgumentParser from my original project, the build icon gets crossed out and doesn't work. Not sure I know how "package resolution" is supposed to "take place".

It won't be added automatically. It's up to the developer to add dependencies they want to use in the package. You'll have to edit your Package.swift as described in ArgumentParser's README.md, replacing <command-line-tool> with the name of your executable target.

Yes, and once I add that code, the Build icon goes grey and crossed out.

Try building your package with swift build in the terminal. In case there are any errors in package resolution they may be more discoverable in the command line.

It says:

Source files for target ArgumentParser should be located under 'Sources/ArgumentParser', or a custom sources path can be set with the 'path' property in Package.swift

Would you be able to share contents of Package.swift that causes swift build to produce this error message?

It's copied from the Package.swift file that the Package Manager gives me in the original project:

import PackageDescription

var package = Package(
    name: "swift-argument-parser",
    products: [
        .library(
            name: "ArgumentParser",
            targets: ["ArgumentParser"]),
    ],
    dependencies: [],
    targets: [
        // Core Library
        .target(
            name: "ArgumentParser",
            dependencies: ["ArgumentParserToolInfo"],
            exclude: ["CMakeLists.txt"]),
        .target(
            name: "ArgumentParserTestHelpers",
            dependencies: ["ArgumentParser", "ArgumentParserToolInfo"],
            exclude: ["CMakeLists.txt"]),
        .target(
            name: "ArgumentParserToolInfo",
            dependencies: [],
            exclude: ["CMakeLists.txt"]),

        // Examples
        .executableTarget(
            name: "roll",
            dependencies: ["ArgumentParser"],
            path: "Examples/roll"),
        .executableTarget(
            name: "math",
            dependencies: ["ArgumentParser"],
            path: "Examples/math"),
        .executableTarget(
            name: "repeat",
            dependencies: ["ArgumentParser"],
            path: "Examples/repeat"),

        // Tests
        .testTarget(
            name: "ArgumentParserEndToEndTests",
            dependencies: ["ArgumentParser", "ArgumentParserTestHelpers"],
            exclude: ["CMakeLists.txt"]),
        .testTarget(
            name: "ArgumentParserUnitTests",
            dependencies: ["ArgumentParser", "ArgumentParserTestHelpers"],
            exclude: ["CMakeLists.txt"]),
        .testTarget(
            name: "ArgumentParserPackageManagerTests",
            dependencies: ["ArgumentParser", "ArgumentParserTestHelpers"],
            exclude: ["CMakeLists.txt"]),
        .testTarget(
            name: "ArgumentParserExampleTests",
            dependencies: ["ArgumentParserTestHelpers"],
            resources: [.copy("CountLinesTest.txt")]),
    ]
)

Thanks for providing these details! What you're sharing is Package.swift for the ArgumentParser package. You don't have to copy it anywhere and it doesn't belong to your project. You have to use Package.swift generated by swift package init --type executable and edit that by adding only two lines:

  1. this package dependency line:
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0"),

to the dependencies argument passed to Package;

  1. and this target dependency line:
 .product(name: "ArgumentParser", package: "swift-argument-parser"),

to the dependencies argument passed to .executableTarget.

Thanks for your help. This is probably basic and obvious if you're in front of it.

OK. So I now have two projects.

My original one, created in Xcode, for which the Package Manager doesn't create even a boilerplate Package.swift file; but somehow the project builds anyway and references the package when it executes in Xcode, but not outside of it.

A second one, following your instructions, which seems to work with the Hello Word code, but I can't find the target or the Build Options anywhere. When I add my own code, starting thus:

@main

import Foundation
import ArgumentParser
import Quartz

struct pdfutil: ParsableCommand {
    static var configuration = CommandConfiguration(
        abstract: "A PDF manipulation Utility",
        version: "1.0",
        subcommands: [count.self, addPage.self, rotate.self, makePNG.self, join.self]
       // defaultSubcommand: rotate.self
    )

}

I get "@main' attribute cannot be applied to this declaration".

If I paste the Package.swift file from the 2nd project into the first, I get: "No such module "PackageDescription".

Wait: is this new project itself a Package, rather than a Command Line Tool project?

I'm worrying about the little orange box, instead of the 'app' style icon:

Screenshot

The @main attribute is applied to a type, not an import statement, like in your example. Thus, it should be on the line immediately preceding struct pdfutil instead of the top of the file.