Skip to content

Rewatch as the default build system #7426

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 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ env:
OCAMLRUNPARAM: b

jobs:
test-rewatch-integration:
needs:
- pkg-pr-new
runs-on: ubuntu-latest
env:
RUST_BACKTRACE: "1"

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
- name: Install ReScript package
run: |
COMMIT_SHA="${{ github.event.pull_request.head.sha || github.sha }}"
yarn add "rescript@https://pkg.pr.new/rescript-lang/rescript@${COMMIT_SHA::7}"
shell: bash
working-directory: rewatch/testrepo

- name: Run rewatch integration tests
run: |
make test-rewatch-ci

build-rewatch:
strategy:
fail-fast: false
Expand Down Expand Up @@ -78,6 +104,10 @@ jobs:
run: |
cargo build --manifest-path rewatch/Cargo.toml --target ${{ matrix.rust-target }} --release

- name: Run rewatch unit tests
run: |
cargo test --manifest-path rewatch/Cargo.toml

- name: Copy rewatch binary
run: |
cp rewatch/target/${{ matrix.rust-target }}/release/rewatch${{ runner.os == 'Windows' && '.exe' || '' }} rewatch.exe
Expand Down Expand Up @@ -440,7 +470,7 @@ jobs:
path: lib/ocaml

pkg-pr-new:
needs:
needs:
- build-rewatch
- build-compiler
runs-on: ubuntu-24.04-arm
Expand Down
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ dce:
reanalyze.exe -dce-cmt _build/default/compiler

rewatch:
cargo build --manifest-path rewatch/Cargo.toml
cp rewatch/target/debug/rewatch rewatch
cargo build --manifest-path rewatch/Cargo.toml --release
cp rewatch/target/release/rewatch rewatch
./scripts/copyExes.js --rewatch

ninja/ninja:
Expand Down Expand Up @@ -46,15 +46,25 @@ test-syntax-roundtrip:
test-gentype:
make -C tests/gentype_tests/typescript-react-example clean test

test-all: test test-gentype test-analysis test-tools
test-rewatch:
bash ./rewatch/tests/suite-ci.sh

test-rewatch-ci:
bash ./rewatch/tests/suite-ci.sh node_modules/.bin/rewatch

test-all: test test-gentype test-analysis test-tools test-rewatch

reanalyze:
reanalyze.exe -set-exit-code -all-cmt _build/default/compiler _build/default/tests -exclude-paths compiler/outcome_printer,compiler/ml,compiler/frontend,compiler/ext,compiler/depends,compiler/core,compiler/common,compiler/cmij,compiler/bsb_helper,compiler/bsb

lib:
lib-bsb:
./scripts/buildRuntime.sh
./scripts/prebuilt.js

lib:
./scripts/buildRuntimeRewatch.sh
./scripts/prebuilt.js

artifacts: lib
./scripts/npmPack.js --updateArtifactList

Expand Down
135 changes: 135 additions & 0 deletions cli/rescript-legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env node

// @ts-check

// This script is supposed to be running in project root directory
// It matters since we need read .sourcedirs(location)
// and its content are file/directories with regard to project root

import * as tty from "node:tty";
import * as fs from "node:fs";

import { bsc_exe, rescript_exe } from "./common/bins.js";
import * as bsb from "./common/bsb.js";

const cwd = process.cwd();
process.env.BSB_PROJECT_ROOT = cwd;

if (process.env.FORCE_COLOR === undefined) {
if (tty.isatty(1)) {
process.env.FORCE_COLOR = "1";
process.env.NINJA_ANSI_FORCED = "1";
}
} else {
if (
process.env.FORCE_COLOR === "1" &&
process.env.NINJA_ANSI_FORCED === undefined
) {
process.env.NINJA_ANSI_FORCED = "1";
}
if (process.argv.includes("-verbose")) {
console.log(`FORCE_COLOR: "${process.env.FORCE_COLOR}"`);
}
}

const helpMessage = `Usage: rescript <options> <subcommand>

\`rescript\` is equivalent to \`rescript build\`

Options:
-v, -version display version number
-h, -help display help

Subcommands:
build
clean
format
dump
help

Run \`rescript <subcommand> -h\` for subcommand help. Examples:
rescript build -h
rescript format -h`;

function onUncaughtException(err) {
console.error("Uncaught Exception", err);
bsb.releaseBuild();
process.exit(1);
}

function exitProcess() {
bsb.releaseBuild();
process.exit(0);
}

process.on("uncaughtException", onUncaughtException);

// OS signal handlers
// Ctrl+C
process.on("SIGINT", exitProcess);
// kill pid
try {
process.on("SIGUSR1", exitProcess);
process.on("SIGUSR2", exitProcess);
process.on("SIGTERM", exitProcess);
process.on("SIGHUP", exitProcess);
} catch (_e) {
// Deno might throw an error here, see https://github.com/denoland/deno/issues/9995
// TypeError: Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).
}

const args = process.argv.slice(2);
const argPatterns = {
help: ["help", "-h", "-help", "--help"],
version: ["version", "-v", "-version", "--version"],
};

const helpArgIndex = args.findIndex((arg) => argPatterns.help.includes(arg));
const firstPositionalArgIndex = args.findIndex((arg) => !arg.startsWith("-"));

