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

StackOverflowError in Scanner.fetchToken #41

Closed
lefou opened this issue Jul 21, 2022 · 4 comments · Fixed by #42
Closed

StackOverflowError in Scanner.fetchToken #41

lefou opened this issue Jul 21, 2022 · 4 comments · Fixed by #42

Comments

@lefou
Copy link

lefou commented Jul 21, 2022

I see this error message when I try to parse this file: https://github.com/com-lihaoyi/mill/blob/8903f225fabf6d4605a6a8d5f741910fcc0ed256/build.sc

Caused by: java.lang.StackOverflowError
         at com.virtuslab.using_directives.custom.Scanner.fetchToken(Scanner.java:539)
         at com.virtuslab.using_directives.custom.Scanner.fetchToken(Scanner.java:545)
         at com.virtuslab.using_directives.custom.Scanner.fetchToken(Scanner.java:545)
...
        at com.virtuslab.using_directives.custom.Scanner.fetchToken(Scanner.java:545)
        at com.virtuslab.using_directives.custom.Scanner.nextToken(Scanner.java:202)
        at com.virtuslab.using_directives.custom.Scanner.<init>(Scanner.java:32)
        at com.virtuslab.using_directives.custom.Parser.<init>(Parser.java:23)

This is the code:

val content = new String(os.read.bytes(buildSc), Charset.forName("UTF-8"))
val extractor = new SimpleCommentExtractor(content.toCharArray(), true)

val settings = new Settings(false, true)
val context = new Context(new ConsoleReporter(), settings)

val source = new Source(extractor.extractComments())
val parser = new Parser(source, context)
val parsed = parser.parse()
val usingDefs = parsed.getUsingDefs()
@lefou lefou changed the title StackOverflowError StackOverflowError in Scanner Jul 21, 2022
@lefou lefou changed the title StackOverflowError in Scanner StackOverflowError in Scanner.fetchToken Jul 21, 2022
lefou added a commit to lefou/mill that referenced this issue Jul 22, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.
@lefou lefou mentioned this issue Jul 22, 2022
12 tasks
lefou added a commit to lefou/mill that referenced this issue Jul 22, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.
@tgodzik
Copy link
Member

tgodzik commented Jul 22, 2022

I see two problems:

  • it seems we are not stopping after reaching the code section
  • we are calling fetchToken recursively, which is a bad idea for a tokenizer

@KacperFKorban
Copy link
Member

Hi, try using UsingDirectivesProcessor instead of SimpleCommentExtractor. SimpleCommentExtractor is more of a util class that takes comments that have already been preprocessed in some way.
Try something like this:

val content = new String(os.read.bytes(buildSc), Charset.forName("UTF-8"))

val settings = new Settings(false, true)
val context = new Context(new ConsoleReporter(), settings)

val processor = new UsingDirectivesProcessor(context)
val usingDefs = processor.extract(content /* might need a .toArray here or sth similar */, true, false)

A good source of inspiration would be to see how scala-cli handles using directives. e.g. https://github.com/VirtusLab/scala-cli/blob/9b4f317c11daa30aeb2649cdf8b9496304b2a39f/modules/build/src/main/scala/scala/build/preprocessing/ExtractedDirectives.scala

This does not mean of course that this is not a bug. It seems that the Parser can't handle large amounts of whitespaces very well.

@lefou
Copy link
Author

lefou commented Jul 25, 2022

Hi, try using UsingDirectivesProcessor instead of SimpleCommentExtractor. SimpleCommentExtractor is more of a util class that takes comments that have already been preprocessed in some way. Try something like this:

val content = new String(os.read.bytes(buildSc), Charset.forName("UTF-8"))

val settings = new Settings(false, true)
val context = new Context(new ConsoleReporter(), settings)

val processor = new UsingDirectivesProcessor(context)
val usingDefs = processor.extract(content /* might need a .toArray here or sth similar */, true, false)

A good source of inspiration would be to see how scala-cli handles using directives. e.g. https://github.com/VirtusLab/scala-cli/blob/9b4f317c11daa30aeb2649cdf8b9496304b2a39f/modules/build/src/main/scala/scala/build/preprocessing/ExtractedDirectives.scala

This does not mean of course that this is not a bug. It seems that the Parser can't handle large amounts of whitespaces very well.

Thanks for the hint!

Isn't UsingDirectivesProcessor also using SimpleCommentExtractor in its implementation? Only difference is, that it is using the Visitor.

KacperFKorban added a commit to KacperFKorban/using_directives that referenced this issue Jul 25, 2022
KacperFKorban added a commit to KacperFKorban/using_directives that referenced this issue Jul 26, 2022
lefou added a commit to lefou/mill that referenced this issue Jul 27, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.
@lefou
Copy link
Author

lefou commented Sep 3, 2022

Thanks for fixing it. Can you please create a new release containing the fix?

lefou added a commit to lefou/mill that referenced this issue Sep 22, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.

# Conflicts:
#	scalalib/src/mill/scalalib/bsp/BspModule.scala
lefou added a commit to lefou/mill that referenced this issue Oct 9, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.

# Conflicts:
#	scalalib/src/mill/scalalib/bsp/BspModule.scala
lefou added a commit to lefou/mill that referenced this issue Oct 12, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.

# Conflicts:
#	scalalib/src/mill/scalalib/bsp/BspModule.scala
lefou added a commit to lefou/mill that referenced this issue Nov 6, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.

# Conflicts:
#	scalalib/src/mill/scalalib/bsp/BspModule.scala
lefou added a commit to lefou/mill that referenced this issue Dec 16, 2022
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.

# Conflicts:
#	scalalib/src/mill/scalalib/bsp/BspModule.scala
lefou added a commit to lefou/mill that referenced this issue Jan 8, 2023
Using directives parser is currently not able to handle larger files,
see VirtusLab/using_directives#41

Also improved BSP support.
BSP mill-build modules should now be able to compile build files.

# Conflicts:
#	scalalib/src/mill/scalalib/bsp/BspModule.scala
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

Successfully merging a pull request may close this issue.

3 participants