Skip to content

CacheKey generation exception when source scss files are in Razor Class Library #80

Open
@lukas-shawford

Description

When adding an scss bundle with source files from a Razor Class Library project:

pipeline.AddScssBundle("/shared.css", "_content/WebApplication1.SharedAssets/styles/shared-styles.scss");

SCSS compilation fails with the following error:

System.Exception: CacheKey generation exception in WebOptimizer.Sass.Compiler processor
 ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\Me\Sample\WebApplication1\WebApplication1\wwwroot\_content\WebApplication1.SharedAssets\styles\shared-styles.scss'.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFileInfo.CreateReadStream()
   at WebOptimizer.Sass.Compiler.GenerateCacheKey(HttpContext context, IAssetContext config)
   at WebOptimizer.Sass.Compiler.CacheKey(HttpContext context, IAssetContext config)
   at WebOptimizer.Asset.GenerateCacheKey(HttpContext context, IWebOptimizerOptions options)
   --- End of inner exception stack trace ---
   at WebOptimizer.Asset.GenerateCacheKey(HttpContext context, IWebOptimizerOptions options)
   at WebOptimizer.AssetBuilder.BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options)
   at WebOptimizer.AssetMiddleware.HandleAssetAsync(HttpContext context, IAsset asset, WebOptimizerOptions options)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

It works fine when creating a plain CSS bundle:

pipeline.AddScssBundle("/shared-2.css", "_content/WebApplication1.SharedAssets/styles/shared-styles-2.css");

So it is just the SCSS compiler that is throwing an error.

Refer to this PR for how it was fixed in the main WebOptimizer project:

https://github.com/ligershark/WebOptimizer/pull/287/files

Activity

lukas-shawford

lukas-shawford commented on Nov 8, 2024

@lukas-shawford
Author

I was able to work around it by specifying the file provider:

IWebHostEnvironment env = builder.Environment;

pipeline.AddScssBundle("/shared.css", "_content/WebApplication1.SharedAssets/styles/shared-styles.scss")
    .UseFileProvider(env.WebRootFileProvider);
lukas-shawford

lukas-shawford commented on Nov 8, 2024

@lukas-shawford
Author

Also, in case it helps anyone... if you renamed the local environment name from Development to something else (for instance, I call it local), then you need to ensure that you call builder.WebHost.UseStaticWebAssets() BEFORE you call pipeline.AddScssBundle. This will ensure that env.WebRootFileProvider is a CompositeFileProvider, not a PhysicalFileProvider.

When calling UseFileProvider, if env.WebRootFileProvider is not already a CompositeFileProvider, then it will not work, and you need to rearrange your middleware.

The reason the environment name is relevant here is ASP.NET Core will automatically call UseStaticWebAssets for you if HostingEnvironment.IsDevelopment() returns true (which will then take care of switching the WebRootFileProvider to use CompositeFileProvider). But if you renamed the environment, then this configuration is no longer automatic and you must perform it yourself.

Just posting this in case it helps anyone, took a while to figure this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @lukas-shawford

      Issue actions

        CacheKey generation exception when source scss files are in Razor Class Library · Issue #80 · ligershark/WebOptimizer.Sass