comprehensive-rust icon indicating copy to clipboard operation
comprehensive-rust copied to clipboard

Code snippets not showing (Uncaught SyntaxError: Unexpected token '.' (at book.js:1:1))

Open antyj1994 opened this issue 2 years ago • 16 comments

image

Just started this course, error showing when running comprehensive rust in local environment.

Code snippets appear for a fraction of second and then disappear with this error in console.

antyj1994 avatar May 30 '23 15:05 antyj1994

Hi @antyj1994, thanks for reporting this!

I'm not able to reproduce it. When I run mdbook serve, and open http://localhost:3000/, I see the code snippets like normal. What do you see when you browse https://google.github.io/comprehensive-rust/?

How do you build the course locally, with mdbook build or mdbook serve?

Are you on Linux or Windows or...? I've only tested the book on Linux.

I just tried reinstalling mdbook version 0.4.30 with cargo install mdbook and it works the same.

mgeisler avatar May 30 '23 17:05 mgeisler

Hi! I installed latest version of rust and cargo (1.69.0) on Windows 10 and followed the steps described in the readme:

$ cargo install mdbook
$ cargo install mdbook-svgbob
$ cargo install mdbook-i18n-helpers

Then i cloned the repo in my local home and ran $ cargo install --path mdbook-exerciser inside it.

I ran mdbook test One test failed. Here are the logs:

2023-05-30 21:39:00 [ERROR] (mdbook::book): rustdoc returned an error:

--- stdout

running 1 test
test C:\Users\Antonio\AppData\Local\Temp\mdbook-S7BeIx\exercises/day-3/solutions-afternoon.md - Day_3_Afternoon_Exercises::Safe_FFI_Wrapper (line 7) ... FAILED

failures:

---- C:\Users\Antonio\AppData\Local\Temp\mdbook-S7BeIx\exercises/day-3/solutions-afternoon.md - Day_3_Afternoon_Exercises::Safe_FFI_Wrapper (line 7) stdout ----
error[E0433]: failed to resolve: could not find `unix` in `os`
  --> C:\Users\Antonio\AppData\Local\Temp\mdbook-S7BeIx\exercises/day-3/solutions-afternoon.md:66:14
   |
60 | use std::os::unix::ffi::OsStrExt;
   |              ^^^^ could not find `unix` in `os`

error[E0599]: no function or associated item named `from_bytes` found for struct `OsStr` in the current scope
   --> C:\Users\Antonio\AppData\Local\Temp\mdbook-S7BeIx\exercises/day-3/solutions-afternoon.md:107:29
    |
101 |         let os_str = OsStr::from_bytes(d_name.to_bytes());
    |                             ^^^^^^^^^^ function or associated item not found in `OsStr`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0433, E0599.
For more information about an error, try `rustc --explain E0433`.
Couldn't compile the test.

I ran the application with mdbook serve and attended the course intro normally on http://localhost:3000/ until the first code snippet should have appeared.

Then i opened the console and saw that every page shows this error but seems to not having effect except in making code snippets disappear as soon as the page loads completely.

antyj1994 avatar May 30 '23 19:05 antyj1994

Hi @antyj1994, thanks for the detailed explanation!

Then i opened the console and saw that every page shows this error but seems to not having effect except in making code snippets disappear as soon as the page loads completely.

The mdbook test error comes from compiling the code snippets — and it shows another problem: one of the code snippets don't work on Window. We could try to fix that, but it should be independent of the problem with showing the code: When you show the book using mdbook serve, the code snippets are not compiled. The code snippets could be completely broken and you should still be able to see the book just fine.

So I think we're back to the mysterious error from book.js. Let me try and find someone who uses Windows who can help test this for us on another machine.

mgeisler avatar May 31 '23 09:05 mgeisler

I tweeted about this — let's see if someone else can help us debug this.

mgeisler avatar May 31 '23 09:05 mgeisler

I had this happen on Windows, not my Linux machine. I took a look and http://localhost:3000/book.js contains ../third_party/mdbook/book.js, which made me think of symlink issues. Indeed themes/book.js is supposed to be a symlink.

Apparently with git config --global core.symlinks true symlinks are created, but only if it's possible to do so without UAC prompt (via enabling developer mode). I didn't notice a warning about the fallback, which is unfortunate. I just deleted my local repo/dir and re-cloned after fixing my settings. After that, using mdbook serve renders code snippets correctly.

mauvealerts avatar May 31 '23 22:05 mauvealerts

Thanks for debugging this!

I would be happy to include a line about this in the README to advice people building the course on Windows.

mgeisler avatar Jun 01 '23 09:06 mgeisler

