Skip to content

ClasspathAwareImporter fails on empty imported file #79

@qsiebers

Description

@qsiebers

I've been running into this exception in a project when using the ClasspathAwareImporter option:

java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1371)
	at wrm.libsass.Lookup$Result.read(Lookup.java:53)
	at wrm.libsass.Lookup$Result.buildImport(Lookup.java:44)
	at wrm.libsass.ClasspathAwareImporter.lambda$apply$15(ClasspathAwareImporter.java:48)
	at java.util.Optional.map(Optional.java:215)
	at wrm.libsass.ClasspathAwareImporter.apply(ClasspathAwareImporter.java:48)
	at io.bit3.jsass.adapter.NativeImporterWrapper.apply(NativeImporterWrapper.java:39)
	at io.bit3.jsass.adapter.NativeAdapter.compileFile(Native Method)
	at io.bit3.jsass.adapter.NativeAdapter.compile(NativeAdapter.java:44)
	at io.bit3.jsass.Compiler.compile(Compiler.java:124)
	at io.bit3.jsass.Compiler.compileFile(Compiler.java:76)
	at wrm.libsass.SassCompiler.compileFile(SassCompiler.java:45)
	at wrm.AbstractSassMojo.processFile(AbstractSassMojo.java:285)
	at wrm.AbstractSassMojo$1.visitFile(AbstractSassMojo.java:180)
	at wrm.AbstractSassMojo$1.visitFile(AbstractSassMojo.java:175)
	at java.nio.file.Files.walkFileTree(Files.java:2670)
	at java.nio.file.Files.walkFileTree(Files.java:2742)
	at wrm.AbstractSassMojo.compile(AbstractSassMojo.java:175)
	at wrm.CompilationMojo.execute(CompilationMojo.java:41)
	at ... maven stuff

After adding some debugging output I've tracked it down:

Looking for elements/logo in <...>/src/main/sass/_app.scss
Optional[file:<...>/src/main/sass/elements/_logo.scss - /Users/qs/Projects/Cloud/trunk/kamala-cloud/src/main/sass/elements/_logo.scss]
Reading file:<...>/src/main/sass/elements/_logo.scss

Upon inspection, the _logo.scss file turns out to be empty. Re-running the compilation with a non-empty file fixes the exception.

I'm not sure on the use of Scanner, but this patch seems to do the trick:

--- a/src/main/java/wrm/libsass/Lookup.java
+++ b/src/main/java/wrm/libsass/Lookup.java
@@ -49,7 +49,9 @@
 
 		private static String read(URL url) {
 			try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.name())) {
-				return scanner.useDelimiter("\\A").next();
+				if (scanner.hasNext()) {
+					return scanner.useDelimiter("\\A").next();
+				} return "";
 			} catch (IOException e) {
 				throw new RuntimeException("Cannot read the url: " + url, e);
 			}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions