Skip to content

Reenable Specta integration #2432

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

oscartbeaumont
Copy link
Contributor

@oscartbeaumont oscartbeaumont commented Mar 14, 2025

This fixes the commented out Specta integration resolving the TODO comment.

It might be worth moving the type exporting code into the startup of the application if possible when doing a debug build so that you don't need to run cargo test ... to generate the bindings but I don't know enough about Graphite's setup to know where is best. I also suppose any Rust running in wasm would not be a good place for that as it won't have filesystem access so a better place may not exist.

TODO:

  • Decide on approach
  • Fix the errors with linter (we can do that from the Specta side)

Part of #1148

@Keavon
Copy link
Member

Keavon commented Mar 14, 2025

Thank you for writing the library and helping us restore this! I'm eager to do the refactor which replaces messages.ts with these generated bindings.

@Keavon Keavon force-pushed the master branch 4 times, most recently from aa7ff13 to e11b57a Compare April 6, 2025 11:41
@Keavon
Copy link
Member

Keavon commented May 2, 2025

Back to reviewing this now! Could you please help me understand the purpose of pnpm-lock.yaml, bindings_from_node.ts, and export.js?

@@ -34,6 +34,18 @@ pub fn init_graphite() {
log::set_max_level(log::LevelFilter::Debug);
}

#[cfg(debug_assertions)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We expose a wasm function that returns a string with all the Specta types.

This is only enabled in development so Rust can strip more for production builds.

import path from "node:path";
import graphite, { get_specta_types } from "./pkg/graphite_wasm.js";

graphite(fs.readFileSync(path.join(import.meta.dirname, './pkg/graphite_wasm_bg.wasm'))).then(() =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We import the wasm file and then call the get_specta_types function and save the result to the disk.

By doing this we don't need to build a separate x86/ARM binary specially for exporting the types. Commonly this would be done via cargo test. By having it be included in the binary makes it way quicker for Graphite's specific setup.

Copy link
Member

Choose a reason for hiding this comment

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

I'm mostly following how this works, but still confused about when and how to use it and how it compares to the #[test] fn generate_ts_types(). How do we run this export.js file and when is that supposed to occur? The goal would be to have the bindings file be gitignored but autogenerated whenever the build server starts, or when recompiling the build server based on any change to a Rust file, which should pick up the changes to any of the Specta bindings if those structures change (without having to restart the dev server). Is the idea that each wasm binary has this bindings string enclosed and I need to add something to our Vite build process (on the web bundler side of things) to run this export.js file each time the Wasm bundle updates?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm mostly following how this works, but still confused about when and how to use it and how it compares to the #[test] fn generate_ts_types().

Rust requires rebuilding all of the crates before they can be used to run cargo test. So the idea with exposing the bindings from the wasm file is that we only need to run a single build to get the updated bindings. This makes the whole thing much faster.

We can drop the unit tests if we go along with this. I just kept in the code so it's possible to see both possible solutions.

goal would be to have the bindings file be gitignored but autogenerated

I would generally advise against this although feel free to do whatever you think is best. I personally think having the types available in code review and when you clone in the codebase is important.

@@ -0,0 +1,276 @@
// This file has been generated by Specta. DO NOT EDIT.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the result of all the types that Specta generated from Graphite's codebase.

These can be used anywhere in Graphite's frontend.

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be gitignored?

Copy link
Member

Choose a reason for hiding this comment

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

And is there a way to get Specta to generate undefined instead of null in this file? We avoid null by convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't this be gitignored?

I would generally advise against this although feel free to do whatever you think is best. I personally think having the types available in code review and when you clone in the codebase is important.

And is there a way to get Specta to generate undefined instead of null in this file? We avoid null by convention.

I think you can use #[specta(optional)] on each of the fields.

@@ -1,34 +1,15 @@
/// Running this test will generate a `types.ts` file at the root of the repo,
/// containing every type annotated with `specta::Type`
// #[cfg(all(test, feature = "specta-export"))]
#[ignore]

#[test]
fn generate_ts_types() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed this but it's probally not needed with the wasm exporting solution (hence why it's still #[ignore]ed)

@oscartbeaumont
Copy link
Contributor Author

I also removed the pnpm-lock.yaml. It was just from me using pnpm instead of npm.

@TrueDoctor
Copy link
Member

It might be worth moving the type exporting code into the startup of the application if possible when doing a debug build so that you don't need to run cargo test ... to generate the bindings but I don't know enough about Graphite's setup to know where is best.

We can build the specta types in a build.rs eliminating the need to run cargo test which is pretty obscure and would probably lead to issues down the line

@oscartbeaumont
Copy link
Contributor Author

Be aware that will require either:

  • Running nested cargo builds which is not supported due to the lock on the cargo directory, or
  • Splitting all of your types into a different crate than the one with the build.rs so it can import the types as a build dependency.

@TrueDoctor
Copy link
Member

* Splitting all of your types into a different crate than the one with the `build.rs` so it can import the types as a build dependency.

Yeah, was thinking that we could put the build script in the frontend editor-wasm projects which uses the editor crate as its dependency. We thus have access to all types and have them in a separate crate

@oscartbeaumont
Copy link
Contributor Author

That sounds great!

@TrueDoctor
Copy link
Member

We just need to reexport the specta types static global from the editor so we can consume in then the wasm crate but that is easy to do

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.

3 participants