Skip to content

Commit e9336fa

Browse files
authored
Add R language support (#2206)
Closes #42
1 parent 97a5a15 commit e9336fa

16 files changed

Lines changed: 705 additions & 181 deletions

File tree

‎.config/nextest.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ threads-required = "num-test-threads"
1818
platform = 'cfg(windows)'
1919
filter = "test(perl::)"
2020
threads-required = "num-test-threads"
21+
22+
[[profile.ci-language.overrides]]
23+
# R package installs can share user-level cache and lock state.
24+
filter = "test(/^r::/)"
25+
threads-required = "num-test-threads"

‎.github/workflows/ci.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ env:
2929
DOTNET_VERSION: "10.0"
3030
DENO_VERSION: "2"
3131
DART_VERSION: "3.10.4"
32+
R_VERSION: "4.6.0"
3233

3334
# Cargo env vars
3435
CARGO_INCREMENTAL: 0
@@ -623,6 +624,13 @@ jobs:
623624
with:
624625
dotnet-version: ${{ env.DOTNET_VERSION }}
625626

627+
- name: "Install R"
628+
if: ${{ contains(format(' {0} ', matrix.languages), ' r ') }}
629+
uses: r-lib/actions/setup-r@a51a8012b0aab7c32ef9d19bf54da93f3254335e # v2
630+
with:
631+
r-version: ${{ env.R_VERSION }}
632+
use-public-rspm: true
633+
626634
- name: "Check Perl and CPAN"
627635
if: ${{ contains(format(' {0} ', matrix.languages), ' perl ') }}
628636
run: |

‎README.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ while also providing some additional long-requested features.
2626

2727
> [!NOTE]
2828
> Although prek is pretty new, it’s already powering real‑world projects like [CPython](https://github.com/python/cpython), [Apache Airflow](https://github.com/apache/airflow), [FastAPI](https://github.com/fastapi/fastapi), and more projects are picking it up—see [Who is using prek?](#who-is-using-prek). If you’re looking for an alternative to `pre-commit`, please give it a try—we’d love your feedback!
29-
>
30-
> Please note that some languages are not yet supported for full drop‑in parity with `pre-commit`. See [Language Support](https://prek.j178.dev/languages/) for current status.
3129
3230
<!-- --8<-- [start:features] -->
3331

‎crates/prek-consts/src/env_vars.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ impl EnvVars {
8989
pub const PERL_MB_OPT: &'static str = "PERL_MB_OPT";
9090
pub const PERL_MM_OPT: &'static str = "PERL_MM_OPT";
9191

92+
// R related
93+
pub const R_HOME: &'static str = "R_HOME";
94+
pub const R_PROFILE_USER: &'static str = "R_PROFILE_USER";
95+
pub const RENV_PROJECT: &'static str = "RENV_PROJECT";
96+
9297
// Conda related
9398
pub const CONDA_PREFIX: &'static str = "CONDA_PREFIX";
9499
pub const PRE_COMMIT_USE_MAMBA: &'static str = "PRE_COMMIT_USE_MAMBA";

‎crates/prek/src/cli/run/run.rs‎

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ fn select_hooks_to_install<'paths>(
429429

430430
let mut candidates = hooks
431431
.into_iter()
432-
.filter(|hook| hook.language.supported())
433432
.map(|hook| {
434433
let matches = hook.always_run;
435434
(hook, matches)
@@ -476,7 +475,7 @@ fn select_hooks_to_install<'paths>(
476475
};
477476

478477
let project_input = ProjectHookInput::new(input, project, None, None)?;
479-
for hook in hooks.into_iter().filter(|hook| hook.language.supported()) {
478+
for hook in hooks {
480479
if hook.always_run || project_input.matches_hook(&hook, tag_cache) {
481480
hooks_to_install.push(hook);
482481
}
@@ -675,7 +674,6 @@ struct HookRunSession<'a> {
675674
verbose: bool,
676675
success: bool,
677676
file_modified: bool,
678-
has_unimplemented: bool,
679677
}
680678

681679
impl<'a> HookRunSession<'a> {
@@ -700,7 +698,6 @@ impl<'a> HookRunSession<'a> {
700698
verbose,
701699
success: true,
702700
file_modified: false,
703-
has_unimplemented: false,
704701
}
705702
}
706703

@@ -931,8 +928,6 @@ impl<'a> HookRunSession<'a> {
931928
.suspend(|| self.render_priority_group(&results, modified_files, hook_prefix))?;
932929

933930
for RunResult { status, .. } in &results {
934-
self.has_unimplemented |= status.is_unimplemented();
935-
936931
let ok = if modified_files {
937932
false
938933
} else {
@@ -998,7 +993,7 @@ impl<'a> HookRunSession<'a> {
998993
self.status_printer
999994
.write(&result.hook.name, &prefix, status)?;
1000995

1001-
if matches!(status, RunStatus::NoFiles | RunStatus::Unimplemented) {
996+
if matches!(status, RunStatus::NoFiles) {
1002997
continue;
1003998
}
1004999

@@ -1079,13 +1074,6 @@ impl<'a> HookRunSession<'a> {
10791074
) -> Result<ExitStatus> {
10801075
self.reporter.on_complete();
10811076

1082-
if self.has_unimplemented {
1083-
warn_user!(
1084-
"Some hooks were skipped because their languages are unimplemented.\nWe're working hard to support more languages. Check out current support status at {}.",
1085-
"https://prek.j178.dev/languages/".cyan().underline()
1086-
);
1087-
}
1088-
10891077
if !self.success && show_diff_on_failure && self.file_modified {
10901078
if EnvVars::is_under_ci() {
10911079
writeln!(
@@ -1300,23 +1288,15 @@ enum RunStatus {
13001288
Failed,
13011289
DryRun,
13021290
NoFiles,
1303-
Unimplemented,
13041291
}
13051292

13061293
impl RunStatus {
13071294
fn as_bool(self) -> bool {
1308-
matches!(
1309-
self,
1310-
Self::Success | Self::NoFiles | Self::DryRun | Self::Unimplemented
1311-
)
1312-
}
1313-
1314-
fn is_unimplemented(self) -> bool {
1315-
matches!(self, Self::Unimplemented)
1295+
matches!(self, Self::Success | Self::NoFiles | Self::DryRun)
13161296
}
13171297

13181298
fn is_skipped(self) -> bool {
1319-
matches!(self, Self::DryRun | Self::NoFiles | Self::Unimplemented)
1299+
matches!(self, Self::DryRun | Self::NoFiles)
13201300
}
13211301
}
13221302

@@ -1331,7 +1311,6 @@ impl StatusPrinter {
13311311
const SKIPPED: &'static str = "Skipped";
13321312
const DRY_RUN: &'static str = "Dry Run";
13331313
const NO_FILES: &'static str = "(no files to check)";
1334-
const UNIMPLEMENTED: &'static str = "(unimplemented yet)";
13351314

13361315
fn for_hooks<T>(hooks: &[T], printer: Printer) -> Self
13371316
where
@@ -1370,11 +1349,6 @@ impl StatusPrinter {
13701349
Self::SKIPPED.black().on_cyan().to_string(),
13711350
Self::SKIPPED.width(),
13721351
),
1373-
RunStatus::Unimplemented => (
1374-
Self::UNIMPLEMENTED,
1375-
Self::SKIPPED.black().on_yellow().to_string(),
1376-
Self::SKIPPED.width(),
1377-
),
13781352
RunStatus::DryRun => (
13791353
"",
13801354
Self::DRY_RUN.on_yellow().to_string(),
@@ -1453,10 +1427,6 @@ async fn run_hook(
14531427
if !matched && !hook.always_run {
14541428
return Ok(RunResult::from_status(hook, RunStatus::NoFiles));
14551429
}
1456-
if !hook.language.supported() {
1457-
return Ok(RunResult::from_status(hook, RunStatus::Unimplemented));
1458-
}
1459-
14601430
let start = std::time::Instant::now();
14611431
input.shuffle();
14621432

‎crates/prek/src/languages/mod.rs‎

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod node;
3333
mod perl;
3434
mod pygrep;
3535
mod python;
36+
mod r;
3637
mod ruby;
3738
mod rust;
3839
mod script;
@@ -57,12 +58,12 @@ static NODE: node::Node = node::Node;
5758
static PERL: perl::Perl = perl::Perl;
5859
static PYGREP: pygrep::Pygrep = pygrep::Pygrep;
5960
static PYTHON: python::Python = python::Python;
61+
static R: r::R = r::R;
6062
static RUBY: ruby::Ruby = ruby::Ruby;
6163
static RUST: rust::Rust = rust::Rust;
6264
static SCRIPT: script::Script = script::Script;
6365
static SWIFT: swift::Swift = swift::Swift;
6466
static SYSTEM: system::System = system::System;
65-
static UNIMPLEMENTED: Unimplemented = Unimplemented;
6667

6768
trait LanguageImpl {
6869
async fn install(
@@ -83,43 +84,12 @@ trait LanguageImpl {
8384
) -> Result<(i32, Vec<u8>)>;
8485
}
8586

86-
#[derive(thiserror::Error, Debug)]
87-
#[error("Language `{0}` is not implemented yet")]
88-
struct UnimplementedError(String);
89-
90-
struct Unimplemented;
91-
9287
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9388
pub(crate) enum ShellSupport {
9489
Supported,
9590
Unsupported(&'static str),
9691
}
9792

98-
impl LanguageImpl for Unimplemented {
99-
async fn install(
100-
&self,
101-
hook: Arc<Hook>,
102-
_store: &Store,
103-
_reporter: &HookInstallReporter,
104-
) -> Result<InstalledHook> {
105-
Ok(InstalledHook::NoNeedInstall(hook))
106-
}
107-
108-
async fn check_health(&self, _info: &InstallInfo) -> Result<()> {
109-
Ok(())
110-
}
111-
112-
async fn run(
113-
&self,
114-
hook: &InstalledHook,
115-
_filenames: &[&Path],
116-
_store: &Store,
117-
_reporter: &HookRunReporter,
118-
) -> Result<(i32, Vec<u8>)> {
119-
anyhow::bail!(UnimplementedError(format!("{}", hook.language)))
120-
}
121-
}
122-
12393
// `pre-commit` language support:
12494
// bun: install requested version, support env, support additional deps
12595
// conda: only system version, support env, support additional deps
@@ -144,34 +114,6 @@ impl LanguageImpl for Unimplemented {
144114
// system: only system version, no env, no additional deps
145115

146116
impl Language {
147-
pub(crate) fn supported(self) -> bool {
148-
match self {
149-
Self::Bun
150-
| Self::Conda
151-
| Self::Coursier
152-
| Self::Dart
153-
| Self::Deno
154-
| Self::Docker
155-
| Self::DockerImage
156-
| Self::Dotnet
157-
| Self::Fail
158-
| Self::Golang
159-
| Self::Haskell
160-
| Self::Julia
161-
| Self::Lua
162-
| Self::Node
163-
| Self::Perl
164-
| Self::Pygrep
165-
| Self::Python
166-
| Self::Ruby
167-
| Self::Rust
168-
| Self::Script
169-
| Self::Swift
170-
| Self::System => true,
171-
Self::R => false,
172-
}
173-
}
174-
175117
pub(crate) fn supports_install_env(self) -> bool {
176118
match self {
177119
Self::Bun
@@ -214,7 +156,9 @@ impl Language {
214156
| Self::Script
215157
| Self::Swift
216158
| Self::System => ShellSupport::Supported,
217-
Self::R => ShellSupport::Unsupported("no runner is implemented yet"),
159+
Self::R => ShellSupport::Unsupported(
160+
"`entry` must use the R backend's `Rscript -e <expr>` or `Rscript <file>` forms",
161+
),
218162
Self::Dart => ShellSupport::Unsupported(
219163
"`--packages` injection requires the resolved argv to contain `dart` directly",
220164
),
@@ -369,13 +313,13 @@ impl Language {
369313
Self::Perl => PERL.install(hook, store, reporter).await,
370314
Self::Pygrep => PYGREP.install(hook, store, reporter).await,
371315
Self::Python => PYTHON.install(hook, store, reporter).await,
316+
Self::R => R.install(hook, store, reporter).await,
372317
Self::Ruby => RUBY.install(hook, store, reporter).await,
373318
Self::Rust => RUST.install(hook, store, reporter).await,
374319
Self::Script => SCRIPT.install(hook, store, reporter).await,
375320
Self::Swift => SWIFT.install(hook, store, reporter).await,
376321
Self::System => SYSTEM.install(hook, store, reporter).await,
377322
Self::Conda => CONDA.install(hook, store, reporter).await,
378-
Self::R => UNIMPLEMENTED.install(hook, store, reporter).await,
379323
}
380324
}
381325

@@ -397,13 +341,13 @@ impl Language {
397341
Self::Perl => PERL.check_health(info).await,
398342
Self::Pygrep => PYGREP.check_health(info).await,
399343
Self::Python => PYTHON.check_health(info).await,
344+
Self::R => R.check_health(info).await,
400345
Self::Ruby => RUBY.check_health(info).await,
401346
Self::Rust => RUST.check_health(info).await,
402347
Self::Script => SCRIPT.check_health(info).await,
403348
Self::Swift => SWIFT.check_health(info).await,
404349
Self::System => SYSTEM.check_health(info).await,
405350
Self::Conda => CONDA.check_health(info).await,
406-
Self::R => UNIMPLEMENTED.check_health(info).await,
407351
}
408352
}
409353

@@ -454,13 +398,13 @@ impl Language {
454398
Self::Perl => PERL.run(hook, filenames, store, reporter).await,
455399
Self::Pygrep => PYGREP.run(hook, filenames, store, reporter).await,
456400
Self::Python => PYTHON.run(hook, filenames, store, reporter).await,
401+
Self::R => R.run(hook, filenames, store, reporter).await,
457402
Self::Ruby => RUBY.run(hook, filenames, store, reporter).await,
458403
Self::Rust => RUST.run(hook, filenames, store, reporter).await,
459404
Self::Script => SCRIPT.run(hook, filenames, store, reporter).await,
460405
Self::Swift => SWIFT.run(hook, filenames, store, reporter).await,
461406
Self::System => SYSTEM.run(hook, filenames, store, reporter).await,
462407
Self::Conda => CONDA.run(hook, filenames, store, reporter).await,
463-
Self::R => UNIMPLEMENTED.run(hook, filenames, store, reporter).await,
464408
}
465409
}
466410
}

0 commit comments

Comments
 (0)