You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 16, 2021. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 16, 2021. It is now read-only.
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
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...
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 --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?
#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).
Activity
schwa commentedon 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:
Running
swift buildbefore doesn't seem to help.[-]Error - module importing failed: invalid pathname[/-][+]error: use of unresolved identifier[/+]schwa commentedon May 8, 2020
the
error: module importing failed: invalid pathnamewas due to missing python modules in my .lldbinit. I've cleaned those up but am still seeing the unresolved identifier issue...mattt commentedon May 11, 2020
Thanks for the detailed write-up, @schwa. I was able to reproduce this locally.
The reason that
answeris unresolved is that the code block doesn't import theExamplemodule. I was able to get this to run successfully by adding animportstatement:$ swift doctest --verbose --package Sources/Example/Example.swiftIt's counter-intuitive that the execution context for code within the
Examplemodule requires an explicit import statement for itself — indeed, looking at the README, I see that I got that wrong myself in theadd(_:_:)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 commentedon 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
--packageoption). 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).