Skip to content

Commit d7e9876

Browse files
Fix no-service-found warning to check for service existence and exclude library namespaces (#8974)
The `no-service-found` warning incorrectly checked for empty routes instead of checking whether a `@service` decorator exists, and warned on all namespaces including library namespaces. ## Changes - **Modified `reportIfNoRoutes` logic**: Now checks `listServices(program).length === 0` instead of `routes.length === 0` to determine if any service exists - **Use `getLocationContext` to filter library code**: Uses `getLocationContext(program, namespace).type === "project"` to only warn on user project namespaces, automatically excluding library namespaces based on source file location - **Skip global namespace**: Added check to skip the global namespace (with empty name "") ## Example Before this change, the following would incorrectly warn on both namespaces: ```typespec namespace MyApp { @get @route("/app") op appOp(): void; } namespace TypeSpec.Library { @get @route("/lib") op libOp(): void; } ``` After this change, only `MyApp` receives the warning since `TypeSpec.Library` is from a library package (determined by source file location, not namespace naming). > [!WARNING] > > <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <__filter_complete__></__filter_complete__></details><issue_title>no-service-found warning should actually check if there is any service not just operations</issue_title> ><issue_description>https://azure.github.io/typespec-azure/playground/?e=%40azure-tools%2Ftypespec-autorest&amp;c=aW1wb3J0ICJAYXp1cmUtdG9vbHMvdHlwZXNwZWMtxhVjb3JlIjsKCkBzZXJ2aWNlKCN7IHRpdGxlOiAiQcQiIEFJIiB9KQpuYW1lc3BhY2UgxRcuQUkuUHJvamVjdHMgewoKfQo%3D&amp;options=%7B%7D&amp;vs=%7B%7D > > > ```tsp > import "@azure-tools/typespec-azure-core"; > > @service > namespace Test; > ``` > > > Additionally it would be good that the error is not flagged on all namespaces, maybe only on user namespaces</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > ><comments> ></comments> > - Fixes #8973 <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>no-service-found warning should actually check if there is any service not just operations</issue_title> > <issue_description>https://azure.github.io/typespec-azure/playground/?e=%40azure-tools%2Ftypespec-autorest&c=aW1wb3J0ICJAYXp1cmUtdG9vbHMvdHlwZXNwZWMtxhVjb3JlIjsKCkBzZXJ2aWNlKCN7IHRpdGxlOiAiQcQiIEFJIiB9KQpuYW1lc3BhY2UgxRcuQUkuUHJvamVjdHMgewoKfQo%3D&options=%7B%7D&vs=%7B%7D > > > ```tsp > import "@azure-tools/typespec-azure-core"; > > @service > namespace Test; > ``` > > > Additionally it would be good that the error is not flagged on all namespaces, maybe only on user namespaces</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes #8973 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: timotheeguerin <[email protected]> Co-authored-by: Timothee Guerin <[email protected]>
1 parent e3c8704 commit d7e9876

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http"
5+
---
6+
7+
Do not report `no-service-found` if there is a service even if it has no routes

packages/http/src/operations.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
createDiagnosticCollector,
33
Diagnostic,
44
DiagnosticCollector,
5+
getLocationContext,
56
getOverloadedOperation,
67
getOverloads,
78
listOperationsIn,
@@ -107,10 +108,18 @@ export function getHttpService(
107108
}
108109

109110
export function reportIfNoRoutes(program: Program, routes: HttpOperation[]) {
110-
if (routes.length === 0) {
111+
const services = listServices(program);
112+
// Only warn if there are no services defined anywhere in the program
113+
if (services.length === 0) {
111114
navigateProgram(program, {
112115
namespace: (namespace) => {
113-
if (namespace.operations.size > 0) {
116+
// Skip the global namespace (it has an empty name)
117+
if (namespace.name === "") {
118+
return;
119+
}
120+
// Only warn on user project namespaces with operations, not library namespaces
121+
const locationContext = getLocationContext(program, namespace);
122+
if (namespace.operations.size > 0 && locationContext.type === "project") {
114123
reportDiagnostic(program, {
115124
code: "no-service-found",
116125
format: {

0 commit comments

Comments
 (0)