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

swift package init --package-path <dir-does-not-exists> does not create the directory #8393

Open
1 task done
bkhouri opened this issue Mar 20, 2025 · 0 comments
Open
1 task done
Assignees
Labels
bug good first issue Good for newcomers Edit swift package `swift package` subcommand

Comments

@bkhouri
Copy link
Contributor

bkhouri commented Mar 20, 2025

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

When trying to create a new package, using the --package-path option fails if the directory does not exist.

Expected behavior

swift package init --package-path <dir> should:

  • create the directory, and populate the package if it does not exists
  • should populate the package if the directory exists and is empty
  • prompt the user for action if the directory exists and is non-empty
  • should error if <dir> exists as a file

Actual behavior

directory does not exists

❯ swift package init --package-path Foo
error: chdir error: No such file or directory (2): /Users/bkhouri/Downloads/Foo

Directory exists and is empty

❯ swift package init --package-path Foo
Creating library package: Foo
Creating Package.swift
Creating .gitignore
Creating Sources
Creating Sources/Foo/Foo.swift
Creating Tests/
Creating Tests/FooTests/
Creating Tests/FooTests/FooTests.swift

Directory Exists and contains a Package.swift

❯ swift package init --package-path Foo
Creating library package: Foo
error: a manifest file already exists in this directory

Directory exists and is not empty (does not contains Package.swift)

❯ swift package init --package-path Foo
<...SNIP...>
❯ rm -rf Foo/Package.swift
❯ swift package init --package-path Foo
Creating library package: Foo
Creating Package.swift

Steps to reproduce

swift package init --package-path ./does-not-exists-yadda-yadda

Swift Package Manager version/commit hash

Swift Package Manager - Swift 6.2.0-dev

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

❯ swift --version
Apple Swift version 6.2-dev (LLVM 162ee50b401fff2, Swift 57288d13c9f3c02)
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

@bkhouri bkhouri added bug good first issue Good for newcomers Edit swift package `swift package` subcommand labels Mar 20, 2025
@bkhouri bkhouri self-assigned this Mar 20, 2025
plemarquand added a commit to plemarquand/swift-package-manager that referenced this issue Mar 21, 2025
Running `swift package init --package-path <dir-does-not-exists>`
results in an error when SPM attempts to change directories in to the
directory that doesn't exist.

Add a new boolean to the `_SwiftCommand` protocol that lets commands opt
in to creating the directory at `--package-path` if it doesn't exist.
Opt in `InitCommand` to this behaviour.

Issue: swiftlang#8393
plemarquand added a commit that referenced this issue Mar 24, 2025
…8401)

Running `swift package init --package-path <dir-does-not-exists>`
results in an error when SwiftPM attempts to change directories in to
the directory that doesn't exist.

### Motivation:

It is rare that a user wants to do a `swift package init` in a folder
that already has content. A typical pattern is to `mkdir mypackage && cd
my package && swift package init`. SwiftPM already has a
`--package-path` flag indicating the package folder that the command
should operate on, but attempting to use this to create a folder that
doesn't exist during `swift package init` results in an error when
SwiftPM attempts to chdir to the --package-path that doesn't exist.

### Modifications:

Add a new boolean to the `_SwiftCommand` protocol that lets commands opt
in to creating the directory at `--package-path` if it doesn't exist.
Opt in `InitCommand` to this behaviour.

### Result:

`swift package init --package-path ./mypackage` successfully initializes
a package in `./mypackage`, even if `./mypackage` doesn't exist.

Issue: #8393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Good for newcomers Edit swift package `swift package` subcommand
Projects
None yet
Development

No branches or pull requests

1 participant