refactor(cli): move crates/oxc_cli to apps/oxlint (#3413)

We need to split the cli crate up to reduce dependencies,
the current cli crate is pulling in `oxc_prettier`, which is redundant
for the linter.
This commit is contained in:
Boshen 2024-05-26 05:00:30 +00:00
parent 4149d22aa3
commit 21505e885c
52 changed files with 31 additions and 332 deletions

2
.github/labeler.yml vendored
View file

@ -4,7 +4,7 @@ A-ast:
A-cli: A-cli:
- changed-files: - changed-files:
- any-glob-to-any-file: ['crates/oxc_cli/**'] - any-glob-to-any-file: ['apps/oxlint/**']
A-prettier: A-prettier:
- changed-files: - changed-files:

View file

@ -27,7 +27,7 @@ jobs:
- name: Run - name: Run
run: | run: |
export CMD="cargo bloat --profile release-debug -p oxc_cli --bin oxlint --features allocator" export CMD="cargo bloat --profile release-debug -p oxlint --features allocator"
# Get largest functions # Get largest functions
export BLOAT_FUNC_CMD="${CMD} -n 10" export BLOAT_FUNC_CMD="${CMD} -n 10"

View file

@ -1,177 +0,0 @@
name: Release CLI
on:
push:
branches:
- main
paths:
- npm/oxc/package.json # Please only commit this file, so we don't need to wait for test CI to pass.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
check:
name: Check version
runs-on: ubuntu-latest
outputs:
version: ${{ env.version }}
version_changed: ${{ steps.version.outputs.changed }}
steps:
- uses: taiki-e/checkout-action@v1
- name: Check version changes
uses: EndBug/version-check@v2
id: version
with:
static-checking: localIsNew
file-url: https://unpkg.com/oxidation-compiler@latest/package.json
file-name: npm/oxc/package.json
- name: Set version name
if: steps.version.outputs.changed == 'true'
run: |
echo "Version change found! New version: ${{ steps.version.outputs.version }} (${{ steps.version.outputs.version_type }})"
echo "version=${{ steps.version.outputs.version }}" >> $GITHUB_ENV
build:
needs: check
if: needs.check.outputs.version_changed == 'true'
env:
version: ${{ needs.check.outputs.version }}
outputs:
version: ${{ env.version }}
strategy:
matrix:
include:
- os: windows-latest
target: x86_64-pc-windows-msvc
code-target: win32-x64
- os: windows-latest
target: aarch64-pc-windows-msvc
code-target: win32-arm64
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
code-target: linux-x64
- os: ubuntu-20.04
target: aarch64-unknown-linux-gnu
code-target: linux-arm64
- os: macos-14
target: x86_64-apple-darwin
code-target: darwin-x64
- os: macos-14
target: aarch64-apple-darwin
code-target: darwin-arm64
name: Package ${{ matrix.code-target }}
runs-on: ${{ matrix.os }}
steps:
- uses: taiki-e/checkout-action@v1
- name: Install Rust toolchain
run: rustup target add ${{ matrix.target }}
- name: Install arm64 toolchain
if: matrix.code-target == 'linux-arm64'
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu
- name: Cache
uses: Swatinem/rust-cache@v2
with:
shared-key: release-${{ matrix.target }}
- name: Build Binary
# strip debug symbols from std, see https://github.com/johnthagen/min-sized-rust#remove-panic-string-formatting-with-panic_immediate_abort
run: cargo build --release --target ${{ matrix.target }} -p oxc_cli --features allocator
env:
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
# The binary is zipped to fix permission loss https://github.com/actions/upload-artifact#permission-loss
- name: Archive Binary
if: runner.os == 'Windows'
shell: bash
run: |
BIN_NAME=oxc-${{ matrix.code-target }}
mv target/${{ matrix.target }}/release/oxc_cli.exe $BIN_NAME.exe
7z a $BIN_NAME.zip $BIN_NAME.exe
# The binary is zipped to fix permission loss https://github.com/actions/upload-artifact#permission-loss
- name: Archive Binary
if: runner.os != 'Windows'
run: |
BIN_NAME=oxc-${{ matrix.code-target }}
mv target/${{ matrix.target }}/release/oxc_cli $BIN_NAME
tar czf $BIN_NAME.tar.gz $BIN_NAME
- name: Upload Binary
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: binaries
path: |
*.zip
*.tar.gz
publish:
name: Publish
runs-on: ubuntu-latest
permissions:
contents: write # for softprops/action-gh-release@v1
id-token: write # for `npm publish --provenance`
needs:
- build
steps:
- uses: taiki-e/checkout-action@v1
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: 'https://registry.npmjs.org'
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
name: binaries
- name: Unzip
uses: montudor/action-zip@v1
with:
args: unzip -qq *.zip -d .
- name: Untar
run: ls *.gz | xargs -i tar xf {}
- name: Generate npm packages
run: |
node npm/oxc/scripts/generate-packages.mjs
cat npm/oxc/package.json
for package in npm/oxc*; do cat $package/package.json ; echo ; done
- name: Publish npm packages as latest
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
shell: bash
# NOTE: The trailing slash on $package/ changes it to publishing the directory
run: |
for package in npm/oxc*
do
npm publish $package/ --tag latest --provenance --access public
echo '----'
done
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
name: CLI v${{ needs.build.outputs.version }}
draft: true
files: oxc-*
fail_on_unmatched_files: true

View file

@ -92,7 +92,7 @@ jobs:
run: rustup target add ${{ matrix.target }} run: rustup target add ${{ matrix.target }}
- name: Build with cross - name: Build with cross
run: cross build -p oxc_cli --bin oxlint --release --target=${{ matrix.target }} --features allocator run: cross build --release --target=${{ matrix.target }} --bin oxlint --features allocator
# The binary is zipped to fix permission loss https://github.com/actions/upload-artifact#permission-loss # The binary is zipped to fix permission loss https://github.com/actions/upload-artifact#permission-loss
- name: Archive Binary - name: Archive Binary

41
Cargo.lock generated
View file

@ -1313,27 +1313,6 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "oxc_cli"
version = "0.0.0"
dependencies = [
"bpaf",
"glob",
"ignore",
"jemallocator",
"miette",
"mimalloc",
"oxc_allocator",
"oxc_diagnostics",
"oxc_linter",
"oxc_parser",
"oxc_prettier",
"oxc_span",
"rayon",
"tempfile",
"tracing-subscriber",
]
[[package]] [[package]]
name = "oxc_codegen" name = "oxc_codegen"
version = "0.13.1" version = "0.13.1"
@ -1751,6 +1730,24 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "oxlint"
version = "0.0.0"
dependencies = [
"bpaf",
"glob",
"ignore",
"jemallocator",
"miette",
"mimalloc",
"oxc_diagnostics",
"oxc_linter",
"oxc_span",
"rayon",
"tempfile",
"tracing-subscriber",
]
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.2" version = "0.12.2"
@ -2961,8 +2958,8 @@ dependencies = [
"ckb_schemars", "ckb_schemars",
"handlebars", "handlebars",
"insta", "insta",
"oxc_cli",
"oxc_linter", "oxc_linter",
"oxlint",
"pico-args", "pico-args",
"serde", "serde",
"serde_json", "serde_json",

View file

@ -1,6 +1,6 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = ["crates/*", "napi/*", "tasks/*", "wasm/*"] members = ["apps/*", "crates/*", "napi/*", "tasks/*", "wasm/*"]
exclude = ["tasks/lint_rules"] exclude = ["tasks/lint_rules"]
[workspace.package] [workspace.package]

View file

@ -1,5 +1,5 @@
[package] [package]
name = "oxc_cli" name = "oxlint"
version = "0.0.0" version = "0.0.0"
publish = false publish = false
authors.workspace = true authors.workspace = true
@ -18,19 +18,9 @@ workspace = true
[lib] [lib]
doctest = false doctest = false
[[bin]]
name = "oxc_cli"
path = "src/main.rs"
test = false
[[bin]] [[bin]]
name = "oxlint" name = "oxlint"
path = "src/lint/main.rs" path = "src/main.rs"
test = false
[[bin]]
name = "oxformat"
path = "src/format/main.rs"
test = false test = false
[target.'cfg(not(target_env = "msvc"))'.dependencies] [target.'cfg(not(target_env = "msvc"))'.dependencies]
@ -40,11 +30,8 @@ jemallocator = { workspace = true, optional = true }
mimalloc = { workspace = true, optional = true } mimalloc = { workspace = true, optional = true }
[dependencies] [dependencies]
oxc_allocator = { workspace = true }
oxc_diagnostics = { workspace = true } oxc_diagnostics = { workspace = true }
oxc_linter = { workspace = true } oxc_linter = { workspace = true }
oxc_parser = { workspace = true }
oxc_prettier = { workspace = true }
oxc_span = { workspace = true } oxc_span = { workspace = true }
glob = { workspace = true } glob = { workspace = true }

View file

@ -1,5 +1,4 @@
mod command; mod command;
mod format;
mod lint; mod lint;
mod result; mod result;
mod runner; mod runner;
@ -7,7 +6,6 @@ mod walk;
pub use crate::{ pub use crate::{
command::*, command::*,
format::FormatRunner,
lint::LintRunner, lint::LintRunner,
result::{CliRunResult, LintResult}, result::{CliRunResult, LintResult},
runner::Runner, runner::Runner,

View file

@ -10,13 +10,13 @@ static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[global_allocator] #[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use oxc_cli::{CliRunResult, LintRunner, Runner}; use oxlint::{CliRunResult, LintRunner, Runner};
fn main() -> CliRunResult { fn main() -> CliRunResult {
init_tracing(); init_tracing();
init_miette(); init_miette();
let command = oxc_cli::lint_command().run(); let command = oxlint::lint_command().run();
command.handle_threads(); command.handle_threads();
LintRunner::new(command.lint_options).run() LintRunner::new(command.lint_options).run()
} }

View file

@ -1,26 +0,0 @@
#![cfg(not(miri))] // Miri does not support custom allocators
#[cfg(not(debug_assertions))]
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[cfg(not(debug_assertions))]
#[cfg(target_os = "windows")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use oxc_cli::{CliRunResult, FormatRunner, Runner};
fn main() -> CliRunResult {
init_miette();
let command = oxc_cli::format_command().fallback_to_usage().run();
command.handle_threads();
FormatRunner::new(command.format_options).run()
}
// Initialize the data which relies on `is_atty` system calls so they don't block subsequent threads.
fn init_miette() {
miette::set_hook(Box::new(|_| Box::new(miette::MietteHandlerOpts::new().build()))).unwrap();
}

View file

@ -1,58 +0,0 @@
use std::path::Path;
use oxc_allocator::Allocator;
use oxc_parser::Parser;
use oxc_prettier::{Prettier, PrettierOptions};
use oxc_span::SourceType;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use crate::{
command::FormatOptions,
result::{CliRunResult, FormatResult},
walk::Walk,
Runner,
};
pub struct FormatRunner {
options: FormatOptions,
}
impl Runner for FormatRunner {
type Options = FormatOptions;
fn new(options: Self::Options) -> Self {
Self { options }
}
fn run(self) -> CliRunResult {
let FormatOptions { paths, ignore_options, .. } = &self.options;
if paths.is_empty() {
return CliRunResult::InvalidOptions { message: "No paths are provided.".to_string() };
}
let now = std::time::Instant::now();
let paths = Walk::new(paths, ignore_options).paths();
paths.par_iter().for_each(|path| {
Self::format(path);
});
CliRunResult::FormatResult(FormatResult {
duration: now.elapsed(),
number_of_files: paths.len(),
})
}
}
impl FormatRunner {
fn format(path: &Path) {
let source_text = std::fs::read_to_string(path).unwrap();
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
let ret = Parser::new(&allocator, &source_text, source_type).preserve_parens(false).parse();
let _ = Prettier::new(&allocator, &source_text, &ret.trivias, PrettierOptions::default())
.build(&ret.program);
}
}

View file

@ -1,22 +0,0 @@
#![cfg(not(miri))] // Miri does not support custom allocators
#[cfg(not(debug_assertions))]
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[cfg(not(debug_assertions))]
#[cfg(target_os = "windows")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use oxc_cli::{CliCommand, CliRunResult, FormatRunner, LintRunner, Runner};
fn main() -> CliRunResult {
let options = oxc_cli::cli_command().fallback_to_usage().run();
options.handle_threads();
match options {
CliCommand::Lint(options) => LintRunner::new(options).run(),
CliCommand::Format(options) => FormatRunner::new(options).run(),
}
}

View file

@ -93,7 +93,7 @@ test-transform *args='':
# Build oxlint in release build # Build oxlint in release build
oxlint: oxlint:
cargo build --release -p oxc_cli --bin oxlint --features allocator cargo build --release --bin oxlint --features allocator
# Generate the JavaScript global variables. See `tasks/javascript_globals` # Generate the JavaScript global variables. See `tasks/javascript_globals`
javascript-globals: javascript-globals:

View file

@ -17,7 +17,7 @@ doctest = false
[dependencies] [dependencies]
oxc_linter = { workspace = true } oxc_linter = { workspace = true }
oxc_cli = { path = "../../crates/oxc_cli" } oxlint = { path = "../../apps/oxlint" }
bpaf = { workspace = true, features = ["docgen"] } bpaf = { workspace = true, features = ["docgen"] }
pico-args = { workspace = true } pico-args = { workspace = true }

View file

@ -1,5 +1,5 @@
use bpaf::Parser; use bpaf::Parser;
use oxc_cli::lint_options; use oxlint::lint_options;
#[test] #[test]
fn test_cli() { fn test_cli() {
@ -11,7 +11,7 @@ fn test_cli() {
#[test] #[test]
fn test_cli_terminal() { fn test_cli_terminal() {
let snapshot = oxc_cli::lint_command().run_inner(&["--help"]).unwrap_err().unwrap_stdout(); let snapshot = oxlint::lint_command().run_inner(&["--help"]).unwrap_err().unwrap_stdout();
insta::with_settings!({ prepend_module_to_snapshot => false }, { insta::with_settings!({ prepend_module_to_snapshot => false }, {
insta::assert_snapshot!(snapshot); insta::assert_snapshot!(snapshot);
}); });