Swift“错误:在自动导入中:无法从AST上下文获取模块'foo':”

时间:2023-01-23 22:01:22

With the following setup (gist):

通过以下设置(要点):

Package.swift:

Package.swift:

import PackageDescription

let package = Package(
    name: "foo",
    dependencies: [
        .Package(url: "https://github.com/rxwei/LLVM_C", majorVersion: 1, minor: 0)
    ]
)

Makefile:

Makefile文件:

all:
    @swift build \
        -Xcc -I`llvm-config --includedir` \
        -Xlinker -L`llvm-config --libdir` \
        -Xlinker -rpath -Xlinker `llvm-config --libdir`

main.swift:

main.swift:

import LLVM_C.Core

func foo(_ a: Int) {
    let b = a * a
    print(b)
}

foo(4)

After compiling an executable with make and starting the executable in a debugger, I cannot print the values of any variables:

在使用make编译可执行文件并在调试器中启动可执行文件后,我无法打印任何变量的值:

(lldb) b foo
Breakpoint 1: where = foo`foo.foo (Swift.Int) -> () + 12 at main.swift:4, address = 0x00000001000014dc
(lldb) r
Process 14376 launched: '/Users/emlai/Code/foo/.build/debug/foo' (x86_64)
Process 14376 stopped
* thread #1: tid = 0x226d5, 0x00000001000014dc foo`foo(a=4) -> () + 12 at main.swift:4, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x00000001000014dc foo`foo(a=4) -> () + 12 at main.swift:4
   1    import LLVM_C.Core
   2    
   3    func foo(_ a: Int) {
-> 4        let b = a * a
   5        print(b)
   6    }
   7    
(lldb) p a
error: in auto-import:
failed to get module 'foo' from AST context:

(lldb) p a
Shared Swift state for foo has developed fatal errors and is being discarded.
REPL definitions and persistent names/types will be lost.
warning: Swift error in module foo.
Debug info from this module will be unavailable in the debugger.

error: in auto-import:
failed to get module 'foo' from AST context

If I comment out the import LLVM_C.Core line, everything works properly.

如果我注释掉导入LLVM_C.Core行,一切正常。

This is preventing me from debugging my project and making forward progress. How can I fix this?

这使我无法调试我的项目并取得进展。我怎样才能解决这个问题?

1 个解决方案

#1


2  

Searching the web for this problem results only to "its a lldb bug". The only way I've found to debug is to debug tests. But AFAIK you can't run tests with having own main.swift script. This results into multiple definition of 'main'.

在网上搜索此问题只会导致“它的lldb bug”。我发现调试的唯一方法是调试测试。但是AFAIK你不能使用自己的main.swift脚本运行测试。这导致'main'的多重定义。

So just follow instructions here https://swift.org/getting-started/#using-the-package-manager to create needed file hierarchy for package with tests (with swift package init or manually), write some tests, run swift test and, finally, lldb .build/debug/fooPackageTests.xctest (<binary name>PackageTests.xctest is binary file for running tests). I suppose it is compiled differently in some point rather than common binary. At least that worked in my case :)

所以只需按照这里的说明https://swift.org/getting-started/#using-the-package-manager为包测试创建所需的文件层次结构(使用swift包init或手动),编写一些测试,运行swift测试最后,lldb .build / debug / fooPackageTests.xctest( <二进制名称> PackageTests.xctest是运行测试的二进制文件)。我想它在某些方面编译的方式不同,而不是普通的二进制文件。至少那个在我的情况下工作:)

Good luck with debugging! :)

祝你好运调试! :)

#1


2  

Searching the web for this problem results only to "its a lldb bug". The only way I've found to debug is to debug tests. But AFAIK you can't run tests with having own main.swift script. This results into multiple definition of 'main'.

在网上搜索此问题只会导致“它的lldb bug”。我发现调试的唯一方法是调试测试。但是AFAIK你不能使用自己的main.swift脚本运行测试。这导致'main'的多重定义。

So just follow instructions here https://swift.org/getting-started/#using-the-package-manager to create needed file hierarchy for package with tests (with swift package init or manually), write some tests, run swift test and, finally, lldb .build/debug/fooPackageTests.xctest (<binary name>PackageTests.xctest is binary file for running tests). I suppose it is compiled differently in some point rather than common binary. At least that worked in my case :)

所以只需按照这里的说明https://swift.org/getting-started/#using-the-package-manager为包测试创建所需的文件层次结构(使用swift包init或手动),编写一些测试,运行swift测试最后,lldb .build / debug / fooPackageTests.xctest( <二进制名称> PackageTests.xctest是运行测试的二进制文件)。我想它在某些方面编译的方式不同,而不是普通的二进制文件。至少那个在我的情况下工作:)

Good luck with debugging! :)

祝你好运调试! :)