gpt4 book ai didi

Getting 'no such module' error when importing a Swift Package Manager dependency(在导入SWIFT程序包管理器依赖项时出现“无此模块”错误)

转载 作者:bug小助手 更新时间:2023-10-26 20:43:40 33 4
gpt4 key购买 nike



I'm running Xcode 11 Beta 4.
I'm using CocoaPods, and wanted to use one of my dependencies with Swift Package Manager as a static library instead of as a framework.
On a fresh project created with Xcode 11, the dependency can be imported successfully, but on my existing CocoaPods workspace, it does not.

我正在运行Xcode11Beta 4。我正在使用CocoaPods,并希望将我与SWIFT Package Manager的一个依赖项用作静态库,而不是作为框架。在使用Xcode11创建的新项目上,可以成功导入依赖项,但在我现有的CocoaPods工作区上,它不能。



I think it's likely related, but I'm also getting this link warning in Xcode:

我认为这很可能是相关的,但我在Xcode中也收到了这个链接警告:



directory not found for option '-L/Users/username/Library/Developer/Xcode/DerivedData/App-axanznliwntexmdfdskitsxlfypz/Build/Products/Release-iphoneos


I went to see if the directory exists after the warning is emitted, and it does.
I could not find any meaningful difference between the newly-created project and my old one, other than the existence of CocoaPods.

在发出警告后,我查看了该目录是否存在,它确实存在。除了CocoaPods的存在,我找不到新创建的项目和我的旧项目之间有任何有意义的区别。



Would appreciate any pointers.

如果您能指点迷津,我们将不胜感激。


更多回答

Seems to be resolved in Xcode 11.3

似乎在Xcode11.3中得到了解决

优秀答案推荐

After adding a library (FASwiftUI in my case) through Swift Package Manager I had to add it to

在通过Swift Package Manager添加了一个库(在我的例子中是FASwiftUI)之后,我不得不将它添加到


Project Settings -> My Target ->
General -> Frameworks, Libraries, and Embedded Content

项目设置->我的目标->常规->框架、库和嵌入内容


to be visible in the import statement.

在IMPORT语句中可见。


I did not add any scripts for it to work.

我没有添加任何脚本以使其正常工作。


enter image description here



Solution


let package = Package(
name: "PackageName",
dependencies: [
// YOU MUST ADD THE DEPENDENCY BOTH HERE [1] AND BELOW [2]
.package(url: "https://github.com/mattmaddux/FASwiftUI", from: "1.0.4")
],
targets: [
.target(
name: "PackageName",
/*[2]*/ dependencies: ["FASwiftUI"], // [2] <<<--------- Added here as well
]
)

Explanation


I'm developing a Swift package that must provide FontAwesome Icons to whoever imports it.

我正在开发一个SWIFT包,它必须向导入它的人提供FontAwous图标。


I was getting "No such module 'FASwiftUI'" in my SwiftUI preview canvas.

我在SwiftUI预览画布上看到“没有这样的模块‘FASwiftUI’”。


I solved it by adding "FASwiftUI" to BOTH the dependencies array of the package AS WELL AS to the dependencies array in the target itself.

我通过将“FASwiftUI”添加到包的依赖项数组和目标本身的依赖项数组来解决了这个问题。


Full Package.swift File


// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "PackageName",
platforms: [
.macOS(.v11),
.iOS(.v14)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "PackageName",
targets: ["PackageName"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/nalexn/ViewInspector", from: "0.8.1"),
.package(url: "https://github.com/mattmaddux/FASwiftUI", from: "1.0.4")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "PackageName",
dependencies: ["FASwiftUI"], // <<<--------- Added this here
resources: [
.process("Assets")
]
),
.testTarget(
name: "PackageNameTests",
dependencies: ["PackageName", "ViewInspector"])
]
)


Based on @AlexandreMorgado answer it seems like it is better to run this script in Build phases before Compile Sources. Then it works when archiving.

根据@AlexandreMorgado的回答,在编译源代码之前在构建阶段运行这个脚本似乎更好。然后它在存档时起作用。



enter image description here



if [ -d "${SYMROOT}/Release${EFFECTIVE_PLATFORM_NAME}/" ] && [ "${SYMROOT}/Release${EFFECTIVE_PLATFORM_NAME}/" != "${SYMROOT}/${CONFIGURATION}${EFFECTIVE_PLATFORM_NAME}/" ] 
then
cp -f -R "${SYMROOT}/Release${EFFECTIVE_PLATFORM_NAME}/" "${SYMROOT}/${CONFIGURATION}${EFFECTIVE_PLATFORM_NAME}/"
fi


