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

add-target breaks a package that it's a single target package and doesn't have a subdirectory under Sources #8410

Closed
1 task done
kkebo opened this issue Mar 25, 2025 · 0 comments
Labels

Comments

@kkebo
Copy link
Contributor

kkebo commented Mar 25, 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

swift package add-target breaks a package that it's a single target package and doesn't have a subdirectory under Sources.

Expected behavior

The package can be built and its directory hierarchy becomes like this:

.
├── Package.swift
└── Sources
    ├── SamplePackage
    │   └── main.swift
    └── SecondTarget
        └── SecondTarget.swift

Actual behavior

error: 'samplepackage': Source files for target SamplePackage should be located under 'Sources/SamplePackage', or a custom sources path can be set with the 'path' property in Package.swift
.
├── Package.swift
└── Sources
    ├── main.swift
    └── SecondTarget
        └── SecondTarget.swift

Steps to reproduce

mkdir SamplePackage
cd SamplePackage
swift package init --type executable
swift package add-target SecondTarget
swift build

Swift Package Manager version/commit hash

6.0.3

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

Swift version 6.0.3 (swift-6.0.3-RELEASE)
Target: aarch64-unknown-linux-gnu
Linux Raspberry-beetle 6.6.74+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27) aarch64 GNU/Linux
@kkebo kkebo added the bug label Mar 25, 2025
plemarquand added a commit to plemarquand/swift-package-manager that referenced this issue Mar 25, 2025
Currently `swift package init --type executable` in a folder called
`Example` creates a project with the following structure:

```
./Example/Sources
./Example/Sources/main.swift
./Example/Package.swift
```

Following this up with a `swift package add-target Foo` produces a
package that no longer builds. It now has the structure:

```
./Example/Sources
./Example/Sources/main.swift
./Example/Sources/Foo/Foo.swift
./Example/Package.swift
```

Packages with multiple targets cannot have code directly in `./Sources`,
and the user gets the error:

`Source files for target Example should be located under 'Sources/Example'`

To work around this in the AddTarget command we can check if a package
is structured as a single target package with sources directly in
`./Sources` and move these files into a new folder located at
`./Sources/Example`. This allows the project to build after the new
target has been added.

Issue: swiftlang#8410
plemarquand added a commit that referenced this issue Mar 31, 2025
…8413)

### Motivation:

Currently running `swift package init --type executable` in a folder
called `Example` creates a project with the following structure:

```
./Example/Sources
./Example/Sources/main.swift
./Example/Package.swift
```

Following this up with a `swift package add-target Foo` produces a
package that no longer builds. It now has the structure:

```
./Example/Sources
./Example/Sources/main.swift
./Example/Sources/Foo/Foo.swift
./Example/Package.swift
```

Packages with multiple targets cannot have code directly in `./Sources`,
and the user gets the error:

`Source files for target Example should be located under
'Sources/Example'`

### Modifications:

To work around this in the AddTarget command we can check if a package
is structured as a single target package with sources directly in
`./Sources` and move these files into a new folder located at
`./Sources/Example`.

### Result:

This allows the project to build after the new target has been added.

Issue: #8410

---------

Co-authored-by: Max Desiatov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants