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

Should be able to stripe prefix panic prevents building site with pages on Windows file server #1435

Open
kinxiel opened this issue Apr 23, 2021 · 8 comments
Labels
bug help wanted Need Windows help We need someone using Windows to help us with this issue

Comments

@kinxiel
Copy link

kinxiel commented Apr 23, 2021

Bug Report

Environment

Zola version: 0.13.0

Expected Behavior

Be able to build the site with pages within a directory in a file server. Built zola from src and used zola build on my machine to build the site, works like a charm!

Current Behavior

Copied the exact same site that worked locally, together with zola.exe to a shared folder located in a shared file server. The specific use case it to allow non-techy colleagues to add markdown files to the shared directory and build from there so that we only have a single version. Upon building from within the directory containing the files, I get the following error:

$ ./zola build
Building site...
`highlight_code` has been moved to a [markdown] section. Top level `highlight_code` and 
`highlight_theme` will stop working in 0.14. 
thread 'main' panicked at 'Should be able to stripe prefix: StripPrefixError(())',
components\library\src\content\page.rs:299:22 note: run with `RUST_BACKTRACE=1` 
environment variable to display a backtrace

Next I tried to check whether I could initiate a new project with zola init and that worked. Next I tried to build the empty project with zola build and that also worked. However, when I tried to add a page (index.md) to the project, I get the error shown above.

I tried removing any pages from the original project and zola build worked. So it appears there seems to be a problem when processing pages. It works fine if the site only contains sections.

Tried to follow the error and checked page.rs:299:22, which points to the following code block. But I am not sure what is causing the problem.

 /// Creates a vectors of asset URLs.
    fn serialize_assets(&self, base_path: &PathBuf) -> Vec<String> {
        self.assets
            .iter()
            .filter_map(|asset| asset.file_name())
            .filter_map(|filename| filename.to_str())
            .map(|filename| {
                let mut path = self.file.path.clone();
                // Popping the index.md from the path since file.parent would be one level too high
                // for our need here
                path.pop();
                path.push(filename);
                path = path
                    .strip_prefix(&base_path.join("content"))
                    .expect("Should be able to stripe prefix")
                    .to_path_buf();
                path
            })
            .map(|path| path.to_string_lossy().to_string())
            .collect()
    }

Step to reproduce

  1. build zola from src (Windows)
  2. build a local project with zola build
  3. check that it works
  4. copy exactly the same project to a directory on a local shared file server
  5. cd into that directory
  6. build project with zola build (zola serve also produced the same issue)

Don't know if anyone has encountered the same issue before. Thanks

@Keats
Copy link
Collaborator

Keats commented Apr 24, 2021

You can debug by adding a line I believe. From:

                path = path
                    .strip_prefix(&base_path.join("content"))
                    .expect("Should be able to stripe prefix")
                    .to_path_buf();

to

let new_path = println!("{:?} joined with {:?} = {:?}", path, base_path.join("content"), path.strip_prefix(&base_path.join("content")));
                path = path
                    .strip_prefix(&base_path.join("content"))
                    .expect("Should be able to stripe prefix")
                    .to_path_buf();

so we can see the actual error.

@kinxiel
Copy link
Author

kinxiel commented Apr 26, 2021

Thanks I tried adding the logging line and get the following results.

Current project with colocated pdfs

Log for working local build

Building site...
`highlight_code` has been moved to a [markdown] section. Top level `highlight_code` and `highlight_theme` will stop working in 0.14.
"C:/Users/Admin/Desktop/zola/content/my_content\\blog\\colocated_file.pdf" joined with "C:\\Users\\Admin\\Desktop\\zola\\content" = Ok("my_content\\blog\\colocated_file.pdf")

Log for non-working build on file server

Building site...
`highlight_code` has been moved to a [markdown] section. Top level `highlight_code` and `highlight_theme` will stop working in 0.14.
"//server/maindirectory/subdirectory/subdirectorytwo/myfolder/zola/content/my_content\\blog\\colocated_file.pdf" joined with "\\\\server\\maindirectory\\subdirectory\\subdirectorytwo\\myfolder\\zola\\content" = Err(StripPrefixError(()))
thread 'main' panicked at 'Should be able to stripe prefix: StripPrefixError(())', components\library\src\content\page.rs:300:22note: run with `

I am not sure why, but maybe the \\ instead of C: in base_path might have something to do with it, which probably prevents strip_prefix from working (sorry I am lacking knowledge on this matter)? Do you have any ideas on a what maybe causing this and and how to fix it?

Sample using the starter template

I also tried doing the same with the starter project with only a single page (first.md based on the getting started zola example)

Log for working local build

Building site...
-> Creating 1 pages (0 orphan), 1 sections, and processing 0 images
Done in 105ms.

Log for non-working build on file server

Building site...
-> Creating 1 pages (0 orphan), 1 sections, and processing 0 images
Failed to build the site
Error: Failed to render page '//server/maindirectory/subdirectory/subdirectorytwo/myfolder/test/zola_starter/content/blog\first.md'
Reason: Tried to render `blog-page.html` but the template wasn't found

Note: If i remove all colocated assets from my project, I get more or less the same error as above.

I am not sure if they are related or a completely different issues. Thank you

@kinxiel
Copy link
Author

kinxiel commented Apr 28, 2021

Tried something out to see if it works in this specific case. Basically in fn serialize_assets(). It looks like there really was a problem with the additional \\ generated in Windows.

fn serialize_assets(&self, base_path: &PathBuf) -> Vec<String> {
        self.assets
            .iter()
            .filter_map(|asset| asset.file_name())
            .filter_map(|filename| filename.to_str())
            .map(|filename| {
                let mut path = self.file.path.clone();
                // Popping the index.md from the path since file.parent would be one level too high
                // for our need here
                path.pop();
                path.push(filename);

               // Added this, pretty naive but just for testing purposes
               let base_path_string = &base_path.display().to_string();
               let base_path_new = PathBuf::from(&base_path_string[1..]);

                path = path
                    // Replaced base_path with base_path_new, which is basically base_path less the two "\\"
                    //.strip_prefix(&base_path.join("content"))
                    .strip_prefix(&base_path_new.join("content"))
                    .expect("Should be able to stripe prefix")
                    .to_path_buf();
                path
            })
            .map(|path| path.to_string_lossy().to_string())
            .collect()
    }

    // This works and get the Ok from strip_prefix

This works and the assets are properly transferred to the build directory BUT... I am now getting a different problem. Zola says it cannot find the templates.

To simplify testing, I used the Zola starter and tried to build the project. I get the following error:

Building site...
-> Creating 1 pages (0 orphan), 1 sections, and processing 0 images
Failed to build the site
Error: Failed to render page '//server/maindirectory/subdirectory/subdirectorytwo/myfolder/test/zola_starter/content/blog\first.md'
Reason: Tried to render `blog-page.html` but the template wasn't found

Also tried this with just the section template

+++
title = "List of blog posts"
sort_by = "date"
template = "blog.html"
+++

But get a similar error, Tried to render blog.html but the template wasn't found. My guess is that Zola can't find the templates directory due to some path issues. Do you think this is a possibility? Where's a good place to look for this problem?

Is there are way to pass a path to template in the front-matter?

Really like Zola! Hope there's a way around this.

@Keats
Copy link
Collaborator

Keats commented May 5, 2021

Hm that seems like a lot of path issues, weird that no one ran into them... Are you using some specific systems like WSL or something?

@kinxiel
Copy link
Author

kinxiel commented May 6, 2021

If I inspect the directory properties, it says "Windows Server", so probably the usual Windows file server?

I've tried copying the project to a thumb drive and executing the binary from within the thumb drive, aside from the slightly slower build speed it works perfectly. Wondering maybe it has something to do with the file server not starting with a "letter" drive (like C:)? the thumb drive on windows starts with a letter as well. Hmmm

Is there a way to print the path that Zola is using to look for the templates?

@Keats
Copy link
Collaborator

Keats commented May 6, 2021

It would be super helpful if we had another Windows user that could try to reproduce.

Is there a way to print the path that Zola is using to look for the templates?

It should be path of directory of config.toml + templates, joined by whatever is the OS separator.

Are you able to write a test that fails?

@kinxiel
Copy link
Author

kinxiel commented May 10, 2021

Yeah, would be great if someone else on Windows could reproduce the issue.

My guess at the moment is that it is a similar problem related to the initial issue. Probably for some reason std::path is prefixing the path with an additional "\" on Windows volumes without a letter drive, which is an invalid path.

I am not familiar with the entire codebase, but am I right to assume that most of the path related functionality is based on std::path? Perhaps this might be an edge case not yet fully explored.

I will try to investigate a little bit more.

@nyanpasu64
Copy link
Contributor

I think this is the same bug as #1939, which is supposedly fixed by #1941 merged into the next branch (though I can't test that branch since I can't any prebuilt CI artifacts, and cannot build Zola locally to test that PR because sass-sys tries to use VS2019 to build a VS2022 .sln file and fails).

@Keats Keats added the Need Windows help We need someone using Windows to help us with this issue label Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted Need Windows help We need someone using Windows to help us with this issue
Projects
None yet
Development

No branches or pull requests

3 participants