jpsim / jpsim/SourceKitten

Cannot read .swift from SwiftDocs

Open
#465 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Swift
Stars
2.4k
Forks
237
PR merge metrics
No merged PRs in 30d

Description

I am trying to extract document JSON structure from a swift file using SwiftDocs class. I have imported Sourcekitten framework and below is the code snippet of the same.

  import Cocoa
  import SourceKittenFramework
  
  class ViewController: NSViewController {
    var sourcekitten: SwiftDocs?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        sourcekitten = SwiftDocs(file: File(path: "/Users/.../sample.swift")!, arguments: [])!
        print(sourcekitten!.docsDictionary) // Should print document json structure
    }
 }

Expecting it to print a JSON string similar to below one

{
  "key.substructure" : [
    {
      "key.kind" : "source.lang.swift.decl.struct",
      "key.offset" : 0,
      "key.nameoffset" : 7,
      "key.namelength" : 1,
      "key.bodyoffset" : 10,
      "key.bodylength" : 13,
      "key.length" : 24,
      "key.substructure" : [
        {
          "key.kind" : "source.lang.swift.decl.function.method.instance",
          "key.offset" : 11,
          "key.nameoffset" : 16,
          "key.namelength" : 3,
          "key.bodyoffset" : 21,
          "key.bodylength" : 0,
          "key.length" : 11,
          "key.substructure" : [

          ],
          "key.name" : "b()"
        }
      ],
      "key.name" : "A"
    }
  ],
  "key.offset" : 0,
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.length" : 24
}

Instead, I am getting cannot read file error.

When I did a 'po' this is what I got

Could not read contents of `/Users/.../SwiftyP/SwiftyP/SwiftyP/sample.swift`
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2017-12-28 15:27:27.960442-0600 SwiftyP[70384:15566179] Fatal error: Unexpectedly found nil while unwrapping an Optional value
(lldb) po File(path: "/Users/.../SwiftyP/SwiftyP/SwiftyP/sample.swift")!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ViewController example and the sample.swift path shown in the report, then inspect the File(path:) result before SwiftDocs is initialized. Confirm that the file can be read and that docsDictionary produces the expected document structure without force-unwrapping nil.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.