if (
helpArgIndex !== -1 &&
(firstPositionalArgIndex === -1 || helpArgIndex <= firstPositionalArgIndex)
) {
console.log(helpMessage);
} else if (argPatterns.version.includes(args[0])) {
const packageSpec = JSON.parse(
fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8")
);

console.log(packageSpec.version);
} else if (firstPositionalArgIndex !== -1) {
const subcmd = args[firstPositionalArgIndex];
const subcmdArgs = args.slice(firstPositionalArgIndex + 1);

switch (subcmd) {
case "info": {
bsb.info(subcmdArgs);
break;
}
case "clean": {
bsb.clean(subcmdArgs);
break;
}
case "build": {
bsb.build(subcmdArgs);
break;
}
case "format": {
const mod = await import("./rescript/format.js");
await mod.main(subcmdArgs, rescript_exe, bsc_exe);
break;
}
case "dump": {
const mod = await import("./rescript/dump.js");
mod.main(subcmdArgs, rescript_exe, bsc_exe);
break;
}
default: {
console.error(`Error: Unknown command "${subcmd}".\n${helpMessage}`);
process.exit(2);
}
}
} else {
bsb.build(args);
}
134 changes: 5 additions & 129 deletions cli/rescript.js
Original file line number Diff line number Diff line change
@@ -1,135 +1,11 @@
#!/usr/bin/env node

// @ts-check

// This script is supposed to be running in project root directory
// It matters since we need read .sourcedirs(location)
// and its content are file/directories with regard to project root

import * as tty from "node:tty";
import * as fs from "node:fs";

import { bsc_exe, rescript_exe } from "./common/bins.js";
import * as bsb from "./common/bsb.js";

const cwd = process.cwd();
process.env.BSB_PROJECT_ROOT = cwd;

if (process.env.FORCE_COLOR === undefined) {
if (tty.isatty(1)) {
process.env.FORCE_COLOR = "1";
process.env.NINJA_ANSI_FORCED = "1";
}
} else {
if (
process.env.FORCE_COLOR === "1" &&
process.env.NINJA_ANSI_FORCED === undefined
) {
process.env.NINJA_ANSI_FORCED = "1";
}
if (process.argv.includes("-verbose")) {
console.log(`FORCE_COLOR: "${process.env.FORCE_COLOR}"`);
}
}

const helpMessage = `Usage: rescript <options> <subcommand>

\`rescript\` is equivalent to \`rescript build\`

Options:
-v, -version display version number
-h, -help display help

Subcommands:
build
clean
format
dump
help

Run \`rescript <subcommand> -h\` for subcommand help. Examples:
rescript build -h
rescript format -h`;

function onUncaughtException(err) {
console.error("Uncaught Exception", err);
bsb.releaseBuild();
process.exit(1);
}

function exitProcess() {
bsb.releaseBuild();
process.exit(0);
}

process.on("uncaughtException", onUncaughtException);

// OS signal handlers
// Ctrl+C
process.on("SIGINT", exitProcess);
// kill pid
try {
process.on("SIGUSR1", exitProcess);
process.on("SIGUSR2", exitProcess);
process.on("SIGTERM", exitProcess);
process.on("SIGHUP", exitProcess);
} catch (_e) {
// Deno might throw an error here, see https://github.com/denoland/deno/issues/9995
// TypeError: Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).
}
import * as child_process from "node:child_process";
import { rewatch_exe, bsc_exe } from "./common/bins.js";

const args = process.argv.slice(2);
const argPatterns = {
help: ["help", "-h", "-help", "--help"],
version: ["version", "-v", "-version", "--version"],
};

const helpArgIndex = args.findIndex(arg => argPatterns.help.includes(arg));
const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-"));

if (
helpArgIndex !== -1 &&
(firstPositionalArgIndex === -1 || helpArgIndex <= firstPositionalArgIndex)
) {
console.log(helpMessage);
} else if (argPatterns.version.includes(args[0])) {
const packageSpec = JSON.parse(
fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8")
);

console.log(packageSpec.version);
} else if (firstPositionalArgIndex !== -1) {
const subcmd = args[firstPositionalArgIndex];
const subcmdArgs = args.slice(firstPositionalArgIndex + 1);

switch (subcmd) {
case "info": {
bsb.info(subcmdArgs);
break;
}
case "clean": {
bsb.clean(subcmdArgs);
break;
}
case "build": {
bsb.build(subcmdArgs);
break;
}
case "format": {
const mod = await import("./rescript/format.js");
await mod.main(subcmdArgs, rescript_exe, bsc_exe);
break;
}
case "dump": {
const mod = await import("./rescript/dump.js");
mod.main(subcmdArgs, rescript_exe, bsc_exe);
break;
}
default: {
console.error(`Error: Unknown command "${subcmd}".\n${helpMessage}`);
process.exit(2);
}
}
} else {
bsb.build(args);
}
child_process.spawnSync(rewatch_exe, [...args, "--bsc-path", bsc_exe], {
stdio: "inherit",
});
6 changes: 4 additions & 2 deletions cli/rewatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// @ts-check

import * as child_process from "node:child_process";
import { rewatch_exe } from "./common/bins.js";
import { rewatch_exe, bsc_exe } from "./common/bins.js";

const args = process.argv.slice(2);

child_process.spawnSync(rewatch_exe, args, { stdio: "inherit" });
child_process.spawnSync(rewatch_exe, [...args, "--bsc-path", bsc_exe], {
stdio: "inherit",
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"bsc": "cli/bsc.js",
"bstracing": "cli/bstracing.js",
"rescript": "cli/rescript.js",
"rescript-legacy": "cli/rescript-legacy.js",
"rescript-tools": "cli/rescript-tools.js",
"rewatch": "cli/rewatch.js"
},
Expand Down
4 changes: 4 additions & 0 deletions rewatch/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[alias]
build-m1-release = "RUSTFLAGS=\"-C target-cpu=apple-m1\" cargo build --release"
build-docs = "cargo doc --no-deps --document-private-items --target-dir ./docs"

3 changes: 3 additions & 0 deletions rewatch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
.DS_Store
/docs
Loading
Loading