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

Build and publish the lexer to npm #20778

Merged
merged 3 commits into from
Jul 28, 2023
Merged

Conversation

arusahni
Copy link
Contributor

@arusahni arusahni commented Jul 25, 2023

Motivation

  • This PR adds a known-desirable feature: Fixes MaterializeInc/database-issues#6100

Following #20345, let's publish the lexer to npm! This PR contains the three phases of the change, each in its own commit with details in the message. To summarize, they:

  1. Enable WebAssembly (WASM) compilation of mz-sql-lexer. Perform the necessary updates to be able to compile the lexer to WASM targets as well as make it ergonomic for JS/TS callers. This includes a special allocator that reduces the bundle size.
  2. Enable WASM cross-compilation. This updates ci-builder and xcompile to support WASM targets, as well as the wasm-pack tool. It also includes a wrapper script to codify invocation parameters.
  3. Compile the Lexer to WASM and publish it to npm. This brings it all together by using a deploy script to compile tracked crates to WebAssembly and publish them to npm. This requires a npm access token with write access to the @materializinc namespace` be added to the Buildkite environment. A sample npm listing is here (please ignore the incorrect package name held over from testing).

CI updates

Two additional CI jobs are added to the PR and deploy pipelines:

  1. We validate that all WASM targets can be built with each PR.
  2. We build and publish all WASM packages to npm with each merge to main. For dev builds, this uses pre-release versioning with a build identifier derived from the Buildkite build ID.

cargo-deny exemption

The custom allocator (lol_alloc) depends on spin 0.9, while ring (a dependency of aws-config) depends on 0.5. This is fixed on ring's main branch, but it has not yet made it to a stable release. The last stable release for ring is 2+ years old, and the last unstable release is 8 months old. The ring dep may disappear altogether in a future version of aws-config. Given the alternate version of this crate is only used when targeting WASM, I believe this exemption is fine to carve out.

Tips for reviewer

These are grouped by commit, and each commit message includes additional information.

Unfortunately, I had to disable TypeScript definition autogeneration for the lex function as js-sys does not provide adequate support for container types (i.e., it would return Array instead of Token[]).

Checklist

  • This PR has adequate test coverage / QA involvement has been duly considered.
  • This PR has an associated up-to-date design doc, is a design doc (template), or is sufficiently small to not require a design.
  • If this PR evolves an existing $T ⇔ Proto$T mapping (possibly in a backwards-incompatible way), then it is tagged with a T-proto label.
  • If this PR will require changes to cloud orchestration, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label (example).
  • This PR includes the following user-facing behavior changes:

@arusahni arusahni force-pushed the main branch 3 times, most recently from 2896925 to c6c1464 Compare July 25, 2023 19:18
@arusahni arusahni changed the title Build and publish mz-sql-lexer to npm Build and publish the lexer to npm Jul 25, 2023
@arusahni arusahni marked this pull request as ready for review July 25, 2023 20:01
@arusahni arusahni requested review from a team as code owners July 25, 2023 20:01
Copy link
Contributor

@maddyblue maddyblue left a comment

Choose a reason for hiding this comment

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

LGTM, although I'm not super familiar with some of these build scripts.

@arusahni arusahni requested a review from benesch July 25, 2023 20:22
Copy link
Member

@benesch benesch left a comment

Choose a reason for hiding this comment

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

Structure looks great! Just a few nits on the specifics within!

bin/wasm-build Outdated Show resolved Hide resolved
ci/builder/Dockerfile Outdated Show resolved Hide resolved
ci/deploy/pipeline.yml Outdated Show resolved Hide resolved
src/sql-lexer/src/lexer.rs Outdated Show resolved Hide resolved
bin/ci-builder Outdated Show resolved Hide resolved
ci/deploy/npm.py Outdated Show resolved Hide resolved
ci/deploy/npm.py Outdated Show resolved Hide resolved
ci/deploy/npm.py Outdated Show resolved Hide resolved
ci/deploy/npm.py Outdated Show resolved Hide resolved
ci/deploy/npm.py Outdated Show resolved Hide resolved
@arusahni
Copy link
Contributor Author

I've updated the PR to capture the above changes.

@arusahni
Copy link
Contributor Author

@benesch between approval and merge we'll need to set the NPM_TOKEN envvar in CI.

@benesch
Copy link
Member

benesch commented Jul 28, 2023

@benesch between approval and merge we'll need to set the NPM_TOKEN envvar in CI.

Done: https://github.com/MaterializeInc/i2/pull/1016

bin/wasm-build Outdated Show resolved Hide resolved
src/sql-lexer/src/lexer.rs Show resolved Hide resolved
ci/deploy/pipeline.yml Outdated Show resolved Hide resolved
@benesch
Copy link
Member

benesch commented Jul 28, 2023

LGTM, modulo the debugging --scope I commented on above!

In an effort to ensure we're not maintaing two concurrent Lexer
implementations, make the existing lexer available to more platforms. By
building a WASM module, we can re-use it in JavaScript contexts.

`wasm-pack` does most of the heavy lifting, but we need to ensure we're
emitting types that can be serialized across the WASM interface, hence
the flattening of the token array.
Certain crates, such as 'sql-lexer', can be compiled to WASM using the
'wasm-pack' tool. This commit:

  1. Adds the relevant WASM libraries and tooling to the ci-builder.
  2. Exposes additional CLI surface area to allow xcompile to run in the
     ci-builder image.
Add CI hooks to build WASM packages alongside the crates in CI, and
tooling to publish built artifacts to npm with the appropriate tags and
version information.

This will publish packages for _all_ builds on the deploy pipeline.
Stable/official builds will not have a `-dev` suffix, and will receive
the `latest` dist tag. Unstable builds will have a `dev` suffix that
includes a build number, and will receive the `dev` dist tag.
@arusahni arusahni enabled auto-merge July 28, 2023 14:05
@arusahni arusahni merged commit b78f03d into MaterializeInc:main Jul 28, 2023
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