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

Improve WASM layer error handling #149

Open
1 of 2 tasks
Tracked by #50
swernli opened this issue Apr 12, 2023 · 1 comment
Open
1 of 2 tasks
Tracked by #50

Improve WASM layer error handling #149

swernli opened this issue Apr 12, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@swernli
Copy link
Collaborator

swernli commented Apr 12, 2023

The qsc_wasm layer does not fully handle the errors reported by check_internal, as some information gets lost in translation (secondary labels, help text, etc). In addition, run_internal API currently only supports returning a single error rather than a list, which might not work for errors from passes like extracting entry point. We need to update the error handling to support multiple, detailed errors, and work on the compilation/execution context caching semantics to better provide actionable errors to the user via WASM.

Tasks

@swernli swernli added the enhancement New feature or request label Apr 12, 2023
@swernli swernli added this to the Katas MVP milestone Apr 17, 2023
@swernli swernli assigned billti and unassigned cesarzc May 11, 2023
@swernli swernli removed this from the Initial MVP milestone Aug 29, 2023
@minestarks
Copy link
Member

minestarks commented Oct 9, 2023

Not sure if was meant to be covered in this issue, but another concern is that errors contain SourceMap offsets which do not get properly mapped to file offsets. The effect can be seen in this example where the runtime error is coming from stdlib, but get show at a random point in the current file.

This is also the root cause of #750 , I think

github-merge-queue bot pushed a commit that referenced this issue Oct 17, 2023
Fixes #750 . Addresses some of #149 .

Runtime error squiggles in the playground were off by one. The root
cause was that we actually use `SourceMap` offsets, not `Source`
offsets, to report the error. This sometimes used to "just work" since
we always have a single file in the compilation. However, this really
broke down when #614 inserted an additional EOF character between
sources... and then when #622 moved the entry expression (`""` in this
case) to the _beginning_ of the source map.

Moreover, when runtime error spans came from stdlib, the offsets were
just wildly off (since the spans were actually coming from stdlib). This
probably wasn't noticeable since the UI "covered up" the problem by
mapping the squiggles to the very end of the document.

So, the bulk of this PR is properly resolving `SourceMap` offsets to
`Source` offsets in the wasm layer. When the span doesn't belong in the
current document, we just show a squiggle at the beginning of the
document. But we also take advantage of the "related information"
feature of Monaco to still include the relevant stdlib labels in the
hover text (see screenshots below).

This brings spans to the realm of "technically correct", but I'm still
not happy with the experience in the playground UI for runtime errors.
For a better experienece, we should consider:

- Either, grabbing spans from the call stack and squiggle the statements
in the stack.
- Or, not bothering with runtime error squiggles at all....

However, this change was already getting too big, and the callstack
changes would have been pretty complicated. I decided to just go with
this as an incremental improvement, and debate the rest later.

### Playground runtime error changes

**Case 1: The runtime error has a label that belongs in the current
file**

This had the off-by-one error, now fixed.

**Before**


![image](https://github.com/microsoft/qsharp/assets/16928427/a94c83db-15e4-424c-82ea-75fd99240866)

**After**


![image](https://github.com/microsoft/qsharp/assets/16928427/1665c0e1-e2db-4ffb-8832-2bb2f459a01e)

**Case 2: The runtime error has a label that belongs in stdlib**

This used to map to the end of the file (just by luck), now it maps to
the beginning of the file and shows the label from the original location
in the hover text.

**Before**


![image](https://github.com/microsoft/qsharp/assets/16928427/2a45a1a7-d195-4a1d-8972-208d99f92e49)

**After**


![image](https://github.com/microsoft/qsharp/assets/16928427/99c997bf-371c-4f94-962f-ebb35152b6e3)

**Case 3: The runtime error has a label that belongs in stdlib, _and_
the label doesn't contain a message**

This is the worst experience - we could show the stdlib location in the
hover, but there's no message in the label so it would be entirely
pointless to display. So we omit it. The location of the squiggle is now
technically correct but just as unhelpful.

**Before**

![image](https://github.com/microsoft/qsharp/assets/16928427/7d4a0878-3cd4-4f56-b9be-921b70bac9f1)

**After**

![image](https://github.com/microsoft/qsharp/assets/16928427/624aa247-f62c-4a4e-91e8-2c92a0547a6a)

### Other goodness:

- In the playground, we clear the last runtime squiggle when the user
starts typing (or click on a different sample).

- Properly reporting compiler errors with multiple spans (e.g.
[`Ambiguous`](https://github.com/microsoft/qsharp/blob/c75940913518763000a3b18b053ef683cc0e3ad7/compiler/qsc_frontend/src/resolve.rs#L57))
using VSCode/Monaco's "related information" feature. These related spans
show up in the error hover.


![image](https://github.com/microsoft/qsharp/assets/16928427/62ab9118-0603-4d26-9cac-6c1bf2f17f9c)

- `qsc::compile::compile()` now just returns a `WithSource<Error>`
instead of relying on the caller to use the `SourceMap` to resolve error
spans to source files. Every caller has to do this mapping at _some_
point to report errors, so it makes sense for the errors to just contain
that information from the start. Using `WithSource` as the error type
also mirrors the error types that `Interpreter` and `Incremental`
already return.

- Somehow `VSDiagnostic` was defined in two places. One with the name
`IDiagnostic` and the other `VSDiagnostic`. They describe the same
thing. Deleted one of the definitions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants