* Add NodePath to operations
* Change to use nodePath to get pathToNode instead of sourceRange
* Add additional node path unit test
* Update output
* Fix import statement NodePaths
* Update output
* Factor into function
Previously in a member expression like `foo.x` or `foo[3]`, `foo` had to be an identifier. You could not do something like `f().x` (and if you tried, you got a cryptic error). Rather than make the error better, we should just accept any expression to be the LHS of a member expression (aka its 'object').
This does knock our "parse lots of function calls" from 58 to 55 calls before it stack overflows. But I think it's fine, we'll address this in https://github.com/KittyCAD/modeling-app/pull/6226 when I get back to it.
Closes https://github.com/KittyCAD/modeling-app/issues/7273
* Move import graph to execution
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* Refactor artifact handling
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* Refactor caching to separate global state from per-module state
Signed-off-by: Nick Cameron <nrc@ncameron.org>
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
This brings the `execute_mock` function into line with the `execute` function, which I tweaked in https://github.com/KittyCAD/modeling-app/pull/7351. Now mock execution, like real execution, will always return a properly-formatted KCL error, instead of any possible JS value.
Also, incidentally, I noticed that send_response always succeeds, so I changed it from Result<()> to void.
# Symptoms
This code produces a big ugly confusing error in the frontend, see #7340.
# Root cause
I added a new test case, with an unknown type. In `ast.snap` under `body[0].declaration.init.ty` there two different `type` fields in the AST node for the type's name, and they have conflicting values Primitive and Identifier.
<img width="602" alt="Screenshot 2025-06-03 at 4 04 55 PM" src="https://github.com/user-attachments/assets/913a0fa0-3e8d-473f-bb64-003d44915be0" />
# Solution
Change the `enum PrimitiveType` variant from `Named(Node<Identifier>)` to `Named { name: Node<Identifier> }` so that the fields nest differently.
Now the error correctly points out to the user that the type `NotARealType` can't be found. Much better error message that shows the user the problem.
# Alternative solutions
Stop the duplicated JSON fields altogether. I tried this previously in https://github.com/KittyCAD/modeling-app/pull/4369 but it was very involved, and I didn't think it was worth it. Maybe I should reopen that PR and solve this properly.
Closes#7340
I ignored some new clippy lints about large differences between enum variants.
We can always revisit these later (the compiler suggests boxing them so
that the enum variants are similar size)
There's some bug in the frontend or KCL somewhere, which results in the TypeScript frontend sending an AST (serialized to JSON) to the KCL executor, but the JSON cannot be deserialized into an AST. If this happens, it's a bug in ZDS, not a user error.
The problem is that this sort of error will cause the frontend to silently stop rendering KCL, and it won't show the user any errors. They need to open up the console and look at the error there, and even if they do, it's hard to understand.
This PR changes how we report these unexpected errors due to bugs in ZDS. ZDS should not silently stop working, it should at least print a half-decent error like this:
<img width="527" alt="nicer error" src="https://github.com/user-attachments/assets/1bb37a64-0915-4472-849c-d146f397356b" />
## Fix
Right now, the wasm library exports a function `execute`. It previous returned an error as a String if one occurred. The frontend assumed this error string would be JSON that matched the schema `KclErrorWithOutputs`. This was not always true! For example, if something couldn't be serialized to JSON, we'd take the raw Serde error and stringify that. It wouldn't match `KclErrorWithOutputs`.
Now I've changed `execute` so that if it errors, it'll returns a JsValue not a string. So that's one check (can this string be deserialized into a JSON object) that can be removed -- it'll return a JSON object directly now. The next check is "does this JSON object conform to the KclErrorWithOutputs schema". To prove that's correct, I changed `execute` to be a thin wrapper around `fn execute_typed` which returns `Result<ExecOutcome, KclErrorWithOutputs>`. Now we know the error will be the right type.
Previously, `x = cos(x)` would just say "`x` is undefined". Now it says that `x` cannot be referenced in its own definition, try using a different variable instead.
To do this, I've added a new `Option<String>` field to the mod-local executor context, tracking the current variable declaration. This means cloning some strings, implying a small performance hit. I think it's fine, for the better diagnostics.
In the future we could refactor this to use a &str or store variable labels in stack-allocated strings like docs.rs/compact_str or something.
Closes https://github.com/KittyCAD/modeling-app/issues/6072
We've changed the unnamed field of `KclError` variants to a named called `details`.
To clarify: previously KCL errors looked like this:
```rust
pub enum KclError {
Lexical(KclErrorDetails),
Syntax(KclErrorDetails),
```
Now they look like this:
```rust
pub enum KclError {
Lexical { details: KclErrorDetails },
Syntax { details: KclErrorDetails },
}
```
This lets us more easily add fields to the errors. For example, in the UndefinedValue case, adding a field for what the undefined name was. This PR refactors the code to make my PR in https://github.com/KittyCAD/modeling-app/pull/7309 much easier.
Pure refactor, should not change any behaviour.
This program:
```kcl
1
|> extrude(
length=depth,
})
```
was giving this bad error:
```
unexpected token |>
```
Now it gives
```kcl
There was an unexpected }. Try removing it.
```
and it correctly puts the diagnostic on the extra }.
Fixes https://github.com/KittyCAD/modeling-app/issues/6126
Previously, this KCL
```
arc(
endAbsolute = [0, 50]
interiorAbsolute = [-50, 0]
)
```
gave the error `This argument has a label, but no value. Put some value after the equals sign`.
Now it gives this much better error `Missing comma between arguments, try adding a comma in`, and its source range (red underline) is on the whitespace which was missing a comma:
<img width="666" src="https://github.com/user-attachments/assets/aa5035f5-f748-4dab-b918-b81b05733323" />
Thanks for reporting this @benjamaan476
* Move some sketch functions to KCL
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* Move asserts to KCL
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* sweep, loft -> KCL
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* Move pattern transforms to KCL
Signed-off-by: Nick Cameron <nrc@ncameron.org>
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* Replace uses of get_unlabeled_kw_arg with _typed version
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* Remove more untyped arg getters
Signed-off-by: Nick Cameron <nrc@ncameron.org>
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
* Only consider staight lines for colinear check
* Neaten up code and add test
* Sir, a second sphere has hit the unit test
* Update test snapshots
---------
Co-authored-by: Adam Chalmers <adam.chalmers@zoo.dev>