@mauvealerts

and re-cloned after fixing my settings.

would you please give more details on what settings you have fixed and how you've done it? Thank you

I manually created a symlink to the file third_party/mdbook/book.js thus replacing the exiting theme/book.js but based on your answer I wonder whether you did something else, perhaps more convenient?

drhcc avatar Jun 02 '23 09:06 drhcc

The settings are:

  • Enable Git symlinks via git config --global core.symlinks true
  • Enable Developer Mode (on my Windows 11 machine, it's under Privacy & security > For developers)

I did some more testing and the symlinks can be fixed via git restore, git reset --hard, etc. (instead of deleting and re-cloning).

mauvealerts avatar Jun 02 '23 11:06 mauvealerts

Thanks for solving this issue. From my side enabling developer mode and symlinks via git config --global core.symlinks true then git restore and git reset --hard did not work. I had to delete and clone the repo again but now it's working fine.

antyj1994 avatar Jun 02 '23 14:06 antyj1994

If one of you could create a PR to update the README with a note about this, that would be great! Maybe just suggest git config --global core.symlinks true and include a link to this issue for more information?

djmitche avatar Jun 02 '23 14:06 djmitche

#760 added a brief note to the README.

mauvealerts avatar Jun 02 '23 17:06 mauvealerts

I had a similar issue on Windows 10 -- no code display at all in the snippets, just line numbers. I also downloaded the .zip file and did not do a git clone; I used 7-zip to unzip and extract the directory. For those who might not wish to put more write wear on their SSDs than is absolutely necessary, the symlink in Windows can be created using just a Command Prompt with Administrator privileges. After opening such a command prompt then cd C:\Users\<username>\comprehensive-rust-main\theme and rename the existing book.js file to some other name. Then use the following to create the symlink in Windows: mklink book.js ..\third_party\mdbook\book.js Note the use of the backslashes here compared to *nix usage.

After the command completes, a dir command will show that a SYMLINK has been created for book.js and the target of the symlink will appear to the right in brackets. For those in corporate environments where Developer mode is disabled by policy, this method has the advantage of not needing Developer mode enabled.

Everything appears to work now... what I don't know is whether this was the only broken symlink... there might be others!

fphartnc avatar Jun 05 '23 05:06 fphartnc

Thanks for solving this issue. From my side enabling developer mode and symlinks via git config --global core.symlinks true then git restore and git reset --hard did not work. I had to delete and clone the repo again but now it's working fine.

The problem is that, if you didn't have symlinks enabled before the clone, git clone writes a .git/config with symlinks = false which overrides the global setting. So, to "repair" a repo after cloning you need to git config core.symlinks true inside the repo then git restore some/path or git reset --hard

Everything appears to work now... what I don't know is whether this was the only broken symlink... there might be others!

At the time of this writing there are two symlinks in the repo:

  • src/android/interoperability/cpp/overview.svgthird_party/cxx/overview.svg
  • theme/book.jsthird_party/mdbook/book.js

The above list will become out of date over time, of course. You can enumerate the symlinks by running (in PowerShell) git ls-files --format='%(objectmode) %(path)' | Select-String '^120000 '

mauvealerts avatar Jun 05 '23 05:06 mauvealerts

@mauvalerts: Thanks for pointing out the other symlink. I was able to fix that using a process similar to my previous post. I do wish to emphasize here that I pulled the .zip file from the repository and did not do a git clone, and it appears that the .zip format might suffer from this problem of busted symlinks indefinitely. If so, the developers may wish to consider deprecating availability of the .zip format. For myself, I have no problem with going over to git clone (I have git-bash installed on Windows already), but others might be impacted.

fphartnc avatar Jun 05 '23 20:06 fphartnc

Using GitHub's "Download ZIP" is unlikely to work well on Windows. Although the symlinks are stored with correct metadata, the couple utilities I've tried either:

  • Don't handle symlinks at all
  • Don't translate the path (i.e. replace / with \), resulting in broken links

I don't know of a way to disable GitHub's "Download ZIP" option, but none of the material links to it either (AFAICT)... There's a link to a zip file with the exercises, but that doesn't contain symlinks.

mauvealerts avatar Jun 06 '23 07:06 mauvealerts

I don't know of a way to disable GitHub's "Download ZIP" option, but none of the material links to it either (AFAICT)...

Yeah, this is not something we can or should disable — it's outside our area so to speak 🙂. The note in the README about symlinks should help here. If GitHub has more documentation about this, then I would be happy to point to that too.

For now, I think we can close this issue since there is a documented workaround and since the problem is not specific to the course.

mgeisler avatar Jun 06 '23 09:06 mgeisler