johnxnguyen / Down

Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.
Other
2.24k stars 320 forks source link

[Crash] Missing resource bundle when using SPM #243

Closed acoustep closed 3 years ago

acoustep commented 3 years ago

Please help prevent duplicate issues before submitting a new one:

Report

What did you do?

I'm trying to parse some markdown and render it to a DownView

What did you expect to happen?

I expected the HTML to render in the DownView.

What happened instead?

I'm getting this crash on the system:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file Down/DownView.swift, line 42
2021-01-30 11:12:09.122751+0000 DownTest[8704:153732] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file Down/DownView.swift, line 42

Background Info

I've previously had this working on an old Mac app using Carthage. I recently upgraded to Big Sur (Intel) and I'm trying to use the Swift Package Manager instead. I've also been upgrading my dependencies including Down and came across this error in my app and couldn't figure out how to fix it.

I tried recreating the issue in a fresh app and the same thing happened. So either I'm doing something very wrong or there seems to be a bug.

I've added a repo which replicates the issue here: https://github.com/acoustep/DownBugTest

Forgot to mention: I'm running Xcode 12.4

johnxnguyen commented 3 years ago

Hey @acoustep , thanks for flagging this issue. So it looks like the DownView.bundle (a resource that contains some css and javascript files) can't be loaded. Bundle resources were not supported in Swift packages until Swift 5.3, and unfortunately Down is still targeting 5.1.

I'll look into getting a fix for this, in the meantime you may need to continue to use Carthage to pull Down into your project.

acoustep commented 3 years ago

Hi @johnxnguyen , no problem. Thanks for responding so fast, I appreciate it!

johnxnguyen commented 3 years ago

Hey @acoustep , I have a draft PR that should solve this crash. I've tested it locally and it seems to work, but it'd be great if you could pull the branch fix/swift-package-structure to your project via SPM and check that you can use DownView.

acoustep commented 3 years ago

Hi @johnxnguyen , I've tried this and it compiles but I'm unable to see anything in the view.

I'm seeing this in my error logs:

2021-02-22 17:11:58.798066+0000 DownTest2[88558:2286407] [Process] 0x7f956b846020 - [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::processDidTerminate: (pid 0), reason 3
2021-02-22 17:11:58.798215+0000 DownTest2[88558:2286407] [Loading] 0x7f956b846020 - [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::dispatchProcessDidTerminate: reason = 3
2021-02-22 17:11:58.909875+0000 DownTest2[88558:2286407] [Process] 0x7f956b846020 - [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::processDidTerminate: (pid 0), reason 3
2021-02-22 17:11:58.928114+0000 DownTest2[88558:2286407] [Loading] 0x7f956b846020 - [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::dispatchProcessDidTerminate: reason = 3
2021-02-22 17:11:58.928157+0000 DownTest2[88558:2286407] [Process] 0x7f956b846020 - [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::tryReloadAfterProcessTermination: process crashed and the client did not handle it, not reloading the page because we reached the maximum number of attempts

Here's a screenshot

image

and the code

//
//  ViewController.swift
//  DownTest2
//
//  Created by Mitch Stanley on 22/02/2021.
//

import Cocoa
import Down

class ViewController: NSViewController {

    @IBOutlet weak var dview: NSView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // Do any additional setup after loading the view.
        renderDownInWebView()
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

}

private extension ViewController {

    func renderDownInWebView() {
        let readMeURL = Bundle.main.url(forResource: nil, withExtension: "md")!
        let readMeContents = try! String(contentsOf: readMeURL)

        do {
            let downView = try DownView(frame: dview.bounds, markdownString: readMeContents, didLoadSuccessfully: {
                print("Markdown was rendered.")
            })
            downView.autoresizingMask = [.width, .height]
            dview.addSubview(downView, positioned: .above, relativeTo: nil)
        } catch {
            NSApp.presentError(error)
        }
    }
}
johnxnguyen commented 3 years ago

I've tried this and it compiles but I'm unable to see anything in the view.

Hey @acoustep , make sure your entitlements file has a key com.apple.security.network.client with a boolean value 1, as seen here:

Screen Shot 2021-02-22 at 6 18 43 PM

I'm not sure why you need it (I don't use DownView) but I saw it in the demo project and it works for me. Let me know how it goes.

acoustep commented 3 years ago

Thanks @johnxnguyen that's done the trick. I think it must be because the example markdown file has links to external images.

johnxnguyen commented 3 years ago

Good to know! I'll do some some checks that o haven't broken anything when pulling in Down via Carthage or Pods, and if all is good I'll merge the PR and release the changes soon.