From 4f27aec396c9683c406702dcef7aae96d466b2ff Mon Sep 17 00:00:00 2001 From: itvi-1234 Date: Tue, 17 Nov 2026 19:09:58 +0530 Subject: [PATCH] feat(plugins): run the Java profiler with rc profile java Add the Java profiler as an optional language add-on, found the way the TypeScript one is: rc-profiler-java on PATH, or the absolute path in RC_JAVA_PROFILER. It is run only if its --version --json lists core profile schema 0.4.0, and everything after "java" is passed to it unchanged, so it keeps its own flags such as --classpath. The add-on is a launcher that starts the jar on the JDK named by RC_JAVA_HOME or JAVA_HOME, or the one bundled in its system package, or java on PATH, and says what is missing. So rc lists no runtime for it and does not look for java itself. rc plugins list shows it, and the deb suggests rc-profiler-java. Tested with a fake runner and, by hand, with the real launcher and jar: rc plugins list shows java ready, and rc profile java generate prints the profile. An existing test used profile java as an example of an unknown command; it now uses ruby. --- CLI-IMPLEMENTATION.md | 3 +- README.md | 20 ++++++++---- distribution/nfpm.yaml | 1 + internal/cli/go_profiler_test.go | 2 +- internal/cli/plugins_test.go | 54 +++++++++++++++++++++++++++++--- internal/plugins/plugins.go | 13 +++++++- internal/plugins/plugins_test.go | 35 +++++++++++++++++++++ 7 files changed, 114 insertions(+), 14 deletions(-) diff --git a/CLI-IMPLEMENTATION.md b/CLI-IMPLEMENTATION.md index c9065a6..1761a3b 100644 --- a/CLI-IMPLEMENTATION.md +++ b/CLI-IMPLEMENTATION.md @@ -2,7 +2,7 @@ Status: implementation sequence, updated 2026-11-11. Native Go profiling and binding commands are implemented, with CI coverage for the relocated CLI and shared YAML resources. -The TypeScript add-on runs from PATH (`rc profile typescript`, `rc plugins list`). Add-on descriptors, `rc doctor`, +The TypeScript and Java add-ons run from PATH (`rc profile typescript`, `rc profile java`, `rc plugins list`). Add-on descriptors, `rc doctor`, other languages, system-package distribution, and upstream workflow migration remain planned. Repository: github.com/runtimeconditions/rc-cli; executable and command name: `rc`. System package and Homebrew formula name: `rc-cli`; releases use independent `vMAJOR.MINOR.PATCH` tags. @@ -61,6 +61,7 @@ rc profile generate --dir ./service --out profile.yaml rc profile go generate --dir ./service --out profile.yaml rc profile go validate-extensions --dir ./service rc profile typescript generate --project ./service --name orders --workload-uri https://example.com/orders +rc profile java generate --project ./service --classpath bindings.jar --name orders --workload-uri https://example.com/orders rc profile python profile generate --project ./service rc plugins list [--output yaml] rc doctor [--language go|python|java|...] [--output yaml] diff --git a/README.md b/README.md index 2922749..beceecf 100644 --- a/README.md +++ b/README.md @@ -99,19 +99,27 @@ generation leaves an existing `--out` file unchanged. Other languages run a separate add-on that `rc` finds on `PATH`. It can come from the language's own tooling or from a system package, and `rc` does not care which. The TypeScript profiler is on npm for developers who want it in their npm scripts: -`npm install --global @runtimeconditions/typescript-profiler`. System packages for Homebrew, +`npm install --global @runtimeconditions/typescript-profiler`. The Java profiler is a jar that its +launcher, `rc-profiler-java`, runs; download it from the +[releases](https://github.com/runtimeconditions/java-rc-profiler/releases) of +`runtimeconditions/java-rc-profiler` and put the launcher on `PATH`. System packages for Homebrew, apt, and yum, so that `rc` and its add-ons install the same way on a build server, are planned. | Command | Purpose | | --- | --- | | `rc profile typescript [flags]` | Run the installed TypeScript profiler. | +| `rc profile java [flags]` | Run the installed Java profiler. | | `rc plugins list [--output yaml]` | Show the Go profiler and each add-on's status, version, and path. | -Everything after `typescript` goes to the add-on unchanged, so it keeps its own flags -(for example `--project` instead of `--dir`). `rc` looks for `rc-profiler-typescript`, then -`runtimeconditions-typescript-profiler`, on `PATH`; `RC_TYPESCRIPT_PROFILER` can name an -absolute path instead. It runs the add-on only if `--version --json` lists the core profile -schema `rc` targets (0.4.0). The add-on's output and exit status pass through. +Everything after `typescript` or `java` goes to the add-on unchanged, so it keeps its own flags +(for example `--project` instead of `--dir`, and `--classpath` for Java). `rc` looks for +`rc-profiler-typescript`, then `runtimeconditions-typescript-profiler`, or `rc-profiler-java`, on `PATH`; +`RC_TYPESCRIPT_PROFILER` or `RC_JAVA_PROFILER` can name an absolute path instead. It runs the add-on only +if `--version --json` lists the core profile schema `rc` targets (0.4.0). The add-on's output and exit +status pass through. + +The Java launcher chooses the JDK, not `rc`: `RC_JAVA_HOME`, then `JAVA_HOME`, then the runtime bundled +in its system package, then `java` on `PATH`. It needs JDK 17 or newer and says so when none is found. ## Binding commands diff --git a/distribution/nfpm.yaml b/distribution/nfpm.yaml index 79a5f2f..3b78de5 100644 --- a/distribution/nfpm.yaml +++ b/distribution/nfpm.yaml @@ -12,6 +12,7 @@ homepage: https://github.com/runtimeconditions/rc-cli license: MIT suggests: - rc-profiler-typescript + - rc-profiler-java contents: - src: usr dst: /usr diff --git a/internal/cli/go_profiler_test.go b/internal/cli/go_profiler_test.go index 1af3bd7..80ed141 100644 --- a/internal/cli/go_profiler_test.go +++ b/internal/cli/go_profiler_test.go @@ -124,7 +124,7 @@ func TestGoValidationLibraryCalls(t *testing.T) { func TestGoUsageAndHelp(t *testing.T) { for _, args := range [][]string{ {"profile", "generate", "--unknown"}, {"profile", "generate", "-dir", "."}, - {"profile", "go", "generate", "extra"}, {"profile", "java", "generate"}, + {"profile", "go", "generate", "extra"}, {"profile", "ruby", "generate"}, {"profile", "validate-extension"}, {"profile", "go", "validate-extension", "--package", ""}, } { code, out, errOut := invoke(t, args...) diff --git a/internal/cli/plugins_test.go b/internal/cli/plugins_test.go index 4b1334a..ac50a9e 100644 --- a/internal/cli/plugins_test.go +++ b/internal/cli/plugins_test.go @@ -34,6 +34,8 @@ func (f *fakeRunner) Run(_ context.Context, spec process.Spec) (int, error) { const readyVersion = `{"name":"@runtimeconditions/typescript-profiler","version":"0.2.0","coreSchemas":["0.4.0"]}` +const javaVersion = `{"name":"runtimeconditions-java-profiler","version":"0.1.0","coreSchemas":["0.4.0"]}` + func deps(runner process.Runner, installed ...string) Dependencies { present := map[string]bool{} for _, name := range installed { @@ -159,11 +161,20 @@ func TestPluginsListReportsEveryStatus(t *testing.T) { t.Fatalf("an add-on with its own runtime is ready without node: %+v", packaged[1]) } ready := read(deps(&fakeRunner{version: readyVersion}, "rc-profiler-typescript", "node")) - if len(ready) != 2 || ready[0].Language != "go" || ready[0].Status != "built-in" || + if len(ready) != 3 || ready[0].Language != "go" || ready[0].Status != "built-in" || ready[1].Language != "typescript" || ready[1].Status != "ready" || ready[1].Version != "0.2.0" || - ready[1].Path != "/usr/bin/rc-profiler-typescript" || ready[1].Origin != "path" { + ready[1].Path != "/usr/bin/rc-profiler-typescript" || ready[1].Origin != "path" || + ready[2].Language != "java" || ready[2].Status != "missing" { t.Fatalf("%+v", ready) } + java := read(deps(&fakeRunner{version: javaVersion}, "rc-profiler-java")) + if java[2].Language != "java" || java[2].Status != "ready" || java[2].Version != "0.1.0" || + java[2].Path != "/usr/bin/rc-profiler-java" || java[2].Origin != "path" { + t.Fatalf("a Java launcher on PATH is ready without java on PATH: %+v", java[2]) + } + if incompatible := read(deps(&fakeRunner{version: `{"name":"x","version":"1","coreSchemas":["0.3.0"]}`}, "rc-profiler-java")); incompatible[2].Status != "incompatible" { + t.Fatalf("%+v", incompatible[2]) + } for status, d := range map[string]Dependencies{ "missing": deps(&fakeRunner{}), "runtime-missing": deps(&fakeRunner{}, "rc-profiler-typescript"), @@ -179,15 +190,16 @@ func TestPluginsListReportsEveryStatus(t *testing.T) { func TestPluginsListTextAndHelp(t *testing.T) { code, out, _ := run(t, deps(&fakeRunner{version: readyVersion}, "rc-profiler-typescript", "node"), "plugins", "list") lines := strings.Split(strings.TrimSpace(out), "\n") - if code != 0 || len(lines) != 3 || !strings.HasPrefix(lines[0], "LANGUAGE") || - !slices.Equal(strings.Fields(lines[2]), []string{"typescript", "ready", "0.2.0", "/usr/bin/rc-profiler-typescript"}) { + if code != 0 || len(lines) != 4 || !strings.HasPrefix(lines[0], "LANGUAGE") || + !slices.Equal(strings.Fields(lines[2]), []string{"typescript", "ready", "0.2.0", "/usr/bin/rc-profiler-typescript"}) || + !slices.Equal(strings.Fields(lines[3]), []string{"java", "missing"}) { t.Fatalf("code=%d %q", code, out) } if code, _, errOut := run(t, deps(&fakeRunner{}), "plugins", "list", "--output", "json"); code != 2 || !strings.Contains(errOut, "unsupported output format") { t.Fatalf("code=%d %q", code, errOut) } code, out, _ = run(t, deps(&fakeRunner{}), "profile", "--help") - if code != 0 || !strings.Contains(out, "typescript") || !strings.Contains(out, "go ") { + if code != 0 || !strings.Contains(out, "typescript") || !strings.Contains(out, "java") || !strings.Contains(out, "go ") { t.Fatalf("profile help %q", out) } } @@ -208,3 +220,35 @@ func TestCoreSchemaVersionMatchesTheComponentLock(t *testing.T) { t.Fatalf("lock says %q, rc targets %q (%v)", lock.GoProfiler.CoreSchema.Version, coreSchemaVersion, err) } } + +func TestJavaAddOnReceivesItsArgumentsUnchangedAndItsOutputPassesThrough(t *testing.T) { + runner := &fakeRunner{version: javaVersion, output: "profile: yaml\n"} + args := []string{"generate", "--project", "./svc", "--classpath", "a.jar:b.jar", "--name", "orders", "--workload-uri", "https://example.com/o", "--", "--odd", "a b"} + code, out, errOut := run(t, deps(runner, "rc-profiler-java"), append([]string{"profile", "java"}, args...)...) + if code != 0 || out != "profile: yaml\n" || errOut != "" { + t.Fatalf("code=%d out=%q err=%q", code, out, errOut) + } + if len(runner.calls) != 1 || runner.calls[0].Path != "/usr/bin/rc-profiler-java" || !slices.Equal(runner.calls[0].Args, args) { + t.Fatalf("calls: %+v", runner.calls) + } +} + +func TestJavaAddOnThatIsNotInstalledSaysHowToGetIt(t *testing.T) { + runner := &fakeRunner{} + code, _, errOut := run(t, deps(runner, "node"), "profile", "java", "generate") + if code != 1 || strings.Count(errOut, "rc:") != 1 || !strings.Contains(errOut, "Java profiler is not installed") || + !strings.Contains(errOut, "https://github.com/runtimeconditions/java-rc-profiler/releases") || strings.Contains(errOut, "Usage:") { + t.Fatalf("code=%d %q", code, errOut) + } + if len(runner.calls) != 0 { + t.Fatalf("the add-on ran: %+v", runner.calls) + } +} + +func TestJavaAddOnForAnotherCoreSchemaIsNotRun(t *testing.T) { + runner := &fakeRunner{version: `{"name":"runtimeconditions-java-profiler","version":"0.1.0","coreSchemas":["0.3.0"]}`} + code, _, errOut := run(t, deps(runner, "rc-profiler-java"), "profile", "java", "generate") + if code != 1 || !strings.Contains(errOut, "0.4.0") || len(runner.calls) != 0 { + t.Fatalf("code=%d %q calls=%+v", code, errOut, runner.calls) + } +} diff --git a/internal/plugins/plugins.go b/internal/plugins/plugins.go index a933ff4..4db0c10 100644 --- a/internal/plugins/plugins.go +++ b/internal/plugins/plugins.go @@ -45,8 +45,19 @@ var TypeScript = Language{ InstallHint: "npm install --global @runtimeconditions/typescript-profiler", } +// Java is the Java profiler, a jar that the rc-profiler-java launcher runs on the JDK named by RC_JAVA_HOME or +// JAVA_HOME, or the one bundled in its system package, or java on PATH. It lists no runtime, because the launcher +// looks for the JDK itself and says what is missing, so rc cannot tell from PATH alone whether one is there. +var Java = Language{ + ID: "java", + Display: "Java", + Executables: []string{"rc-profiler-java"}, + OverrideEnv: "RC_JAVA_PROFILER", + InstallHint: "download rc-profiler-java from https://github.com/runtimeconditions/java-rc-profiler/releases and put it on PATH", +} + // Languages returns the optional languages rc supports. -func Languages() []Language { return []Language{TypeScript} } +func Languages() []Language { return []Language{TypeScript, Java} } // Finder looks up executables. Tests replace both functions. type Finder struct { diff --git a/internal/plugins/plugins_test.go b/internal/plugins/plugins_test.go index f913f52..a805ead 100644 --- a/internal/plugins/plugins_test.go +++ b/internal/plugins/plugins_test.go @@ -145,3 +145,38 @@ func TestDescribeRunsARealExecutable(t *testing.T) { t.Fatalf("%+v %v", info, err) } } + +func TestJavaIsFoundByItsLauncherAndNeedsNoRuntimeOnPath(t *testing.T) { + // The launcher picks the JDK itself, so java need not be on PATH for rc to run it. + found, err := finder(map[string]string{"rc-profiler-java": "/usr/bin/rc-profiler-java"}, nil).Find(Java) + if err != nil || found.Path != "/usr/bin/rc-profiler-java" || found.Origin != "path" { + t.Fatalf("%+v %v", found, err) + } + if got := finder(map[string]string{"rc-profiler-java": "/usr/bin/rc-profiler-java"}, nil).MissingRuntime(Java); got != "" { + t.Fatalf("Java lists no runtime, got %q", got) + } + // Only the launcher counts; the jar's own name is not an executable. + if _, err := finder(map[string]string{"runtimeconditions-java-profiler": "/usr/bin/x"}, nil).Find(Java); err == nil { + t.Fatal("found an add-on that is not the launcher") + } +} + +func TestJavaMissingSaysHowToInstallIt(t *testing.T) { + _, err := finder(map[string]string{}, nil).Find(Java) + if err == nil { + t.Fatal("expected an error") + } + for _, want := range []string{"Java profiler is not installed", "rc-profiler-java", "install it with: " + Java.InstallHint, "RC_JAVA_PROFILER"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("%q is missing from %q", want, err) + } + } +} + +func TestJavaOverrideNamesAnAbsoluteExecutable(t *testing.T) { + installed := map[string]string{"/opt/java/rc-profiler-java": "/opt/java/rc-profiler-java"} + found, err := finder(installed, map[string]string{Java.OverrideEnv: "/opt/java/rc-profiler-java"}).Find(Java) + if err != nil || found.Path != "/opt/java/rc-profiler-java" || found.Origin != "override" { + t.Fatalf("%+v %v", found, err) + } +}