From 56945a0615e05bbd2b8489bba8a4fea4300f1147 Mon Sep 17 00:00:00 2001 From: rk Date: Mon, 2 Nov 2026 15:13:47 +0500 Subject: [PATCH] fix(@schematics/angular): keep existing worker tsconfig when generating another web worker second `ng g web-worker` failed with `A merge conflicted on path "/tsconfig.worker.json"` in any workspace from `ng new` since 21.2.0, CLI formats first file with prettier and it no longer matches template config is shared by all workers of project, so it is now written only when file is missing --- packages/schematics/angular/web-worker/index.ts | 7 ++++--- .../schematics/angular/web-worker/index_spec.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/schematics/angular/web-worker/index.ts b/packages/schematics/angular/web-worker/index.ts index 9718f0407c39..b42f1c877d51 100644 --- a/packages/schematics/angular/web-worker/index.ts +++ b/packages/schematics/angular/web-worker/index.ts @@ -76,7 +76,7 @@ if (typeof Worker !== 'undefined') { } const webWorkerSchematic: RuleFactory = createProjectSchematic( - (options, { project }) => { + (options, { project, tree }) => { const projectType = project.extensions['projectType']; if (projectType !== 'application') { throw new SchematicsException(`Web Worker requires a project type of "application".`); @@ -95,6 +95,7 @@ const webWorkerSchematic: RuleFactory = createProjectSchematic ]); const root = project.root || ''; + const workerConfigPath = join(root, 'tsconfig.worker.json'); const templateSourceWorkerConfig = apply(url('./files/worker-tsconfig'), [ applyTemplates({ ...options, @@ -114,7 +115,6 @@ const webWorkerSchematic: RuleFactory = createProjectSchematic throw new Error(`Build target is not defined for this project.`); } - const workerConfigPath = join(root, 'tsconfig.worker.json'); (buildTarget.options ??= {}).webWorkerTsConfig ??= workerConfigPath; // The unit-test builder has no `webWorkerTsConfig` option and reads it from the build target. if (testTarget && testTarget.builder !== Builders.BuildUnitTest) { @@ -125,7 +125,8 @@ const webWorkerSchematic: RuleFactory = createProjectSchematic options.snippet ? addSnippet(options) : noop(), // Add the worker. mergeWith(templateSourceWorkerCode), - mergeWith(templateSourceWorkerConfig), + // The configuration is shared by all the workers of the project. + tree.exists(workerConfigPath) ? noop() : mergeWith(templateSourceWorkerConfig), ]); }, ); diff --git a/packages/schematics/angular/web-worker/index_spec.ts b/packages/schematics/angular/web-worker/index_spec.ts index 0f59ed82f0b9..c100bda5b5e1 100644 --- a/packages/schematics/angular/web-worker/index_spec.ts +++ b/packages/schematics/angular/web-worker/index_spec.ts @@ -147,4 +147,21 @@ describe('Web Worker Schematic', () => { const { compilerOptions } = parseJson(tree.readContent(path).toString()); expect(compilerOptions.outDir).toBe('./out-tsc/worker'); }); + + it(`should keep the existing 'tsconfig.worker.json' when adding another worker`, async () => { + let tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree); + // The file no longer matches the template once it was customized or formatted by the CLI. + const path = '/projects/bar/tsconfig.worker.json'; + const customized = `{ "compilerOptions": { "lib": ["es2022", "webworker"] } }\n`; + tree.overwrite(path, customized); + + tree = await schematicRunner.runSchematic( + 'web-worker', + { ...defaultOptions, name: 'other' }, + tree, + ); + + expect(tree.exists('/projects/bar/src/app/other.worker.ts')).toBeTrue(); + expect(tree.readContent(path)).toBe(customized); + }); });