Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(workspace): read package-qualified Java ImplBase and Node grpc-js addService as gRPC providers (#4529)

Raghav Chamadiyacdb5b005e660
diff --git a/docs/architecture/language-support.md b/docs/architecture/language-support.md
index 6b2b763bc..7757c2528 100644
--- a/docs/architecture/language-support.md
+++ b/docs/architecture/language-support.md
@@ -1311,7 +1311,7 @@ mislead the masker.
| Contract | Providers | Consumers |
|----------|-----------|-----------|
| **HTTP** | Express, Hono, Fastify, Koa, Elysia, NestJS, Next.js, Remix, FastAPI, Flask, Django, Spring, JAX-RS, Micronaut, Laravel, Go, ASP.NET, Axum/Actix/Rocket, OpenAPI 3.x | Client calls in JS/TS, Python, PHP, Java, Kotlin, Go, C#, Ruby and Rust; JS/TS client instances carry their base URL across files |
-| **gRPC** | `.proto` IDL, Go, Java, Python, NestJS (`@GrpcMethod`), C# (gRPC-dotnet) | Go, Java, Python, C# |
+| **gRPC** | `.proto` IDL, Go, Java (bare or package-qualified `ImplBase`), Python, NestJS (`@GrpcMethod`), Node grpc-js (`server.addService(X.service)`), C# (gRPC-dotnet) | Go, Java, Python, C# |
| **Topic** | Kafka (Spring `KafkaTemplate` under any name, kafka-clients `ProducerRecord`, kafkajs, kafka-python, aiokafka, confluent-kafka for Python and .NET, Go sarama, segmentio/kafka-go and franz-go), RabbitMQ (Spring AMQP, amqplib, pika, php-amqplib), NATS, Redis pub/sub, BullMQ / Bull, SQS / SNS, NestJS `ClientProxy`, Laravel job dispatch | The same libraries' consumers (including pattern subscriptions and Laravel `ShouldQueue` classes), plus RabbitMQ queue bindings |
| **Socket** | SignalR `MapHub`, FastAPI `@app.websocket`, `ws`, NestJS gateways; socket.io `emit`, Laravel broadcast events, Pusher `trigger` | ClientWebSocket, SignalR client, NativeWebSocket, WebSocketSharp, `new WebSocket(url)`; socket.io `on`, Laravel Echo, pusher-js |
| **Data** | DDL `CREATE`/`ALTER`, Alembic `op.create_table`, Laravel `Schema::create`/`Schema::table`, Knex migrations, ORM models (SQLAlchemy, SQLModel, Django, JPA, EF Core, ActiveRecord, Eloquent, Prisma, TypeORM, Sequelize, Drizzle) | SQL string literals in app code (sqlglot-parsed, verb-anchored-regex fallback), Laravel `DB::table`, Knex queries |
diff --git a/packages/core/src/repowise/core/workspace/extractors/grpc/languages.py b/packages/core/src/repowise/core/workspace/extractors/grpc/languages.py
index 483649578..3fae5b0f1 100644
--- a/packages/core/src/repowise/core/workspace/extractors/grpc/languages.py
+++ b/packages/core/src/repowise/core/workspace/extractors/grpc/languages.py
@@ -2,10 +2,11 @@
Every language dialect was the same loop over a provider regex and a consumer
regex, differing only in the pattern, the display wording and the origin.
-The three real per-language facts are declared instead of coded: C# has two
-provider shapes, TypeScript has no consumer shape and is the only one whose
-match names a method, and a bare ``new XxxClient(...)`` needs a gRPC marker in
-the file before it means anything.
+The real per-language facts are declared instead of coded: C# has two
+provider shapes, TypeScript has no consumer shape and one shape that names a
+method, and a shape other libraries share (a bare ``new XxxClient(...)``, an
+``addService(...)``) declares a ``gate``: a marker the file must carry before
+that shape means anything.
``.proto`` is not here — it is an IDL with a real grammar, not a stub shape.
"""
@@ -44,6 +45,8 @@ class _Rule:
source: str # meta["source"], the tag that says which shape matched
origin: ContractOrigin
method_group: int | None = None # only a shape that names a method sets this
+ #: A marker the file must carry before this rule runs.
+ gate: re.Pattern[str] | None = None
@dataclass(frozen=True)
@@ -54,8 +57,6 @@ class _Language:
#: Tokens one of which a file must contain before any rule runs: every
#: rule's pattern needs one, so the gate drops no match.
requires: tuple[str, ...] = ()
- #: A marker the file must carry before any consumer rule runs.
- consumer_gate: re.Pattern[str] | None = None
#: Stub-name prefixes that are test doubles rather than clients.
false_prefixes: tuple[str, ...] = field(default=())
@@ -78,8 +79,8 @@ LANGUAGES: tuple[_Language, ...] = (
name="java",
extensions=JAVA,
rules=(
- # extends AuthServiceGrpc.AuthServiceImplBase
- _Rule("provider", re.compile(r"extends\s+(\w+)Grpc\.(\w+)ImplBase"),
+ # extends AuthServiceGrpc.AuthServiceImplBase, package-qualified or not
+ _Rule("provider", re.compile(r"extends\s+(?:[\w.]+\.)?(\w+)Grpc\.(\w+)ImplBase"),
"java:extends {service}Grpc.ImplBase", "java_extends", _ROUTE_REGISTRATION),
# AuthServiceGrpc.newBlockingStub(channel)
_Rule("consumer", re.compile(r"(\w+)Grpc\.new(?:Blocking|Future)?Stub\s*\("),
@@ -109,8 +110,15 @@ LANGUAGES: tuple[_Language, ...] = (
_Rule("provider", re.compile(r"@GrpcMethod\s*\(\s*'(\w+)'\s*,\s*'(\w+)'\s*\)"),
"ts:@GrpcMethod('{service}', '{method}')", "ts_decorator", _ROUTE_REGISTRATION,
method_group=2),
+ # server.addService(pkg.PaymentService.service, {charge}): grpc-js or the
+ # legacy `grpc` package, the proto often loaded at runtime. A proto service
+ # name is PascalCase, so `this.service` names nothing.
+ _Rule("provider",
+ re.compile(r"\.addService\s*\(\s*(?:[\w$]+\.)*?([A-Z]\w*)\.service\b"),
+ "js:addService({service}.service)", "js_add_service", _ROUTE_REGISTRATION,
+ gate=re.compile(r"""['"](?:@grpc/grpc-js|grpc)['"]""")),
),
- requires=("@GrpcMethod",),
+ requires=("@GrpcMethod", "addService"),
),
_Language(
name="csharp",
@@ -123,16 +131,16 @@ LANGUAGES: tuple[_Language, ...] = (
_Rule("provider",
re.compile(r"class\s+\w+\s*:\s*(?:[\w.]+\.)?(\w+?)\s*\.\s*\1Base\b"),
"cs:extends {service}.{service}ServiceBase", "csharp_base", _ROUTE_REGISTRATION),
- # new AuthServiceClient(channel)
+ # new AuthServiceClient(channel): TLS and HTTP classes match too, hence the gate.
_Rule("consumer", re.compile(r"\bnew\s+(\w+)Client\s*\("),
- "cs:new {service}Client", "csharp_client", _NAME_HEURISTIC),
+ "cs:new {service}Client", "csharp_client", _NAME_HEURISTIC,
+ gate=re.compile(
+ r"\bGrpcChannel\b|\bGrpc\.(?:Net|Core)\b|\bCallInvoker\b|\bChannelBase\b"
+ r"|\bAddGrpcClient\b|using\s+Grpc\b"
+ )),
),
- # The consumer gate's markers all hold `Grpc`, `CallInvoker` or `Base`.
+ # The client gate's markers all hold `Grpc`, `CallInvoker` or `Base`.
requires=("MapGrpcService", "Base", "Grpc", "CallInvoker"),
- consumer_gate=re.compile(
- r"\bGrpcChannel\b|\bGrpc\.(?:Net|Core)\b|\bCallInvoker\b|\bChannelBase\b"
- r"|\bAddGrpcClient\b|using\s+Grpc\b"
- ),
false_prefixes=("mock", "test", "fake", "http"),
),
)
@@ -149,10 +157,9 @@ class LanguageGrpcDialect:
def extract(self, ctx: ScanContext) -> list[Contract]:
lang = self._lang
- gated = lang.consumer_gate is not None and not lang.consumer_gate.search(ctx.content)
out: list[Contract] = []
for rule in lang.rules:
- if rule.role == "consumer" and gated:
+ if rule.gate is not None and not rule.gate.search(ctx.content):
continue
for m in code_matches(ctx, rule.pattern):
service = m.group(1)
diff --git a/tests/unit/workspace/test_grpc_languages.py b/tests/unit/workspace/test_grpc_languages.py
index 7faf9db97..2941d27d6 100644
--- a/tests/unit/workspace/test_grpc_languages.py
+++ b/tests/unit/workspace/test_grpc_languages.py
@@ -126,3 +126,74 @@ class TestPythonAndGoKeepTheirOwnRules:
"pb.RegisterAuthServiceServer(s, &impl{})\nc := pb.NewAuthServiceClient(conn)\n",
)
assert [(r[1], r[3]) for r in rows] == [("provider", 0.85), ("consumer", 0.75)]
+
+
+class TestJavaQualifiedBase:
+ def test_a_package_qualified_impl_base_is_a_provider(self) -> None:
+ # Online Boutique's adservice: a nested class naming the base by package.
+ rows = _extract(
+ ".java",
+ "public final class AdService {\n"
+ " private static class AdServiceImpl\n"
+ " extends hipstershop.AdServiceGrpc.AdServiceImplBase {}\n"
+ "}\n",
+ )
+ assert [(r[0], r[1], r[2]) for r in rows] == [
+ ("grpc::AdService/*", "provider", "java:extends AdServiceGrpc.ImplBase")
+ ]
+
+ def test_the_bare_base_still_matches(self) -> None:
+ rows = _extract(".java", "class Impl extends AuthServiceGrpc.AuthServiceImplBase {}\n")
+ assert [r[0] for r in rows] == ["grpc::AuthService/*"]
+
+
+class TestNodeAddService:
+ _SERVER = (
+ "const grpc = require('@grpc/grpc-js');\n"
+ "const server = new grpc.Server();\n"
+ "this.server.addService(\n"
+ " hipsterShopPackage.PaymentService.service,\n"
+ " { charge: handler }\n"
+ ");\n"
+ "server.addService(shopProto.CurrencyService.service, {convert});\n"
+ )
+
+ def test_each_registered_service_is_a_provider(self) -> None:
+ rows = _extract(".js", self._SERVER)
+ assert rows == [
+ (
+ "grpc::PaymentService/*",
+ "provider",
+ "js:addService(PaymentService.service)",
+ 0.85,
+ (("service", "PaymentService"), ("source", "js_add_service")),
+ ),
+ (
+ "grpc::CurrencyService/*",
+ "provider",
+ "js:addService(CurrencyService.service)",
+ 0.85,
+ (("service", "CurrencyService"), ("source", "js_add_service")),
+ ),
+ ]
+
+ def test_the_legacy_grpc_package_and_typescript_imports_count(self) -> None:
+ legacy = "const grpc = require('grpc');\nserver.addService(proto.Greeter.service, impl);\n"
+ ts = "import * as grpc from '@grpc/grpc-js';\nserver.addService(GreeterService.service, impl);\n"
+ assert [r[0] for r in _extract(".js", legacy)] == ["grpc::Greeter/*"]
+ assert [r[0] for r in _extract(".ts", ts)] == ["grpc::GreeterService/*"]
+
+ def test_without_a_grpc_import_add_service_is_not_read(self) -> None:
+ # `addService(x.service)` is a grpc-js shape, but nothing else says so.
+ assert not _extract(".js", "registry.addService(plugins.Billing.service, opts);\n")
+
+ def test_a_lowercase_receiver_is_not_a_service_name(self) -> None:
+ # `this.service` is a field, not a proto service; the chain's PascalCase
+ # segment is the name even when an earlier segment is called `service`.
+ rows = _extract(
+ ".js",
+ "const grpc = require('@grpc/grpc-js');\n"
+ "s.addService(this.service, impl);\n"
+ "s.addService(pkg.service.Billing.service, impl);\n",
+ )
+ assert [r[0] for r in rows] == ["grpc::Billing/*"]