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

pprint.Renderer makes unsafe call to fansi.Str(_), resulting in thrown exception. #78

Open
axb21 opened this issue Feb 9, 2022 · 0 comments

Comments

@axb21
Copy link

axb21 commented Feb 9, 2022

Right here:

new Truncated(str.map(fansi.Str(_)), maxWidth, height = 99999999).toResult

fansi.Str(_) allows one to pass in an ErrorMode controlling whether an exception is thrown. But this call here defaults to throwing an exception. One can also sanitize the input before it is sent to fansi.Str(_), but that is not done either. There do not appear to be hooks to allow you to do either of these things if so desired, but I'm virtually ignorant of this codebase so I wouldn't know.

The reason this behavior is bad is that mdoc processing of scala worksheets explodes if the output of some line of code contains content that would cause fansi.Str(_) to explode, with no straightforward way to avoid this behavior. I'm seeing this frequently in my current project:

java.lang.IllegalArgumentException: Unknown ansi-escape at index XXX inside string cannot be parsed into an fansi.Str
	at fansi.ErrorMode$Throw$.handle(Fansi.scala:419)
	at fansi.ErrorMode$Throw$.handle(Fansi.scala:407)
	at fansi.Str$.apply(Fansi.scala:272)
	at pprint.Renderer.$anonfun$rec$32(Renderer.scala:147)
	at scala.collection.Iterator$$anon$9.next(Iterator.scala:575)
	at scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:471)
	at pprint.Truncated.hasNext(Truncated.scala:80)
	at scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:470)
	at pprint.Truncated.hasNext(Truncated.scala:80)
	at scala.collection.Iterator$$anon$9.hasNext(Iterator.scala:574)
	at scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:470)
	at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:592)
	at scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:470)
	at scala.collection.mutable.StringBuilder.appendAll(StringBuilder.scala:185)
	at mdoc.internal.document.Printing$.printOneLine(Printing.scala:28)
	at mdoc.internal.worksheets.WorksheetProvider.$anonfun$renderSummary$1(WorksheetProvider.scala:141)
	at mdoc.internal.worksheets.WorksheetProvider.$anonfun$renderSummary$1$adapted(WorksheetProvider.scala:124)
	at scala.collection.IterableOnceOps.foldLeft(IterableOnce.scala:646)
	at scala.collection.IterableOnceOps.foldLeft$(IterableOnce.scala:642)
	at scala.collection.AbstractIterator.foldLeft(Iterator.scala:1288)
	at mdoc.internal.worksheets.WorksheetProvider.renderSummary(WorksheetProvider.scala:124)
	at mdoc.internal.worksheets.WorksheetProvider.renderDecoration(WorksheetProvider.scala:73)
	at mdoc.internal.worksheets.WorksheetProvider.$anonfun$evaluateWorksheet$3(WorksheetProvider.scala:42)
	at scala.collection.immutable.List.map(List.scala:250)
	at mdoc.internal.worksheets.WorksheetProvider.$anonfun$evaluateWorksheet$2(WorksheetProvider.scala:41)
	at scala.collection.Iterator$$anon$10.nextCur(Iterator.scala:585)
	at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:599)
	at scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:470)
	at scala.collection.Iterator$$anon$9.hasNext(Iterator.scala:574)
	at scala.collection.immutable.List.prependedAll(List.scala:152)
	at scala.collection.IterableOnceOps.toList(IterableOnce.scala:1251)
	at scala.collection.IterableOnceOps.toList$(IterableOnce.scala:1251)
	at scala.collection.AbstractIterator.toList(Iterator.scala:1288)
	at mdoc.internal.worksheets.WorksheetProvider.evaluateWorksheet(WorksheetProvider.scala:48)
	at mdoc.internal.worksheets.Mdoc.evaluateWorksheet(Mdoc.scala:55)
	at scala.meta.internal.worksheets.WorksheetProvider.scala$meta$internal$worksheets$WorksheetProvider$$evaluateWorksheet(WorksheetProvider.scala:363)
	at scala.meta.internal.worksheets.WorksheetProvider$$anon$1.liftedTree1$1(WorksheetProvider.scala:267)
	at scala.meta.internal.worksheets.WorksheetProvider$$anon$1.run(WorksheetProvider.scala:267)

I'm sorry that I cannot share the code or data that produces this.

Yes, I'll report an issue to mdoc too. I understand that throwing exceptions might be a design choice for this library. However, it seems like it should be possible to make pprint.Render a little safer, given that the underlying fansi library allows one to control exception throwing behavior.

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