Aloxaf / silicon

Create beautiful image of your source code.
MIT License
3.08k stars 82 forks source link

Silicon assumes all config directories exist #242

Open kenielf opened 6 months ago

kenielf commented 6 months ago

The Issue

If I was adding a custom theme, I would normally not need to also create a syntaxes directory, however, the application crashes with the following when trying to rebuild its cache:

$ silicon --build-cache
[error] error finding all the files in a directory: IO error for operation on /home/kenielf/.config/silicon/syntaxes: No such file or directory (os error 2)

Note: this also happens if I was trying to just add custom syntaxes without creating the theme directory

Workaround

Although this can be easily circumvented by simply creating all necessary directories:

mkdir -p ~/.config/silicon/{themes,syntaxes}

However, it might be better to just check first in the code if the directories exist, and if they don't, just skip them altogether.

Fix

Looking into it, the issue seems to be easily resolved by tweaking the add_from_folder function in the impl HighlightingAssets to first check if the directory exists, in the cases where it does not, just skip the building.

I have tested a simple solution and it is working normally on my end:

    pub fn add_from_folder<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        let path = path.as_ref();
        let theme_dir = path.join("themes");
        if theme_dir.is_dir() {
            self.theme_set.add_from_folder(theme_dir)?;
        }
        let mut builder = self.syntax_set.clone().into_builder();
        let syntaxes_dir = path.join("syntaxes");
        if syntaxes_dir.is_dir() {
            builder.add_from_folder(syntaxes_dir, true)?;
            self.syntax_set = builder.build();
        }
        Ok(())
    }

I can submit a PR myself if desired.

uncenter commented 5 months ago

You should definitely submit a PR!

AndydeCleyre commented 6 days ago

I also found that I need to run silicon --build-cache with ~/.config/silicon as my working directory, otherwise it looks for themes in whatever else my working directory is.

kenielf commented 5 days ago

I also found that I need to run silicon --build-cache with ~/.config/silicon as my working directory, otherwise it looks for themes in whatever else my working directory is.

I have just tested this, and yes - it needs the cwd to contain both the directories for themes and syntaxes - if not, the command fails.