Skip to content

swift package init does not handle --package-path argument if it's not the first command line argument #8418

Open
@bkhouri

Description

@bkhouri

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

  • Confirmed reproduction steps with SwiftPM CLI. The description text must include reproduction steps with either of command-line SwiftPM commands, swift build, swift test, swift package etc.

Description

Running swift package init --type <type> --package-path to-be-deleted/foo fails with error: Unknown option '--package-path'

Actually providing --package-path <path> after any init argument caused this error:

  • swift package init --type <type> --package-path swift-package/$RANDOM
  • swift package init --enable-xctest --package-path swift-package/$RANDOM
  • swift package init --enable-swift-testing --package-path swift-package/$RANDOM
  • swift package init --name thing --package-path swift-package/$RANDOM

To workaround this, we need to place the --package-path <path> before the --type <type>, and the command then work.

Looking at the swift package init --help, it does not contains the --package-path option.

❯ swift package init --help
OVERVIEW: Initialize a new package

USAGE: swift package init [--type <type>] [--enable-xctest] [--disable-xctest] [--enable-swift-testing] [--disable-swift-testing] [--name <name>]

OPTIONS:
  --type <type>           Package type: (default: library)
        library           - A package with a library.
        executable        - A package with an executable.
        tool              - A package with an executable that uses
                            Swift Argument Parser. Use this template if you
                            plan to have a rich set of command-line arguments.
        build-tool-plugin - A package that vends a build tool plugin.
        command-plugin    - A package that vends a command plugin.
        macro             - A package that vends a macro.
        empty             - An empty package with a Package.swift manifest.
  --enable-xctest/--disable-xctest
                          Enable support for XCTest
  --enable-swift-testing/--disable-swift-testing
                          Enable support for Swift Testing
  --name <name>           Provide custom package name
  --version               Show the version.
  -h, -help, --help       Show help information.

Expected behavior

the swift package init should either:

  • support --package-path <path> command line option, no matter the order it is provided
  • the --package-path <path> should be provided before init. e.g.: swift package --package-path <path> init

The current behaviour is confusing and not ideal.

Actual behavior

❯ swift package init --type executable --package-path to-be-deleted/lib
error: Unknown option '--package-path'
Usage: swift package init [--type <type>] [--enable-xctest] [--disable-xctest] [--enable-swift-testing] [--disable-swift-testing] [--name <name>]
  See 'package init -help' for more information.


❯ swift package init --package-path to-be-deleted/foo --type executable
Creating executable package: foo
Creating Package.swift
Creating .gitignore
Creating Sources
Creating Sources/main.swift

Steps to reproduce

Run the following, tested on zsh

set -x
dir="${HOME}/Downloads/can_be_deleted/swift-package"
mkdir -p ${dir}
for t in "library" "executable" "tool" "build-tool-plugin" "command-plugin" "macro" "empty"
do
    swift package init --type $t --package-path "${dir}/$t"
    swift package init  --package-path "${dir}/$t" --type $t
done
set +x

Swift Package Manager version/commit hash

Using swift main-snapshot-2025-03-25

Swift & OS version (output of swift --version ; uname -a)

❯ swift --version
Apple Swift version 6.2-dev (LLVM 62b5a0f34622c56, Swift ea225d5485133ed)
Target: arm64-apple-macosx15.0
Build config: +assertions

❯ uname -a
Darwin BKs-Work-M1Pro-MacBook-Pro-2.local 24.4.0 Darwin Kernel Version 24.4.0: Tue Mar 11 22:26:20 PDT 2025; root:xnu_development-11417.101.12~4/DEVELOPMENT_ARM64_T6000 arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions