DocTest icon indicating copy to clipboard operation
DocTest copied to clipboard

error: use of unresolved identifier

Open schwa opened this issue 5 years ago • 4 comments

This seems to be what I was seeing with #19.

I have a very simple and freshly generated package:

Example/Package.swift
Example/Sources/Example/Example.swift

Example.swift contains:

/*
```swift doctest
answer() // 42
```
*/
public func answer() -> Int {
    return 42
}

I run swift doctest from the Example directory as so:

swift doctest --verbose --package Sources/Example/Example.swift

And get the following error back out:

schwa@pan ~/D/Example> swift doctest --verbose --package Sources/Example/Example.swift
2020-05-08T08:24:06-0700 trace: Starting swift-doctest
2020-05-08T08:24:06-0700 debug: Swift launch path: /usr/bin/swift
2020-05-08T08:24:06-0700 debug: Swift launch arguments: ["run", "--repl"]
2020-05-08T08:24:06-0700 trace: Scanning /Users/schwa/Desktop/Example/Sources/Example/Example.swift for DocTest blocks
2020-05-08T08:24:06-0700 info: Found DocTest block at Sources/Example/Example.swift#3:1
answer() // 42
2020-05-08T08:24:07-0700 trace: Finished running tests.
2020-05-08T08:24:07-0700 trace: Printing test report in TAP format.
TAP version 13
1..1
not ok 1 - `answer()` produced an error
  ---
  actual: 'error: repl.swift:2:1: error: use of unresolved identifier ''answer''
  
    answer()
  
    ^~~~~~'
  column: 1
  file: Sources/Example/Example.swift
  line: 4
  ...
  ⏎                        

schwa avatar May 08 '20 15:05 schwa

Function was originally internal. Changing it to public produces identically results though. Running Swift REPL directly and trying to import Example and run answer fails pretty much the same way:

schwa@pan ~/D/Example [1]> swift repl
error: module importing failed: invalid pathname
error: module importing failed: invalid pathname
Welcome to Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51).
Type :help for assistance.
  1> import Example
  2> answer()
error: repl.swift:2:1: error: use of unresolved identifier 'answer'
answer()
^~~~~~


  2> ^D

Running swift build before doesn't seem to help.

schwa avatar May 08 '20 15:05 schwa

the error: module importing failed: invalid pathname was due to missing python modules in my .lldbinit. I've cleaned those up but am still seeing the unresolved identifier issue...

schwa avatar May 08 '20 15:05 schwa

Thanks for the detailed write-up, @schwa. I was able to reproduce this locally.

The reason that answer is unresolved is that the code block doesn't import the Example module. I was able to get this to run successfully by adding an import statement:

/*
```swift doctest
import Example
answer() // 42
```
*/
public func answer() -> Int {
    return 42
}
$ swift doctest --verbose --package Sources/Example/Example.swift
2020-05-11T06:38:29-0700 trace: Starting swift-doctest
2020-05-11T06:38:29-0700 debug: Swift launch path: /usr/bin/swift
2020-05-11T06:38:29-0700 debug: Swift launch arguments: ["run", "--repl"]
2020-05-11T06:38:29-0700 trace: Scanning /Users/mattt/Downloads/Example/Sources/Example/Example.swift for DocTest blocks
2020-05-11T06:38:29-0700 info: Found DocTest block at Sources/Example/Example.swift#7:1
import Example
answer() // 42
2020-05-11T06:38:30-0700 trace: Finished running tests.
2020-05-11T06:38:30-0700 trace: Printing test report in TAP format.
TAP version 13
1..0⏎   

It's counter-intuitive that the execution context for code within the Example module requires an explicit import statement for itself — indeed, looking at the README, I see that I got that wrong myself in the add(_:_:) example (sorry about that!).

We can definitely improve the documentation here. Another option would be to implicitly add an import <#Module#> statement when running in a package, but this is hard to do right. Any thoughts about how this should all work?

mattt avatar May 11 '20 13:05 mattt

#30 fixes this issue, but goes too far in its solution by running the entire source file ahead of any expectations (when not running with --package option). A more correct solution would run only the code before the doctest block, and correctly handle non-source code contexts (such as Swift example code in a text document).

mattt avatar Nov 24 '20 14:11 mattt