It turned out that Swift Package Manager implicitly depends on the project's Configuration names. I had them at live/qa instead of Release/Debug, and changing them back resolved the issue. Very odd, but I hope it saves you some trouble dear reader.

事实证明,SWIFT包管理器隐式依赖于项目的配置名称。我把它们放在LIVE/QA而不是Release/Debug,把它们改回来解决了这个问题。很奇怪,但我希望它能为你省去一些麻烦,亲爱的读者。



After a whole week fighting this issue, I developed a workaround using schemes and pre-actions.

在与这个问题斗争了整整一周后,我开发了一个使用方案和预操作的变通方法。


I have a configuration called "Beta", so Xcode can't compile SPM dependencies. I realised Xcode compile SPM dependencies as Swift modules and add the files in Build/Products/Release-iphoneos folder in DeriverData.

我有一个名为“Beta”的配置,所以Xcode无法编译SPM依赖项。我意识到Xcode将SPM依赖项编译为Swift模块,并将文件添加到DeriverData的Build/Products/Release-iphoneos文件夹中。


So I created a scheme in Xcode and added this run script on build pre-actions:

因此,我在Xcode中创建了一个方案,并在构建前操作中添加了这个运行脚本:


enter image description here



cp -f -R "${SYMROOT}/Release${EFFECTIVE_PLATFORM_NAME}/" "${SYMROOT}/${CONFIGURATION}${EFFECTIVE_PLATFORM_NAME}/"

This script run before the build process, copying files and modules generated by Xcode on default Release-iphoneos folder to configuration folder, Beta-iphoneos, in my case.

该脚本在构建过程之前运行,将Xcode在默认版本-iphoneos文件夹中生成的文件和模块复制到配置文件夹Beta-iphoneos(在我的例子中)。


After coping the content from Release-iphoneos to your $configuration$-iphoneos folder Xcode should correctly compile, build and run your project.

将来自Release-iphoneos的内容复制到$Configuration$-iphoneos文件夹后,Xcode应该可以正确地编译、构建和运行您的项目。



I just ran into a similar problem and discovered that my schemes referenced old configurations, configurations that no longer existed. Once I updated them to the correct configurations the build succeeded.

我刚刚遇到了一个类似的问题,发现我的方案引用了旧的配置,不再存在的配置。一旦我将它们更新为正确的配置,构建就成功了。


(I'm leaving this comment more than a year after the original post. It's possible that what I ran into is completely different from what was originally reported. Still, it took me quite a while to track the problem down, so I wanted to leave a note that might save others time.)

(我在最初的帖子一年多后留下了这条评论。有可能我遇到的情况与最初报道的完全不同。尽管如此,我花了相当长的时间才找到这个问题,所以我想留下一张纸条,可能会节省其他人的时间。)



Clearing the derived data solved the issue in my case. I have Microsoft Azure Devops CI Pipeline, to clear the derived data I have to edit the Xcode build task and in the "Actions" field add this command: clean.

清除派生数据解决了我的问题。我有Microsoft Azure DevOps CI管道,要清除派生数据,我必须编辑Xcode构建任务,并在“Actions”字段中添加以下命令:Clean。



What worked for me: I removed my import WebMIDIKit line and added it again.

对我有效的方法:我删除了导入的WebMIDIKit行,然后重新添加它。



Based on @sliwinski.lukas's answer, in my case the ${CONFIGURATION} was outputting "Release", so it was just copying the Release folder itself which was no good. I simply hardcoded my configuration name, and flipped Release and MyConfiguration, and it worked. I put the following code right before "Compile Sources" in the "Build Phases" tab:

根据@slwinski.lukas的回答,在我的例子中,${configuration}输出的是“Release”,所以它只是复制Release文件夹本身,这是不好的。我只需硬编码我的配置名称,并翻转Release和MyConfiguration,它就起作用了。我将以下代码放在“构建阶段”选项卡中的“编译源代码”之前:


cp -f -R "${SYMROOT}/MyConfiguration${EFFECTIVE_PLATFORM_NAME}/" "${SYMROOT}/Release${EFFECTIVE_PLATFORM_NAME}/" || true

Also importantly, I had to add this in the project that used the SPM and not in the main app.

同样重要的是,我必须将它添加到使用SPM的项目中,而不是主应用程序中。



I just ran into a similar problem when running xcodebuild from the command line. I was passing CONFIGURATION_BUILD_DIR=build but found that it needs to be an absolute path: CONFIGURATION_BUILD_DIR=$(pwd)/build solved the problem.

