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

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

Open
lukas-shawford opened this issue Nov 8, 2024 · 2 comments

Comments

@lukas-shawford
Copy link

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

@lukas-shawford
Copy link
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
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant