Open
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:
Activity
lukas-shawford commentedon Nov 8, 2024
I was able to work around it by specifying the file provider:
lukas-shawford commentedon Nov 8, 2024
Also, in case it helps anyone... if you renamed the local environment name from
Development
to something else (for instance, I call itlocal
), then you need to ensure that you callbuilder.WebHost.UseStaticWebAssets()
BEFORE you callpipeline.AddScssBundle
. This will ensure thatenv.WebRootFileProvider
is aCompositeFileProvider
, not aPhysicalFileProvider
.When calling
UseFileProvider
, ifenv.WebRootFileProvider
is not already aCompositeFileProvider
, 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 ifHostingEnvironment.IsDevelopment()
returns true (which will then take care of switching theWebRootFileProvider
to useCompositeFileProvider
). 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.