我刚刚在从命令行运行xcodebuild时遇到了类似的问题。我正在传递CONFIGURATION_BUILD_BUILD =build,但发现它需要是一个绝对路径:CONFIGURATION_BUILD_BUILD =$(pwd)/build解决了这个问题。



Might I shed a bit more light on your plight...

我可以更多地解释一下你的困境吗?


I'm working on a fairly large iOS app (6680 files) whose result is composed of many frameworks and a mixed bag of podfiles, swift packages, legacy ObjC code (that still outnumbers newer Swift stuff).

我正在开发一个相当大的iOS应用程序(6680个文件),其结果是由许多框架和一个混合的podfiles包,swift包,遗留的ObjC代码(仍然超过新的Swift代码)组成。


Whenever we deal with swift packages, we need to wrap them in frameworks because it simplifies podfile & dependency resolutions when we have our remote (Jenkins) build system eat everything up to spew binaries for internal QA & ultimately, Enterprise & AppStore publishing.

每当我们处理SWIFT包时,我们都需要将它们包装在框架中,因为当我们让我们的远程(Jenkins)构建系统吞噬一切以喷出内部QA&最终,企业和AppStore发布时,它简化了podfile和依赖项解析。


Earlier today, I was dealing with one such swift package wrapped in a framework and all the issues listed above hit me square in the face.

今天早些时候,我正在处理一个包裹在框架中的如此快速的包,上面列出的所有问题都直击我的脸。


After stashing, pushing usable code and then reapplying my stashed framework wrapper to the swift package, I used a different route than opening our project's workspace where a bunch of projects and targets are collected.

在存储、推送可用代码,然后将隐藏的框架包装器重新应用到SWIFT包之后,我使用了一种与打开我们项目的工作区不同的方法,在那里收集了一堆项目和目标。


Opening the lone framework wrapper seems to have kicked XCode (13.3.1) into submission and at that point, the target settings "Frameworks, Libraries and Embeddable" section was actually able to display the swift package's "Foo" binary. Added it, and then everything was playing nice.

打开单独的框架包装器似乎已经将XCode(13.3.1)踢到了提交状态,在这一点上,目标设置“框架、库和可嵌入的”部分实际上能够显示SWIFT包的“foo”二进制文件。加上它,然后一切都变得很好。


So, if you're still having problems, try simplifying the problem by opening smaller morsles if you can. Or start making these wrapper frameworks (if it's at all possible) so that you can actually manage smaller bites before integrating them on XC's platter.

因此,如果你仍然有问题,试着简化问题,如果可以的话,打开更小的砂浆。或者开始制作这些包装器框架(如果可能的话),这样在将它们集成到XC的拼盘上之前,您就可以实际管理较小的代码片段。



In my case, I am right up against my storage limit, and this seems to have happened because there was no more room to download the package.

在我的情况下,我正好达到了我的存储限制,这似乎是因为没有更多的空间来下载包。


This is an edge case as most people will have more than enough storage, and it's good to know that it fails silently, until you go to build and it gives this error.

这是一种边缘情况,因为大多数人都有足够的存储空间,而且知道它会以静默方式失败是件好事,直到您开始构建时,它会给出这个错误。



For me, I go to Xcode -> File (The one on mac top bar) -> Packages -> Update to Latest Package Versions. This solved my problem.

对我来说,我会去Xcode->文件(Mac顶栏上的那个)->包->更新到最新的包版本。这解决了我的问题。



In order to keep incremental builds working I had to specify the output files of "Fix SPM" build phase like so: enter image description here

为了使增量构建正常工作,我必须指定“Fix SPM”构建阶段的输出文件,如下所示:


更多回答

Thank you. Really helped me to save some time 👍

谢谢。真的帮我节省了一些时间👍

I solved it by adding "FASwiftUI" to the dependencies array in my Swift Package's package file. See my answer below for more info.

我通过将“FASwiftUI”添加到我的SWIFT包的包文件中的依赖数组解决了这个问题。有关更多信息,请参阅下面的答案。

I have a project I started a few years ago with an older Xcode and I have only a section "Embedded content" which is empty and doesn't allow to add any existing packages. It allows to clone though. but when I clone it nothing new happens, the section remains empty and I cannot import the module. anything I miss? what could be the problem?

我有一个几年前用旧的Xcode启动的项目,我只有一个“Embedded Content”部分是空的,不允许添加任何现有的包。不过,它允许克隆。但当我克隆它时,没有发生任何新的事情,该部分保持为空,并且我无法导入该模块。有什么我错过的吗?这可能是什么问题?

Always funny when something really slows you down, you find the solution on SO but can't upvote it anymore because you already did. I'd love to give you a second upvote right now.

有趣的是,当某件事真的让你慢下来的时候,你找到了解决方案,但不能再给它加码了,因为你已经这么做了。我现在很乐意给你第二次赞成票。

Cheers, that was my issue when googling for this!

干杯,这是我的问题时,谷歌为这一点!

I can't add anything to the Package.swift file -- it seems to be read-only.

我无法向Package.swft文件添加任何内容--它似乎是只读的。

This means you're trying to edit the repo Xcode has checked out for you as a dependency, it's supposed to be read only. If you don't have access to the repo of the lib you're trying to change, you can fork it.

这意味着您正在尝试编辑已作为依赖项签出的repo Xcode,它应该是只读的。如果您无法访问您试图更改的库的repo,您可以派生它。

Took far too much Googling to find this. I'm sure it's documented somewhere, but I don't recall seeing it anywhere else. Thank you for posting.

我花了太多时间在谷歌上搜索才找到这个。我肯定它在某个地方有记录,但我不记得在其他地方见过它。感谢您的发帖。

You may have to add it as: .product(name: "FASwiftUI", package: "-whatever-the-package-is-called") In the .target dependencies and not just as "FASwiftUI"

您可能需要在.Target依赖项中将其添加为:.product(名称:“FASwiftUI”,Package:“What-the-Package-is-Call”),而不仅仅是“FASwiftUI”

I have updated the script to check if the Release folder exists before copy to avoid error messages during compilation (that might be the case when you build Debug build configuration)

我已经更新了脚本,以在复制之前检查Release文件夹是否存在,以避免在编译期间出现错误消息(在构建Debug Build配置时可能会出现这种情况)

Please note that this seems to mess up the packages' build configuration: Once they have been build for the Release configuration, that will be used even if you want to make a Debug build! (until you clean the project)

请注意,这似乎会扰乱包的构建配置:一旦为发布配置构建了包,即使您想要进行调试构建,也会使用它!(直到您清理项目)

Note the "before Compile Sources", otherwise it won't work.

注意“在编译源代码之前”,否则它不会工作。

Unfortunately, this solution does not seem to work on Xcode 11.3.1.

不幸的是,这个解决方案似乎不适用于Xcode11.3.1。

It says 'No such module "XXX"'.

上面写着‘没有这样的模块’XXX‘。

Oh dear, I hope apple addresses this when we're out of Xcode 11 beta.

哦,亲爱的,我希望苹果在我们发布Xcode 11 beta时解决这个问题。

I still see this with Xcode 11 (11A420a), the 'live' version. Can somebody confirm this behaviour?

我在Xcode11(11A420a)上仍然看到了这一点,Xcode11(11A420a)是‘live’版本。有人能证实这一行为吗?

This is known by apple Xcode issue. forums.swift.org/t/custom-build-configuration-names/29167

这就是苹果Xcode问题所知道的。Forums.swift.org/t/custom-build-configuration-names/29167

This is still an issue on Xcode 15 beta 1

在Xcode 15测试版1上,这仍然是一个问题

This is Xcode 14 issue as well

这也是Xcode14的问题

I create a new environment such as staging and have above issue. Have you any solution for this? bro

我创造了一个新的环境,比如登台,并有上面的问题。你对此有什么解决方案吗?哥们儿

@logan.Nguyen, have you tried build again? Sometimes this workaround fails on first time, because the compilation process can execute before the files be copied, but when you run again it works.

@logan.Nguyen,你又试过构建了吗?有时,此解决方法第一次会失败,因为编译过程可以在复制文件之前执行,但当您再次运行时,它会起作用。

This worked for me. Thanks! However when creating an archive it didn't. Also when adding the pre-action to the Archive phase. Do you maybe have an idea of how to solve this as well?

这对我很有效谢谢然而,当创建一个档案,它没有。在将预操作添加到存档阶段时也是如此。你也有办法解决这个问题吗?

@Menno, I didn't test archiving with this method. But sliwinski.lukas just posted an alternative to fix this.

@Menno,我没有用这种方法测试存档。但slwinski.lukas刚刚发布了一个替代方案来解决这个问题。

Keep in mind that this is confirmed to be an Xcode bug, as can be seen in a comment to my original answer. So I'd keep that in mind and keep an eye out for the fix in order to remove any workarounds.

请记住,这确实是一个Xcode错误,可以在对我最初回答的一条评论中看到。因此,我会记住这一点,并密切关注修复程序,以便删除任何变通方法。

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com