Skip to content

Commit

Permalink
Use improved libprism source to build Prism without dependency on Ruby
Browse files Browse the repository at this point in the history
The main blocker for upstreaming Prism parser to Sorbet is that we currently
rely on Ruby to build Prism, as its required by Prism's `rake template` task.

But by package the files generated by `rake template` in Prim's release,
we will be able to remove the dependency on Ruby while significantly simplify
the build configurations for Prism.
  • Loading branch information
st0012 committed Jan 10, 2025
1 parent 86c3ab6 commit 90c0782
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 82 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ on:
branches: [prism]
pull_request:
branches: [prism]
env:
RUBY_ROOT: '/opt/hostedtoolcache/Ruby/3.3.0/x64'

jobs:
tests:
runs-on: ubuntu-latest
Expand Down
2 changes: 0 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ workspace(name = "com_stripe_ruby_typer")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//third_party:externals.bzl", "register_sorbet_dependencies")
load("//third_party:ruby_root.bzl", "ruby_root")

register_sorbet_dependencies()
ruby_root(name = "ruby_root")

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

Expand Down
5 changes: 2 additions & 3 deletions third_party/externals.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ def register_sorbet_dependencies():

http_archive(
name = "prism",
url = "https://github.com/ruby/prism/archive/v1.2.0.zip",
sha256 = "f398cdb0ed9e5f9b8f7127a8c180ed6a8a611ad4612dcdae8a91ebfc5ee0ed7c",
strip_prefix = "prism-1.2.0",
url = "https://github.com/Shopify/sorbet/releases/download/test-release-artifacts/prism-release-artifacts-new.tar.gz",
sha256 = "58d93c7fd7dc5fa5a3adc4df4ea714a8982e720c10abce5f5e1d561860a3ba7f",
build_file = "@com_stripe_ruby_typer//third_party:prism.BUILD",
)

Expand Down
55 changes: 2 additions & 53 deletions third_party/prism.BUILD
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
load("@ruby_root//:ruby_root.bzl", "RUBY_ROOT") # Get the RUBY_ROOT environment variable

GENERATED_SRCS = [
"src/diagnostic.c",
"src/node.c",
"src/prettyprint.c",
"src/serialize.c",
"src/token_type.c",
]

GENERATED_HDRS = [
"include/prism/ast.h",
"include/prism/diagnostic.h",
]

genrule(
name = "generate_templates",
srcs = [
"templates/template.rb",
"prism.gemspec",
"Gemfile",
"Gemfile.lock",
"config.yml", # Contains the data to populate the ERB templates.
] + \
["templates/{c_file}.erb".format(c_file = c_file) for c_file in GENERATED_SRCS] + \
["templates/{h_file}.erb".format(h_file = h_file) for h_file in GENERATED_HDRS],
outs = GENERATED_HDRS + GENERATED_SRCS,
cmd = """
# set -o xtrace # Uncomment this to debug the execution of this script.
# echo "PWD: $$PWD"
# echo "RULEDIR: $(RULEDIR)"

export PATH="{ruby_root}/bin:$$PATH"

gemfile="$(location Gemfile)"
script="$(location templates/template.rb)"

bundle install --gemfile="$$gemfile"

{template_render_commands}

""".format(
ruby_root = RUBY_ROOT,
template_render_commands = "\n ".join([
"""
bundle exec --gemfile="$$gemfile" ruby "$$script" {f} "$(location {f})"
""".format(f=f) for f in (GENERATED_SRCS + GENERATED_HDRS)
])
),
)

cc_library(
name = "prism",
srcs = glob(["src/**/*.c"], exclude=GENERATED_SRCS) + [":generate_templates"],
hdrs = glob(["include/**/*.h"], exclude=GENERATED_HDRS) + [":generate_templates"],
srcs = glob(["src/**/*.c"]),
hdrs = glob(["include/**/*.h"]),
visibility = ["//visibility:public"],
includes = ["include"],
copts = ["-Wno-implicit-fallthrough"],
Expand Down
20 changes: 0 additions & 20 deletions third_party/ruby_root.bzl

This file was deleted.

2 changes: 1 addition & 1 deletion tools/scripts/verify_prism_regression_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -euo pipefail

echo "Building Sorbet..."
./bazel build //main:sorbet --config=dbg --define RUBY_PATH=$RUBY_ROOT
./bazel build //main:sorbet --config=dbg

echo "Verifying parse trees..."

Expand Down

0 comments on commit 90c0782

Please sign in to comment.