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

error: use of unresolved identifier #25

Open

Description

@schwa

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

Activity

  1. schwa commented on May 8, 2020

    @schwa
    Author

    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.

  2. changed the title [-]Error - module importing failed: invalid pathname[/-] [+]error: use of unresolved identifier[/+] on May 8, 2020
  3. schwa commented on May 8, 2020

    @schwa
    Author

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

  4. mattt commented on May 11, 2020

    @mattt
    Contributor

    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?

  5. reopened this on Nov 24, 2020
  6. mattt commented on Nov 24, 2020

    @mattt
    Contributor

    #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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions