Skip to content

MistDemo: @testable import fails because MistDemo is an executableTarget #260

@leogdion

Description

@leogdion

Problem

All MistDemoTests files use @testable import MistDemo, but MistDemo is declared as an .executableTarget in Examples/MistDemo/Package.swift. Executable targets do not produce importable modules, so the test suite fails to compile with:

Unable to resolve module dependency: 'MistDemo'
@testable import MistDemo
                 ^

This has been broken since the tests were introduced in alpha.4 but was only surfaced when actually running swift test in the MistDemo example.

Root Cause

In Examples/MistDemo/Package.swift, MistDemo is declared as:

.executableTarget(
    name: "MistDemo",
    ...
)

Swift Package Manager does not expose executable targets as importable modules, so test targets cannot @testable import them.

Fix

Split MistDemo into:

  1. MistDemo library target — all current source files in Sources/MistDemo/ except the @main entry point
  2. A thin executable target (e.g. MistDemoEntry) — contains only MistDemo.swift (the @main struct), depends on the MistDemo library, and is wired to the mistdemo product

Then MistDemoTests can @testable import MistDemo from the library without any changes to the test files.

Affected Files

  • Examples/MistDemo/Package.swift — target restructuring
  • Examples/MistDemo/Sources/MistDemo/MistDemo.swift — move to new entry-point target directory

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions