5

I create a blank template package:

> swift package init --name Temp
> open Package.swift

Xcode Version 13.2.1 (13C100) opens the package.

I add a dependency to the package.

dependencies: [
    .package(url: "https://github.com/johnsundell/publish.git", from: "0.7.0")
],

Xcode > Product > Build succeeds at this point.

I edit Temp/Sources/Temp/Temp.swift to insert the first line the package that is defined in dependencies.

import Publish

A build now generates the following error:…/Temp/Sources/Temp/Temp.swift:1:8: error: no such module 'Publish'.

I feel certain this is an Apple bug. Or I could be missing something.

There are several posts about this problem when there is an xcodeproj and the additional structure that provides. Some of them hint at workarounds that help some people.

Has anyone seen this and/or know of how to resolve it?

Apple's Creating a Standalone Swift Package with Xcode document doesn't provide any insight.

bshirley
  • 8,217
  • 1
  • 37
  • 43
  • Try adding the package in Xcode: File -> Add Package... – koen Dec 29 '21 at 20:37
  • @koen, that requires an "Add to Project" selection which doesn't exist if you have only a package and no Xcode project. – bshirley Dec 29 '21 at 21:19
  • Did you regenerate the xcodeproj afterwards by command line? Meaning: read again the Package.swift, add/remove new dependencies inside it? – Larme Dec 30 '21 at 11:55
  • @Larme i'm not sure what you mean, as mentioned in the document linked above _"Swift packages don’t use `.xcproject` or `.xcworkspace` …"_ – bshirley Dec 30 '21 at 15:06
  • What I mean, is that it's like a Podfile or Carthage. You might modify the Podfile, carthage, Package.swift, but there is no "auto update". So when you open the generated xcodeproj, it doesn't necessary have the last addon. You do `Xcode > Product > Build succeeds at this point.`, so you opened it with Xcode, no? – Larme Dec 30 '21 at 17:18
  • @bshirley - I added a new Swift file to the project, directly under Sources and got no error message for `import Publish`. Inside Sources -> Temp the error pops up. – koen Dec 30 '21 at 18:10
  • @Larme there's definitely some refresh issues going on, killing Xcode and restarting helps. – bshirley Dec 30 '21 at 22:47

1 Answers1

6

thanks for the chatter in the comments, @Larme & @koen, it helped

The issue was user error (and/or a documentation lapse). Living on the (bleeding) edge.

Sometimes updates from changes are slow or require a clean or a relaunch.

Xcode auto-generates Schemes from the targets defined in your package. My build was targeting MyTarget.

Two things were missing:

  1. name: "Publish" was not included in the package dependency - it's needed so you can reference it below (or maybe this can be derived, it's hard to tell because of Xcode refresh issues), and
  2. a reference is needed in the dependencies for each target using the package-dependency, i needed to add dependencies: ["Publish"] in the related target
    dependencies: [
        .package(name: "Publish", url: "https://github.com/johnsundell/publish.git", from: "0.7.0")
    ],
    …
    targets: [
        .target(
            name: "MyTarget",
            dependencies: ["Publish"]),
    ]
bshirley
  • 8,217
  • 1
  • 37
  • 43
  • I am seeing the same error message even with the dependencies, but it's frustratingly intermittent. What's especially weird is that we have five similar packages and four work fine. – EricS Feb 21 '22 at 22:11
  • 1
    clean build, killing app, logging out, rebooting - i tried all these things in trying to resolve my issue, and i wouldn't be surprised if a different subtle problem is causing your issue @EricS - best of luck – bshirley Feb 23 '22 at 19:22