Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read files on iOS #38

Open
Ce1este opened this issue Nov 13, 2023 · 11 comments
Open

Cannot read files on iOS #38

Ce1este opened this issue Nov 13, 2023 · 11 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@Ce1este
Copy link

Ce1este commented Nov 13, 2023

All features work fine on my computer(windows) and Android phone.
When it comes to the iPad, everything works fine when using only the features of Typst itself, but I get error when I use the files in the Obsidian library or import typst packages.

filed to load file (access denied)
    ╭─[/07 DataBase/Obsidian插件使用方法/85 Typst Renderer.md:33:9]
    │
 33 │   image("/Resource_Library/408/磁盘.svg", width: 70%),
    │         ────────────────────────────────────  
    │                                                
    │ 
    │ Help: cannot read file outside of project root
you can adjust the project root with the --root argument
────╯
filed to load file (access denied)
   ╭─[/04 学习笔记/考研/数学/三角函数.md:6:9]
   │
 6 │ #import "@preview/cetz:0.1.2"
   │         ─────────────────────  
   │                                 
   │ 
   │ Help: cannot read file outside of project root
you can adjust the project root with the --root argument
───╯

I've noticed that this problem seems to have been fixed in #9, is there something wrong with the way I'm using it on iPad?

@Ce1este
Copy link
Author

Ce1este commented Nov 13, 2023

By the way, on iPad this plugin didn't download the missing libraries like it did on the computer, so I manually imported all the library files to .obsidian\plugins\typst\packages\preview
Could this be the cause of the problem?

@fenjalien fenjalien added the bug Something isn't working label Nov 13, 2023
@fenjalien
Copy link
Owner

Unfortunately I don't have access to an apple device so I haven't been able to do any testing on iOS. Could you give me some more details?

All features work fine on my computer(windows) and Android phone.
When it comes to the iPad, everything works fine when using only the features of Typst itself, but I get error when I use the files in the Obsidian library or import typst packages.

Are you saying that with everything (and I mean EVERYTHING) the same except the platform is either iPad or Windows, it works on Windows but not iPad? If not what are the differences or what happens if everything is the same? Could show your iOS, Obsidian, plugin versions and any other plugins you have enabled?

With the errors I would guess its Typst refusing to access the file before actually attempting to read the file. Could you change the first file path to a file that doesn't exist but in the same folder? Something like "/Resource_Library/408/a.svg", it should hopefully give the same error. I'm not sure whats happening with the second error. Could you try moving the code to different places in the vault, diffierent folders or at the root?

By the way, on iPad this plugin didn't download the missing libraries like it did on the computer

How do you know this and what error does it give?

I manually imported all the library files to .obsidian\plugins\typst\packages\preview
Could this be the cause of the problem?

I doubt this is the root cause but make sure they're in the same structure as in the computer.

@Ce1este
Copy link
Author

Ce1este commented Nov 14, 2023

First, I don't think this error is caused by other plugins, because when I disable all other plugins and only keep the Typst Renderer, I get the same error message.

But if you're going to need it, here's my list of plugins on the ipad. (from community-plugins.json)

[
  "dataview",
  "typst",
  "music-code-blocks",
  "adamantine-pick",
  "obsidian-admonition",
  "obsidian-advanced-uri",
  "aosr",
  "avatar",
  "obsidian-banners",
  "blur",
  "blindfold-obsidian",
  "buttons",
  "code-styler",
  "customizable-sidebar",
  "dbfolder",
  "new-tab-default-page",
  "obsidian-dynamic-background",
  "obsidian-dynamic-toc",
  "obsidian-excalidraw-plugin",
  "extract-url",
  "exercises",
  "file-tree-alternative",
  "home-tab",
  "vextab",
  "various-complements",
  "obsidian-title-serial-number-plugin",
  "obsidian-timeline",
  "obsidian-tikzjax",
  "templater-obsidian",
  "obsidian-style-settings",
  "table-extended",
  "sheets",
  "obsidian-read-it-later",
  "obsidian-quiet-outline",
  "pseudocode-in-obs",
  "obsidian42-brat",
  "obsidian-memos",
  "obsidian-markmind",
  "obsidian-functionplot",
  "obsidian-columns",
  "multi-column-markdown",
  "obsidian-minimal-settings",
  "mathlinks",
  "media-extended",
  "math-booster",
  "link-favicon",
  "language-translator",
  "obsidian-image-layouts",
  "obsidian-image-gallery",
  "obsidian-icon-shortcodes"
]

With the errors I would guess its Typst refusing to access the file before actually attempting to read the file. Could you change the first file path to a file that doesn't exist but in the same folder? Something like "/Resource_Library/408/a.svg", it should hopefully give the same error. I'm not sure whats happening with the second error. Could you try moving the code to different places in the vault, diffierent folders or at the root?

For /Resource_Library/408/a.svg, It does give the same error.
And I move the code to different places in the vault, It still give the same error.
I think your guess is right, Typst doesn't seem to have access to any files.


I'm using "Windows 11 Version 22H2 22621.1992" and "iPadOS 17.2".
I use the git tool to synchronize the contents of files in the repository between the three platforms(Windows,Android,iPadOS), which ensures that the file structure is the same.
In order to keep the settings of the three platforms from interfering with each other, I set up separate config folders for each of them. For windows is .obsidian, for iPad is .obsidian.iPad, for Android is .obsidian.android.

By the way, on iPad this plugin didn't download the missing libraries like it did on the computer

On the computer, typst automatically detected the missing libraries and then downloaded them automatically, on the iPad I waited for a long time and nothing happened, so I copied all contents from \.obsidian\plugins\typst\packages to \.obsidian.android\plugins\typst\packages and \.obsidian.iPad\plugins\typst\packages.
This works on Android, so I think it's probably not due to a different file structure either.

I'm sorry that I don't know much about how the Obsidian plugin actually works, so I can only judge it superficially: it seems to me that the way access permissions to files are determined on iOS/iPadOS is not quite the same as on Windows/Android, which may be the cause of the problem.

Finally, thank you for your work on this plugin, it has helped me a lot.
If you need any other information, I will try my best to provide it.

@fenjalien
Copy link
Owner

Thanks for the info! I've had a look at the code as an access denied error can't be thrown on mobile on a file read attempt, only before within the Typst compiler. Its a bug the wrong error is thrown however the actual error still gets printed to the console.

Would you be able to get access to the console on your iPad and show the error? I'm not sure how exactly to do this for apple devices as I only have android, but this: https://github.com/KjellConnelly/obsidian-dev-tools might be something. Could you also try in a new vault only on your iPad with only the plugin?

@Ce1este
Copy link
Author

Ce1este commented Nov 14, 2023

I'm not sure I'm using it the right way. Is this it?

Clipboard 2023年11月14日 20 11

blob:capacitor://localhost/4c89bc45-daf2-46bd-a015-ac7b8737e182:1:2529

@Ce1este
Copy link
Author

Ce1este commented Nov 14, 2023

图像

When the packages cannot be found, an error "package not found" will be reported.
Manually importing the package files to the corresponding location will result in an error "failed to load file(access denied)".

It seems that Typst can detect whether the package exists, but it cannot proceed to the next step correctly?

From opening Obsidian and loading the plugin to opening the file containing Typst only these two types of errors occur.

@fenjalien
Copy link
Owner

Thank you! I'm not sure I can diagnose this issue without an apple device with me as there should be more error messages than this...

I have a bad feeling its because the way in which files are read is only available on android and not iOS. Which is a great shame as there was literally no other way to do it.

It seems that Typst can detect whether the package exists, but it cannot proceed to the next step correctly?

Yes, the compiler works on a separate thread where the file reading is different (and not working) but the main thread can read the file system fine and tell the compiler which packages are available. If the package isn't available the main thread attempts to download the package then restart the compiler. I'm assuming you do have an internet connection in which case its receiving a 404 error meaning the package doesn't exist (which it definitely does). Any other issues in fetching the package should result in a different error.

@fenjalien fenjalien added the help wanted Extra attention is needed label Nov 14, 2023
@fenjalien fenjalien changed the title filed to load file on iPad Cannot read files on iOS Jun 19, 2024
@iamrecursion
Copy link

