Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-metadata"
---

Fix incorrect Go package name parsing in `tspconfig.yaml`. The Go package name is now derived from the emitter output directory instead of the module path, which correctly excludes version suffixes (e.g., `/v4`) and uses the language-specific `service-dir` for accurate path resolution.
20 changes: 19 additions & 1 deletion packages/typespec-metadata/src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ function parseTypeScript(

/**
* Go-specific metadata parser.
* Extracts from module path, looking for azure-sdk-for-go suffix.
* Extracts from module path, looking for azure-sdk-for-go suffix, as a fallback.
* The primary derivation of package name from the emitter output directory is handled
* by the caller (createLanguageMetadata) after the relative output path is computed,
* because it requires knowledge of the base output directory.
*/
function parseGo(
options: Record<string, unknown>,
Expand Down Expand Up @@ -560,6 +563,21 @@ function createLanguageMetadata(
relativeOutputDir = relativeOutputDir.replace(/\{namespace\}/g, namespace);
}

// For Go, prefer the emitter output directory over the module path for package name derivation.
// The output directory gives the correct repo-relative path (e.g., sdk/resourcemanager/redisenterprise/armredisenterprise)
// without version suffixes that may appear in the module path (e.g., /v4).
if (heuristicLang === "go" && relativeOutputDir?.startsWith("{output-dir}/")) {
const explicitPackageName =
normalizedOptions["package-name"] ?? normalizedOptions["package_name"];
if (!explicitPackageName) {
const outputDirPackageName = relativeOutputDir.substring("{output-dir}/".length);
packageName = outputDirPackageName;
if (!normalizedOptions["namespace"]) {
namespace = outputDirPackageName;
}
}
}

return {
emitterName,
packageName,
Expand Down
61 changes: 61 additions & 0 deletions packages/typespec-metadata/test/collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,67 @@ describe("language-specific parsers", () => {
});
});

describe("@azure-tools/typespec-go emitter", () => {
it("should derive package name from emitter-output-dir instead of module path", () => {
// Simulates the redisenterprise scenario where module includes version suffix (/v4)
// but the emitter-output-dir gives the correct package path without the suffix.
const optionMap: Record<string, Record<string, unknown>> = {
"@azure-tools/typespec-go": {
module:
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/redisenterprise/armredisenterprise/v4",
"service-dir": "sdk/resourcemanager/redisenterprise",
"emitter-output-dir":
"c:/repos/tsp-output/sdk/resourcemanager/redisenterprise/armredisenterprise",
"generate-fakes": true,
flavor: "azure",
},
};

const result = buildLanguageMetadata(optionMap, {}, "c:/repos/tsp-output");
const lang = result["go"][0];

// Should use output dir path (without /v4 version suffix), not module path
expect(lang.packageName).toBe("sdk/resourcemanager/redisenterprise/armredisenterprise");
expect(lang.namespace).toBe("sdk/resourcemanager/redisenterprise/armredisenterprise");
expect(lang.outputDir).toBe(
"{output-dir}/sdk/resourcemanager/redisenterprise/armredisenterprise",
);
});

it("should fall back to module path when emitter-output-dir is not under base output dir", () => {
const optionMap: Record<string, Record<string, unknown>> = {
"@azure-tools/typespec-go": {
module: "github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets",
},
};

const result = buildLanguageMetadata(optionMap, {}, "/repos/tsp-output");
const lang = result["go"][0];

// No emitter-output-dir, falls back to module path
expect(lang.packageName).toBe("sdk/security/keyvault/azsecrets");
expect(lang.namespace).toBe("sdk/security/keyvault/azsecrets");
});

it("should respect explicit package-name over emitter-output-dir", () => {
const optionMap: Record<string, Record<string, unknown>> = {
"@azure-tools/typespec-go": {
"package-name": "sdk/custom/myPackage",
module: "github.com/Azure/azure-sdk-for-go/sdk/some/module/v2",
"emitter-output-dir":
"c:/repos/tsp-output/sdk/resourcemanager/redisenterprise/armredisenterprise",
flavor: "azure",
},
};

const result = buildLanguageMetadata(optionMap, {}, "c:/repos/tsp-output");
const lang = result["go"][0];

// Explicit package-name should not be overridden by emitter-output-dir
expect(lang.packageName).toBe("sdk/custom/myPackage");
});
});

describe("service-dir handling", () => {
it("should use language-specific service-dir if present", () => {
const languageServiceDir = "sdk/security/keyvault";
Expand Down
Loading