4

I'm incorporating SwiftUI into my project for the very first time!

Unfortunately I can't preview my first SwiftUI file I add to the project. The build only fails on the preview. And the failure is in my tests.

Steps to build error:

  1. Click the "resume" button in the preview area for a newly created SwiftUI view.
  2. Build fails... I click the 'Diagnostics' button:

Diagnostics screen output:

missing required module 'JGProgressHUD'


SchemeBuildError: Failed to build the scheme "MyApp"

missing required module 'JGProgressHUD'

Compile AppTests.swift (x86_64): /Users/myname/Projects/MyApp/MyAppTests/Entities/AppTests.swift:11:8: error: missing required module 'JGProgressHUD' import MyApp ^

Yes, I have the JGProgressHUD lib imported via SPM.

This is what my test class looks like - it is failing on the import of "MyApp", but only for the preview of SwiftUI... there are no build failures for the app or tests otherwise...

import Foundation
import XCTest
import MyApp
// import app here because of: https://stackoverflow.com/questions/27172481/swift-singleton-init-called-twice-in-xctest

class AppTests: XCTestCase {
 // .. tests are here
}

Fails on the "Import MyApp" line.

Here's what I've tried:

  • searching everywhere online
  • messing with build schemes
  • made an example project with JGProgressHUD and tests, no issues with previewing
  • Verified that none of my swift files were added to the test target - none of them are, only xib files.
  • added "import JGProgressHUD" to the test file, then the preview build fails on this import line for JGProgressHUD.

I can't wait to use SwiftUI, but it seems pretty fickle so far adding to a new project, and I have no idea where else to look. Its such a random problem for what I'm trying to do, I am completely out of ideas for where to look.

Any ideas on what to check or look for?

Thanks for any help XCode sages!

TheJeff
  • 3,665
  • 34
  • 52
  • Try `@testable import MyApp` – Paulw11 Mar 11 '21 at 23:33
  • Thanks for the suggestion, but still has error -- "error: missing required module 'JGProgressHUD' @testable import MyApp ^" – TheJeff Mar 12 '21 at 00:41
  • The you will need to add that module to your test target as well – Paulw11 Mar 12 '21 at 00:42
  • Its a swift package (SPM) which I assume applies to all targets as they seem to be global to the workspace when your test target has the host application set to the main application. Is it possible to exclude/include swift packages a test target of this type? Under MyAppTests Target -> General, it shows "Host Application: MyApp" with "Allow testing host application APIs" checked. There are no other options under "General" there for the test target. – TheJeff Mar 12 '21 at 00:49
  • if I add "import JGProgressHUD" to the top of the test class, preview build now fails on that line, not the import MyApp line like before. – TheJeff Mar 12 '21 at 01:41
  • You will need to add that package to the test target. Just as you did to your app target – Paulw11 Mar 12 '21 at 01:57
  • Not the import. You need to add the JHProgressHub package to your test target and your app target. – Paulw11 Mar 12 '21 at 19:31
  • @Paulw11 - please read comments above. SPM packages are global to the workspace... You cannot pick and choose targets they apply to. If you can, then please tell me how. I'm gonig to delete my last comment because Stack Overflow is giving me grief about number of comments. Thanks for any thorough vetting of your next reply, and reading my responses in detail. – TheJeff Mar 12 '21 at 19:45
  • Maybe your test target isn't set up properly. Try deleting the target and re adding it. – Paulw11 Mar 12 '21 at 20:50
  • I have the same problem, but with a different SPM package using Xcode Version 12.5 (12E262). It seems that Xcode is not setting up the environment correctly. My unit tests all compile and pass, but fail to compile when Xcode is previewing SwiftUI. I'm in the same boat, guess I'll pass on SwiftUI. Without preview, swiftUI is useless. And there is absolutely no reason swiftUI previews should care at all about unit tests. – Neil May 02 '21 at 13:07
  • @Neil - by chance would you have your sources folder (where your codes files are stored) named the same as your app, as a subfolder in your project? Eg: (/AppName/AppName/*.swift) ?? From my research my next step was to try updating this to (/AppName/Sources/*.swift) - there may be xcode bugs around this folder naming. – TheJeff May 03 '21 at 13:48
  • @TheJeff - Yep, this is exactly my setup. Are you saying this change will fix it? It's been awhile since I monkeyed around with the folder structure and, years ago, Xcode behaved badly when I tried. – Neil May 04 '21 at 11:03
  • @Neil I'm not sure if it will fix it, but its a strong lead I have. Been meaning to update this question - I removed the JGProgressHUD lib, then went a few more layers down. Found a specific build error identical to another one I found online which was related to a same-named folder renamed fixing the issue. My Feedback ID to Apple is FB9090113 - if you log feedback please link to this. Apple Code Support Request was stumped! – TheJeff May 04 '21 at 13:12
  • @TheJeff Just filed a feedback. Did not reference your feedback id. Hopefully, we might get someone else to look at it. – Neil May 04 '21 at 14:36
  • 1
    My Feedback ID is FB9096575. – Neil May 04 '21 at 15:08
  • I'm now getting a new error after moving all of my sources to a differently named folder than my app I.E. "Sources" -- error: "Unable to find a target which creates the host product for value of $(TEST_HOST)". Googling this isn't much help. – TheJeff May 19 '21 at 02:04
  • I've got the same issue. This just started happening for some unknown reason. It's so frustrating. Your post is the only one that actually matches this problem that I could find in google. Why does the preview try building the tests? Have you had any success with this? BTW I think your testfile should say @testable import MyApp. – 23inhouse Jul 20 '21 at 10:49
  • Apple support says they've forwarded the issue to the team, but there has been no response or reply. It is very frustrating. Please file a feedback and link to the one listed above from Neil and mine is: FB9090113 Yours is the first where it started happening where it was previously working. Are you able to narrow down a git commit or config change that may be the culprit for when it broke? This could be a huge hint / help for this issue. – TheJeff Jul 20 '21 at 14:06

1 Answers1

1

Apple fixed this in XCode 13 Beta 5.

Please verify and post here if Apple's fix has resolved your problem.

TheJeff
  • 3,665
  • 34
  • 52