I am able to get Snippet usage in my Docc files working fine as long as I don't import any third party libraries. In this case I am importing a simple DSL for writing HTML that I built for learning purposes.
The library works fine through the normal exectuableTarget without any issues however when I am importing it within the Snippets directory instead of Sources/TargetName it is returning the below errors:
error: emit-module command failed with exit code 1 (use -v to see invocation)
error: emit-module command failed with exit code 1 (use -v to see invocation)
/Users/maclong/Developer/portfolio/Snippets/Introduction to WebUI/Static.swift:1:8: error: no such module 'WebUI'
1 | import WebUI
| `- error: no such module 'WebUI'
2 |
3 | // snippet.LAYOUT
/Users/maclong/Developer/portfolio/Snippets/Introduction to WebUI/Styles.swift:2:8: error: no such module 'WebUI'
1 | // snippet.hide
2 | import WebUI
| `- error: no such module 'WebUI'
3 |
4 | // snippet.show
/Users/maclong/Developer/portfolio/Snippets/Introduction to WebUI/Styles.swift:2:8: error: no such module 'WebUI'
1 | // snippet.hide
2 | import WebUI
| `- error: no such module 'WebUI'
3 |
4 | // snippet.show
/Users/maclong/Developer/portfolio/Snippets/Introduction to WebUI/Static.swift:1:8: error: no such module 'WebUI'
1 | import WebUI
| `- error: no such module 'WebUI'
2 |
3 | // snippet.LAYOUT
[14/62] Compiling Static Static.swift
Below is my Package.swift
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Portfolio",
platforms: [.macOS(.v13)],
products: [
.executable(name: "Portfolio", targets: ["Portfolio"])
],
dependencies: [
.package(url: "https://github.com/maclong9/web-ui", from: "1.0.1"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
],
targets: [
.executableTarget(
name: "Portfolio",
dependencies: [
.product(name: "WebUI", package: "web-ui")
],
resources: [
.copy("Public")
]
)
]
)
I have tried creating a package.product.library pointed to a package.target that contains WebUI as a dependency and then importing WebUI inside of that target and replacing the WebUI import inside of Snippets with an import of the library and that did not fix the issue unfortunately.
Running or building the project in Xcode returns no errors whatsoever however running swift build from the command line returns the errors shown above. Unfortunately I need to be able to run it on a production server build pipeline so I do required the build step to work from the command line.
I get errors as well - I can use libraries that are products of the same package, but dependencies from other packages don't seem to work in the snippets.
MPB-16 ā Snippets swift build
warning: 'snippets': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
/Users/diggory/Code/Swift/Snippets/Sources/SimpleLibrary/SimpleLibrary.md
[1/1] Planning build
Building for debugging...
error: link command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture arm64:
"type metadata for PlayingCard.PlayingCard", referenced from:
_DeckOfCards_main in DeckOfCards.swift.o
lazy protocol witness table accessor for type PlayingCard.PlayingCard and conformance PlayingCard.PlayingCard : Swift.CustomStringConvertible in PlayingCard in DeckOfCards.swift.o
"protocol conformance descriptor for PlayingCard.PlayingCard : Swift.CustomStringConvertible in PlayingCard", referenced from:
lazy protocol witness table accessor for type PlayingCard.PlayingCard and conformance PlayingCard.PlayingCard : Swift.CustomStringConvertible in PlayingCard in DeckOfCards.swift.o
"static DeckOfPlayingCards.Deck.standard52CardDeck() -> DeckOfPlayingCards.Deck", referenced from:
_DeckOfCards_main in DeckOfCards.swift.o
"DeckOfPlayingCards.Deck.deal() -> PlayingCard.PlayingCard?", referenced from:
_DeckOfCards_main in DeckOfCards.swift.o
"DeckOfPlayingCards.Deck.shuffle() -> ()", referenced from:
_DeckOfCards_main in DeckOfCards.swift.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[10/13] Linking SnippetsExecutable
Snippets are especially useful when the code being prototyped depends on other modules, as Snippets have the ability to import modules from the current package.
Which implies that one should be able to import dependencies (if Iām reading that right). Or maybe it means you can only import modules defined in the current package.
Yeah I did read this article and the patterns in my code follow this guide fairly closely, in theory I should be able to import this library as it's defined in the Package.swift but I'm getting this error.
The code closely follows the swift on server articles repository so I can't see where my code could be differing here and not allowing the import of the library.
I've just done some testing and I seem to be having the same issue with the library not being found if I attempt to import PackageDescription inside a Snippet, it seems that only Foundation is able to be imported with the way I have it setup currently.
I think it must be something to do with the target being an executable instead of a library as that's the only difference in Package.swift I can see when compared to swift-on-server/articles