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

Fix line numbers in exceptions for multi-line code blocks #1361

Merged
merged 3 commits into from
Jul 3, 2023

Conversation

vsevolodstep-db
Copy link
Contributor

@vsevolodstep-db vsevolodstep-db commented Jun 26, 2023

What is changed?

The current Parser ignores whitespaces at the beginning of the code block, which may result in incorrect line number in an exception stackTrace:

@ {
  
  
  1 / 0
  } 
java.lang.ArithmeticException: / by zero
  ammonite.$sess.cmd0$.<init>(cmd0.sc:1)
  ammonite.$sess.cmd0$.<clinit>(cmd0.sc)

This PR changes it so the result will be

@ { 
  
  
  1 / 0
  } 
java.lang.ArithmeticException: / by zero
  ammonite.$sess.cmd0$.<init>(cmd0.sc:4)
  ammonite.$sess.cmd0$.<clinit>(cmd0.sc)

(line number is 4 instead of 1)

This it done by introducing a new parser which does not ignore leading whitespaces

Please note that it changes this only for Scala 2. Scala 3 impl has a different parsing logics which I didn't have time to dive into yet

How is it tested?

New tests in FailureTests checking line numbers

@lihaoyi lihaoyi merged commit 94c5ce8 into com-lihaoyi:main Jul 3, 2023
alexarchambault added a commit that referenced this pull request Jul 18, 2023
This fixes a regression introduced in
#1361. This makes the
following work again as expected:
```scala
@ {
    import $ivy.`org.typelevel::cats-kernel:2.6.1`
  }
```

Before the changes here, in Scala 2.x, one gets an error like
```text
cmd0.sc:2: object org.typelevel::cats-kernel:2.6.1 is not a member of package ammonite.$ivy
import $ivy.`org.typelevel::cats-kernel:2.6.1`
       ^
Compilation Failed
```
lihaoyi added a commit to com-lihaoyi/mill that referenced this pull request Aug 8, 2023
… or whitespace (#2686)

This is basically a port of the following fix in the Ammonite repo
com-lihaoyi/Ammonite#1361. There were some
changes when the logic was first ported from Ammonite to Mill, resulting
in a much smaller code change than was necessary in the Ammonite repo

There are two related follow up PRs that AFAICT do not apply to Mill:
com-lihaoyi/Ammonite#1363
and com-lihaoyi/Ammonite#1369. Both have to do
with the special handling of multi-statement top-level-blocks `{...}`,
that is a thing in the Ammonite REPL (to allow multiple top-level
declarations to be entered into the REPL before submitting it) but
doesn't apply to Mill scripts.

I have updated the integration tests to include a bunch of random
comments and imports to reproduce the problem causing the tests to fail,
and verified the fix causes the tests to pass. Also this caused a
previously passing test to fail (`2-custom-build-logic`), which on
inspection appears like it was asserting the wrong thing, so I fixed the
assert

Fixes #2681
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 this pull request may close these issues.

2 participants