Could you not get around this by allowing typst packages and the WASM bundle to be stored in a user-defined folder in the vault (#52)? Once that is done, they should be able to be read by the vault adapter just like any other file.

@d-kaue
Copy link
Contributor

d-kaue commented Jun 20, 2024

Could you not get around this by allowing typst packages and the WASM bundle to be stored in a user-defined folder in the vault (#52)? Once that is done, they should be able to be read by the vault adapter just like any other file.

You're right. But I think the wasm might still be in the plugin folder, because the plugin already checks if it exists.

An addition, I've noticed that it's not an iOS-only problem. The mobile device does not recognize the nodejs path library and we should avoid using this library in favor of the obsidian API.

Hey @fenjalien, I'm already working on implementing the change to the PackagePath. Should I make a separate PR or a single one including this one?

@fenjalien
Copy link
Owner

fenjalien commented Jun 20, 2024

Could you not get around this by allowing typst packages and the WASM bundle to be stored in a user-defined folder in the vault (#52)? Once that is done, they should be able to be read by the vault adapter just like any other file.

Typst packages and the WASM bundle are being stored in the vault, its under .obsidian/plugins/typst/ but thats not the problem here.

I'm sure I've written this before but I'll reiterate, the biggest hurdle when reading files is that the Typst compiler calls the "read this file please" function synchronously but Obsidian mobile only provides asynchronous ways to read files. Thats all the problem is and its annoyingly difficult to solve in this case.

I'll now highlight the possible solutions I've tried and their problems:

  • On desktop this isn't a problem as we have access to Node modules which includes a synchronous file read. But we can't use Node modules on mobile.
  • We could just pre-read all the files in the vault. This is a terrible idea on large vaults with many many many files.
  • We could run the compiler until it needs a file, say "no" to stop the compilation, read the file then restart the same compilation. While this would work, I feel it is also a terrible idea on large and complicated compilations or when reading a large number of files as you're redoing a lot of compiling work.
  • If you put the Typst compiler in a Web Worker, so it can run parallel to the main thread. The Web Worker can use postMessage to send a file read request, wait using an Atomic and read data using a SharedArrayBuffer. This is what currently happens on desktop as SharedArrayBuffers are enabled, but they are not available on mobile with no way to enable them (The andriod webview doesn't even have them implemented, they should exist on iOS safari but its not clear how obsidian can enable them). This solution does have the side effect of the DOM updating before the Typst compiler finishes which causes the page to jitter and shift in some cases as it does not get blocked (Obsidian's built in Latex renderer runs synchronously which blocks the DOM from updating until all the Latex has been rendered, trust me its not fun in a large page). There is also apparently a bug with this when reading pngs [Bug]: Not support PNG file parse #56 and wasm files Error loading wasm file in Typst package #45 but I haven't had time to figure out why.
  • Keeping the Typst compiler in a Web Worker, you can setup a service worker that intercepts any http requests that the Web Worker makes synchronously using an XMLHttpRequest, communicate with the main thread asynchronously with events to get the file data, then respond to the request. I'm pretty sure this is how the Typst webapp works when reading files. Unfortunately serivce workers are disabled on all platforms for some reason. Apart from that this is my "idealist" soultion.
  • The current implementation for mobile, is I managed to find an http endpoint that emulates the file system to an extent. So the Web Worker uses an XMLHttpReqeust to fetch the files. I should note that this endpoint is not at all documented, or even noted and I stumbled across it by sheer luck. It clearly only works on Android and on iOS it either doesn't exist or theres a different file system.

I've already put a lot of time into solving this problem with very little to show for it, so I'm unwilling to try harder with all these ideas.

@d-kaue

An addition, I've noticed that it's not an iOS-only problem. The mobile device does not recognize the nodejs path library and we should avoid using this library in favor of the obsidian API.

You might have noticed by now but the mobile version should not be using the path library as its a Node module and is not available on mobile, there are checks in place to make sure it doesn't even try to use it.

I'm already working on implementing the change to the PackagePath. Should I make a separate PR or a single one including this one?

Make it separate, but I don't think it'll fix anything.

@d-kaue
Copy link
Contributor

d-kaue commented Jun 20, 2024

You might have noticed by now but the mobile version should not be using the path library as its a Node module and is not available on mobile, there are checks in place to make sure it doesn't even try to use it.

Yea, the this.app.emulate(true) now warns about that.

#Edit
never mind, I can't reproduce it again.
maybe it's the feature I'm implementing and I just ran into this error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants