Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

error: use of unresolved identifier #25

Open
schwa opened this issue May 8, 2020 · 4 comments · Fixed by #30
Open

error: use of unresolved identifier #25

schwa opened this issue May 8, 2020 · 4 comments · Fixed by #30
Labels
bug Something isn't working

Comments

@schwa
Copy link

schwa commented May 8, 2020

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
Copy link
Author

schwa commented May 8, 2020

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 schwa changed the title Error - module importing failed: invalid pathname error: use of unresolved identifier May 8, 2020
@schwa
Copy link
Author

schwa commented May 8, 2020

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...

@mattt mattt added the bug Something isn't working label May 11, 2020
@mattt
Copy link
Contributor

mattt commented May 11, 2020

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
Copy link
Contributor

mattt commented Nov 24, 2020

#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).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants