From 0e15b56a4fd5a64f442a0a3a7be83e357ddc9adf Mon Sep 17 00:00:00 2001 From: samdanae Date: Thu, 19 Nov 2026 15:39:06 +1300 Subject: [PATCH 1/2] fix(compiler): add bin/ and obj/ to the tsp init .gitignore for the C# server emitter --- ...t-gitignore-patterns-2026-10-19-12-56-0.md | 7 ++++++ .../compiler/.scripts/build-init-templates.ts | 1 + packages/compiler/src/init/init-template.ts | 3 +++ packages/compiler/src/init/scaffold.ts | 8 ++++++- packages/compiler/templates/scaffolding.json | 6 ++++- .../compiler/test/init/init-template.test.ts | 23 +++++++++++++++++++ 6 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 .chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md diff --git a/.chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md b/.chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md new file mode 100644 index 00000000000..a7c2fbd6ddb --- /dev/null +++ b/.chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@typespec/compiler" +--- + +`tsp init` adds `bin/` and `obj/` to the generated `.gitignore` when the C# server emitter is selected. Init templates can list `gitignorePatterns` for each emitter to add their own lines. diff --git a/packages/compiler/.scripts/build-init-templates.ts b/packages/compiler/.scripts/build-init-templates.ts index 0d5c63de241..5411c627046 100644 --- a/packages/compiler/.scripts/build-init-templates.ts +++ b/packages/compiler/.scripts/build-init-templates.ts @@ -74,6 +74,7 @@ const builtInTemplates: Record = { \tMore information on getting started: \thttps://aka.ms/tsp/hscs/start `, + gitignorePatterns: ["bin/", "obj/"], }, "@typespec/http-server-js": { label: "JavaScript server stubs", diff --git a/packages/compiler/src/init/init-template.ts b/packages/compiler/src/init/init-template.ts index 349bf80cee2..5430f0c8f4c 100644 --- a/packages/compiler/src/init/init-template.ts +++ b/packages/compiler/src/init/init-template.ts @@ -92,6 +92,8 @@ export interface EmitterTemplate { message?: string; /** Optional specific emitter version. `latest` if not specified */ version?: string; + /** Optional lines to add to the generated .gitignore when this emitter is selected */ + gitignorePatterns?: string[]; } /** @@ -151,6 +153,7 @@ export const InitTemplateSchema: JSONSchemaType = { options: {} as any, message: { type: "string", nullable: true }, version: { type: "string", nullable: true }, + gitignorePatterns: { type: "array", items: { type: "string" }, nullable: true }, }, required: [], }, diff --git a/packages/compiler/src/init/scaffold.ts b/packages/compiler/src/init/scaffold.ts index ad692ee2457..dc964edd21b 100644 --- a/packages/compiler/src/init/scaffold.ts +++ b/packages/compiler/src/init/scaffold.ts @@ -235,7 +235,13 @@ async function writeGitignore(host: SystemHost, config: ScaffoldingConfig) { return; } - return host.writeFile(joinPaths(config.directory, ".gitignore"), defaultGitignore); + const extraLines = new Set( + Object.values(config.emitters).flatMap((emitter) => emitter?.gitignorePatterns ?? []), + ); + const content = extraLines.size + ? [defaultGitignore, "", ...extraLines].join("\n") + : defaultGitignore; + return host.writeFile(joinPaths(config.directory, ".gitignore"), content); } async function writeFiles(host: SystemHost, config: ScaffoldingConfig) { diff --git a/packages/compiler/templates/scaffolding.json b/packages/compiler/templates/scaffolding.json index 2184ab46b4d..26b7854f55e 100644 --- a/packages/compiler/templates/scaffolding.json +++ b/packages/compiler/templates/scaffolding.json @@ -61,7 +61,11 @@ "options": { "emitter-output-dir": "{output-dir}/server/aspnet" }, - "message": "\n\tGenerated ASP.Net services require dotnet 9:\n\thttps://dotnet.microsoft.com/download \n\n\tCreate an ASP.Net service project for your TypeSpec:\n\t> npx hscs-scaffold . --use-swaggerui --overwrite\n\n\tMore information on getting started:\n\thttps://aka.ms/tsp/hscs/start\n " + "message": "\n\tGenerated ASP.Net services require dotnet 9:\n\thttps://dotnet.microsoft.com/download \n\n\tCreate an ASP.Net service project for your TypeSpec:\n\t> npx hscs-scaffold . --use-swaggerui --overwrite\n\n\tMore information on getting started:\n\thttps://aka.ms/tsp/hscs/start\n ", + "gitignorePatterns": [ + "bin/", + "obj/" + ] }, "@typespec/http-server-js": { "label": "JavaScript server stubs", diff --git a/packages/compiler/test/init/init-template.test.ts b/packages/compiler/test/init/init-template.test.ts index 688fc35fecb..94237970dc3 100644 --- a/packages/compiler/test/init/init-template.test.ts +++ b/packages/compiler/test/init/init-template.test.ts @@ -127,6 +127,29 @@ it("can exclude .gitignore file", async () => { strictEqual(typeof getOutputFile(".gitignore"), "undefined"); }); +it("adds gitignore patterns from selected emitters to .gitignore file", async () => { + const emitters = { + foo: { selected: true, gitignorePatterns: ["bin/", "obj/"] }, + }; + + await runTemplate({ emitters }, { emitters }); + const output = getOutputFile(".gitignore"); + ok(output); + expect(output.split("\n")).toEqual(expect.arrayContaining(["bin/", "obj/"])); +}); + +it("adds a gitignore pattern only once when multiple emitters specify it", async () => { + const emitters = { + foo: { selected: true, gitignorePatterns: ["bin/", "obj/"] }, + bar: { selected: true, gitignorePatterns: ["bin/"] }, + }; + + await runTemplate({ emitters }, { emitters }); + const output = getOutputFile(".gitignore"); + ok(output); + expect(output.split("\n").filter((line) => line === "bin/")).toHaveLength(1); +}); + it("specifying both config and emitters merge the 2", async () => { const emitters = { foo: { selected: true, options: { opt1: "val-1" } }, From 11c27016b8f2d8dc4a4ad205f46f507ae1c8e28b Mon Sep 17 00:00:00 2001 From: samdanaeimran Date: Fri, 20 Nov 2026 12:45:40 +1300 Subject: [PATCH 2/2] test(compiler): check tsp init leaves out gitignore patterns of unselected emitters --- ...t-gitignore-patterns-2026-10-19-12-56-0.md | 9 +++++++ packages/compiler/test/cli/init.test.ts | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/.chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md b/.chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md index a7c2fbd6ddb..1f1542d8b28 100644 --- a/.chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md +++ b/.chronus/changes/compiler-init-gitignore-patterns-2026-10-19-12-56-0.md @@ -5,3 +5,12 @@ packages: --- `tsp init` adds `bin/` and `obj/` to the generated `.gitignore` when the C# server emitter is selected. Init templates can list `gitignorePatterns` for each emitter to add their own lines. + +```json +"emitters": { + "@typespec/http-server-csharp": { + "label": "C# server stubs", + "gitignorePatterns": ["bin/", "obj/"] + } +} +``` diff --git a/packages/compiler/test/cli/init.test.ts b/packages/compiler/test/cli/init.test.ts index 31c284b7844..6d46907ce06 100644 --- a/packages/compiler/test/cli/init.test.ts +++ b/packages/compiler/test/cli/init.test.ts @@ -58,6 +58,7 @@ const TEST_SCAFFOLDING = { options: { "emitter-output-dir": "{output-dir}/clients/csharp", }, + gitignorePatterns: ["from-csharp-client/"], }, }, files: [], @@ -235,6 +236,31 @@ it("should support overriding emitters", async () => { expect(tspConfig.emit).toEqual(["@typespec/openapi3", "@typespec/http-client-csharp"]); }); +it("leaves out gitignore patterns of emitters that are not selected", async () => { + const { fs, init } = await createTestFSWithCliCompilerHost(); + + await init("/tmp/test-project", { + template: "foo", + "no-prompt": true, + }); + + const gitignore = fs.get("/tmp/test-project/.gitignore"); + expect(gitignore).toBeDefined(); + expect(gitignore).not.toContain("from-csharp-client/"); +}); + +it("adds gitignore patterns of emitters that are selected", async () => { + const { fs, init } = await createTestFSWithCliCompilerHost(); + + await init("/tmp/test-project", { + template: "foo", + "no-prompt": true, + emitters: ["@typespec/openapi3", "@typespec/http-client-csharp"], + }); + + expect(fs.get("/tmp/test-project/.gitignore")?.split("\n")).toContain("from-csharp-client/"); +}); + it("defaults to initialValue for parameters", async () => { const { compilerHost, init } = await createTestFSWithCliCompilerHost(); await init("/tmp/test-project", {