Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow creation of non-existent directory during swift package init #8401

Merged
merged 3 commits into from
Mar 24, 2025

Conversation

plemarquand
Copy link
Contributor

@plemarquand plemarquand commented Mar 21, 2025

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

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 plemarquand changed the title Allow creation of non-existant directory during swift package init Allow creation of non-existent directory during swift package init Mar 21, 2025
Copy link
Contributor

@bkhouri bkhouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, though I would not close #8393 as one of the expected behaviours is not currently implemented.

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

@plemarquand
Copy link
Contributor Author

@swift-ci test

Copy link
Member

@cmcgee1024 cmcgee1024 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really welcome addition to the init package workflow.

@plemarquand
Copy link
Contributor Author

@swift-ci test

@plemarquand
Copy link
Contributor Author

@swift-ci test linux

@plemarquand plemarquand merged commit 6f40b31 into swiftlang:main Mar 24, 2025
5 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants