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

Bump zinc and account for diagnostic code #1912

Merged
merged 1 commit into from
Oct 8, 2022

Conversation

ckipp01
Copy link
Contributor

@ckipp01 ckipp01 commented Jul 1, 2022

At the moment this is just testing a bit. I see you already have a test
PR to bump zinc in #1845 but one
of the things that this brings in is the changes to Problem I made in
sbt/sbt#6874 that expose the diagnostic code of
the diagnostic coming from dotc. I have been doing some work on that on
the compiler side in scala/scala3#15565 and
wanted to try it out with Mill.

I tried to mimic the way you currently have it set up, so let me know if
it's not the direction you'd want to go. However, the idea here would be
that the diagnostic code is forwarded when diagnostics are published via
BSP so that Metals could then capture that code and know what code
actions to offer. You can see more of the big picture in scala/scala3#14904.

@lefou
Copy link
Member

lefou commented Jul 12, 2022

Hi @ckipp01, what's the state of this? Could you bump this to zinc 1.7.0?

I updated #1845 to zinc 1.7.0 and now it fails three CI workflows. The CaffeineTests to be specific. Do you have any idea what's changed, that is causing this?

@ckipp01
Copy link
Contributor Author

ckipp01 commented Jul 12, 2022

Hi @ckipp01, what's the state of this? Could you bump this to zinc 1.7.0?

Yea, later this week I should have some time to look back at this, but don't let it block updating to 1.7.0 in the other pr. Plus, this won't really matter until Dotty starts giving this information, and I'm still stuck on that since when testing with the locally published version of dotty that gives those, they are alway None so I'm missing something that I need to figure out.

As for the failures in the other PR, no idea.

ckipp01 added a commit to ckipp01/metals that referenced this pull request Aug 9, 2022
This relates to the grand plan of
scala/scala3#14904 and recently forwarding
the `diagnosticCode` has been merged in
scala/scala3#15565 and also backported so it
should show up in the 3.2.x series. While this pr isn't super exciting,
it's just making sure we capture the code and forward it, this should
unlock _much_ better ways to determine what code actions are available
for a given diagnostic. Meaning we don't have to do lovely things like
regex on the diagnostic message for Scala 3 diagnostics.

NOTE: that this does need some more changes in the build servers before
this is usable. So we can wait for those to be merged in if you'd like.

- [ ] sbt - sbt/sbt#6998
- [ ] Bloop - scalacenter/bloop#1750
- [ ] Mill - com-lihaoyi/mill#1912

Now if you look at the trace file for a diagnostic you'll see the
addition of the code:

```
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 9,
          "character": 15
        },
        "end": {
          "line": 9,
          "character": 19
        }
      },
      "severity": 1,
      "code": "7",
      "source": "sbt",
      "message": "Found:    (\u001b[32m\"hi\"\u001b[0m : String)\nRequired: Int\n\nThe following import might make progress towards fixing the problem:\n\n  import sourcecode.Text.generate\n\n"
    }
  ],
```

Refs: scala/scala3#14904
@ckipp01
Copy link
Contributor Author

ckipp01 commented Aug 9, 2022

I honestly have no idea what's going on with this. I was able to get this added to sbt by just mapping the diagnostic code pretty easily in https://github.com/sbt/sbt/pull/6998/files, but when trying to do the same thing here the code is just never there. I'm not sure if I'm misunderstanding something with how the compiler bridge works with Mill, but I can't get this to work. Do you by chance have any insight into what I might be doing wrong here or why this might not be working? You should be able to test this with 3.2.1-RC1-bin-20220805-e560c2d-NIGHTLY as your scalaVersion and then the Problem should end up having what was added in scala/scala3#15565, but it just doesn't seem to ever be set. At first I thought I did something wrong with my Dotty pr, but after seeing it work in sbt I must just be missing something here. Any insight would be greatly appreciated.

@lefou
Copy link
Member

lefou commented Aug 9, 2022

I honestly have no idea what's going on with this. I was able to get this added to sbt by just mapping the diagnostic code pretty easily in https://github.com/sbt/sbt/pull/6998/files, but when trying to do the same thing here the code is just never there. I'm not sure if I'm misunderstanding something with how the compiler bridge works with Mill, but I can't get this to work. Do you by chance have any insight into what I might be doing wrong here or why this might not be working? You should be able to test this with 3.2.1-RC1-bin-20220805-e560c2d-NIGHTLY as your scalaVersion and then the Problem should end up having what was added in lampepfl/dotty#15565, but it just doesn't seem to ever be set. At first I thought I did something wrong with my Dotty pr, but after seeing it work in sbt I must just be missing something here. Any insight would be greatly appreciated.

I can't promise anything, but just from looking at the single line change in sbt, one potential difference between sbt and Mill could be the way we feed the source files. I don't know if we do it differently, but zinc has introduced some VirtualFile concept and it could be we use a different implementation which does not meet some assumptions which are fulfilled by sbt. This is just a guess though. It could also be caused by the use of different APIs, as we can't call all the sbt-private zinc API as sbt does. Maybe, we need to enable something before zinc/scalac is producing this info?

@ckipp01
Copy link
Contributor Author

ckipp01 commented Aug 13, 2022

I can't promise anything, but just from looking at the single line change in sbt, one potential difference between sbt and Mill could be the way we feed the source files. I don't know if we do it differently, but zinc has introduced some VirtualFile concept and it could be we use a different implementation which does not meet some assumptions which are fulfilled by sbt. This is just a guess though. It could also be caused by the use of different APIs, as we can't call all the sbt-private zinc API as sbt does. Maybe, we need to enable something before zinc/scalac is producing this info?

I don't think this should really have anything to do with it since this change touches a very small area, and is only reflected in the diagnosticCode being present. So I can tell that the version of Zinc we have here is correct since we have those fields, but I just can't figure out why it's always empty. When it's reported from Dotty, the sbt-bridge that is being used by that Scala version (3.2.1-RC1-bin-20220805-e560c2d-NIGHTLY) for example should be reporting it. So it should be there just like it is in sbt. I'm going to try to figure it out in Bloop first since maybe there I'll figure out something I'm missing here.

@ckipp01
Copy link
Contributor Author

ckipp01 commented Aug 23, 2022

btw update on this, I need sbt/sbt#7006 merged in first, and then I need to do an update to Zinc https://github.com/sbt/zinc/blob/5804d50afd960330f34af35e4367fdb171204f5a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/LoggedReporter.scala#L133-L142 and then hopefully this should be working here.

@ckipp01
Copy link
Contributor Author

ckipp01 commented Oct 3, 2022

So Mima is failing, but this is now working as expected. For example given the following code in Scala 3:

package other

object Main extends App {
  val a = "hi"
  def test(a: Int) = if (a < 0) "negative" else "positive"
  test(a)
}

If you run it with Mill you'll see:

❯ ./mill-release minimal3.compile
[32/32] minimal3.compile
[info] compiling 1 Scala source to /Users/ckipp/Documents/scala-workspace/minimal/out/minimal3/compile.dest/classes ...
[error] -- [E007] Type Mismatch Error: /Users/ckipp/Documents/scala-workspace/minimal/minimal3/src/Main.scala:6:7
[error] 6 |  test(a)
[error]   |       ^
[error]   |       Found:    (other.Main.a : String)
[error]   |       Required: Int
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] one error found
1 targets failed
minimal3.compile Compilation failed

And then when used as a BSP server Mill will be correctly reporting the code:

[Trace - 08:26:06 am] Received notification 'build/publishDiagnostics'
Params: {
  "textDocument": {
    "uri": "file:/Users/ckipp/Documents/scala-workspace/minimal/minimal3/src/Main.scala"
  },
  "buildTarget": {
    "uri": "file:///Users/ckipp/Documents/scala-workspace/minimal/minimal3?id\u003dminimal3"
  },
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 5,
          "character": 7
        },
        "end": {
          "line": 5,
          "character": 8
        }
      },
      "severity": 1,
      "code": "7",
      "source": "mill",
      "message": "Found:    (other.Main.a : String)\nRequired: Int"
    }
  ],
  "reset": true
}

@ckipp01 ckipp01 marked this pull request as ready for review October 3, 2022 07:15
@ckipp01 ckipp01 requested a review from lefou October 3, 2022 07:15
Copy link
Member

@lefou lefou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this PR. I wonder if there is an easy way to test this?

There is a binary incompatibility in mill.api.Problem, which needs to be fixed when this PR shall make it into Mill 0.10 series. I think, one possible solution could be to let the new method diagnosticCode have a default implementation, which could return None, so that older code inheriting this trait won't fail to load. We can make that change temporary and remove it once we are in 0.11 development scope. (It would be fine to already open a draft PR with the removal of the default value.)

At the moment this is just testing a bit. I see you already have a test
PR to bump zinc in com-lihaoyi#1845 but one
of the things that this brings in is the changes to `Problem` I made in
sbt/sbt#6874 that expose the diagnostic code of
the diagnostic coming from dotc. I have been doing some work on that on
the compiler side in scala/scala3#15565 and
wanted to try it out with Mill.

I tried to mimic the way you currently have it set up, so let me know if
it's not the direction you'd want to go. However, the idea here would be
that the diagnostic code is forwarded when diagnostics are published via
BSP so that Metals could then capture that code and know what code
actions to offer. You can see more of the big picture in scala/scala3#14904.
@ckipp01
Copy link
Contributor Author

ckipp01 commented Oct 8, 2022

I wonder if there is an easy way to test this?

It depends I guess. Testing it at which phase, during the actual BSP reporting? I looked a bit and didn't see any tests regarding diagnostic reporting, but I may have missed them. Did I?

I think, one possible solution could be to let the new method diagnosticCode have a default implementation, which could return None, so that older code inheriting this trait won't fail to load.

Ah yea, should have thought of this. I went ahead and added a default and then also added a TODO. I can create another draft after the merge to remove this so we don't forget.

@ckipp01 ckipp01 requested a review from lefou October 8, 2022 13:03
@lefou
Copy link
Member

lefou commented Oct 8, 2022

I wonder if there is an easy way to test this?

It depends I guess. Testing it at which phase, during the actual BSP reporting? I looked a bit and didn't see any tests regarding diagnostic reporting, but I may have missed them. Did I?

You are right. We currently don't have any thorough BSP workflow testing. I guess, we lack some easy BSP client test toolkit. As we currently don't activate reporting in normal compile runs, there is no existing test into which we can hook easily. I'm fine with the PR without any new test.

Copy link
Member

@lefou lefou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thank you!

@lefou lefou merged commit 0ec0e9e into com-lihaoyi:main Oct 8, 2022
@lefou lefou added this to the 0.10.8 milestone Oct 8, 2022
@ckipp01 ckipp01 deleted the diagnosticCode branch October 9, 2022 10:29
ckipp01 added a commit to ckipp01/metals that referenced this pull request Oct 24, 2022
This relates to the grand plan of
scala/scala3#14904 and recently forwarding
the `diagnosticCode` has been merged in
scala/scala3#15565 and also backported so it
should show up in the 3.2.x series. While this pr isn't super exciting,
it's just making sure we capture the code and forward it, this should
unlock _much_ better ways to determine what code actions are available
for a given diagnostic. Meaning we don't have to do lovely things like
regex on the diagnostic message for Scala 3 diagnostics.

NOTE: that this does need some more changes in the build servers before
this is usable. So we can wait for those to be merged in if you'd like.

- [ ] sbt - sbt/sbt#6998
- [ ] Bloop - scalacenter/bloop#1750
- [ ] Mill - com-lihaoyi/mill#1912

Now if you look at the trace file for a diagnostic you'll see the
addition of the code:

```
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 9,
          "character": 15
        },
        "end": {
          "line": 9,
          "character": 19
        }
      },
      "severity": 1,
      "code": "7",
      "source": "sbt",
      "message": "Found:    (\u001b[32m\"hi\"\u001b[0m : String)\nRequired: Int\n\nThe following import might make progress towards fixing the problem:\n\n  import sourcecode.Text.generate\n\n"
    }
  ],
```

Refs: scala/scala3#14904
tgodzik pushed a commit to scalameta/metals that referenced this pull request Oct 25, 2022
This relates to the grand plan of
scala/scala3#14904 and recently forwarding
the `diagnosticCode` has been merged in
scala/scala3#15565 and also backported so it
should show up in the 3.2.x series. While this pr isn't super exciting,
it's just making sure we capture the code and forward it, this should
unlock _much_ better ways to determine what code actions are available
for a given diagnostic. Meaning we don't have to do lovely things like
regex on the diagnostic message for Scala 3 diagnostics.

NOTE: that this does need some more changes in the build servers before
this is usable. So we can wait for those to be merged in if you'd like.

- [ ] sbt - sbt/sbt#6998
- [ ] Bloop - scalacenter/bloop#1750
- [ ] Mill - com-lihaoyi/mill#1912

Now if you look at the trace file for a diagnostic you'll see the
addition of the code:

```
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 9,
          "character": 15
        },
        "end": {
          "line": 9,
          "character": 19
        }
      },
      "severity": 1,
      "code": "7",
      "source": "sbt",
      "message": "Found:    (\u001b[32m\"hi\"\u001b[0m : String)\nRequired: Int\n\nThe following import might make progress towards fixing the problem:\n\n  import sourcecode.Text.generate\n\n"
    }
  ],
```

Refs: scala/scala3#14904
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