From c5daaa91cf470879fcf90c71aab7cd4a30e882f0 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Sat, 9 Mar 2019 16:58:26 +0100 Subject: [PATCH] refactor(core): static-query schematic should not run multiple times (#29133) With 6215799, we introduced a schematic for the new static-query timing. Currently when someone runs the update schematic manually within his CLI project (the schematic does not run automatically yet), he might have noticed that the migration is executed for the same `tsconfig` file multiple times. This can happen because the `getProjectTsConfigPaths` function can incorrectly return the same tsconfig multiple times. The paths are not properly deduped as we don't normalize the determined project tsconfig paths PR Close #29133 --- .../test/project_tsconfig_paths_spec.ts | 15 ++++++++++++--- packages/core/schematics/utils/BUILD.bazel | 5 ++++- .../schematics/utils/project_tsconfig_paths.ts | 11 ++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/core/schematics/test/project_tsconfig_paths_spec.ts b/packages/core/schematics/test/project_tsconfig_paths_spec.ts index b26137eb37..31cc793ae7 100644 --- a/packages/core/schematics/test/project_tsconfig_paths_spec.ts +++ b/packages/core/schematics/test/project_tsconfig_paths_spec.ts @@ -22,7 +22,7 @@ describe('project tsconfig paths', () => { {my_name: {architect: {build: {options: {tsConfig: './my-custom-config.json'}}}}} })); - expect(getProjectTsConfigPaths(testTree)).toEqual(['./my-custom-config.json']); + expect(getProjectTsConfigPaths(testTree)).toEqual(['my-custom-config.json']); }); it('should detect test tsconfig path inside of .angular.json file', () => { @@ -32,7 +32,7 @@ describe('project tsconfig paths', () => { {with_tests: {architect: {test: {options: {tsConfig: './my-test-config.json'}}}}} })); - expect(getProjectTsConfigPaths(testTree)).toEqual(['./my-test-config.json']); + expect(getProjectTsConfigPaths(testTree)).toEqual(['my-test-config.json']); }); it('should detect common tsconfigs if no workspace config could be found', () => { @@ -41,7 +41,16 @@ describe('project tsconfig paths', () => { testTree.create('/src/tsconfig.app.json', ''); expect(getProjectTsConfigPaths(testTree)).toEqual([ - './tsconfig.json', './src/tsconfig.json', './src/tsconfig.app.json' + 'tsconfig.json', 'src/tsconfig.json', 'src/tsconfig.app.json' ]); }); + + it('should not return duplicate tsconfig files', () => { + testTree.create('/tsconfig.json', ''); + testTree.create('/.angular.json', JSON.stringify({ + projects: {app: {architect: {test: {options: {tsConfig: 'tsconfig.json'}}}}} + })); + + expect(getProjectTsConfigPaths(testTree)).toEqual(['tsconfig.json']); + }); }); diff --git a/packages/core/schematics/utils/BUILD.bazel b/packages/core/schematics/utils/BUILD.bazel index cffc353f4d..df0004600e 100644 --- a/packages/core/schematics/utils/BUILD.bazel +++ b/packages/core/schematics/utils/BUILD.bazel @@ -8,5 +8,8 @@ ts_library( ), tsconfig = "//packages/core/schematics:tsconfig.json", visibility = ["//packages/core/schematics:__subpackages__"], - deps = ["@npm//@angular-devkit/schematics"], + deps = [ + "@npm//@angular-devkit/core", + "@npm//@angular-devkit/schematics", + ], ) diff --git a/packages/core/schematics/utils/project_tsconfig_paths.ts b/packages/core/schematics/utils/project_tsconfig_paths.ts index 4623f012af..a6ef3219c7 100644 --- a/packages/core/schematics/utils/project_tsconfig_paths.ts +++ b/packages/core/schematics/utils/project_tsconfig_paths.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ +import {normalize} from '@angular-devkit/core'; import {Tree} from '@angular-devkit/schematics'; /** Name of the default Angular CLI workspace configuration files. */ @@ -18,9 +19,9 @@ const defaultWorkspaceConfigPaths = ['/angular.json', '/.angular.json']; export function getProjectTsConfigPaths(tree: Tree): string[] { // Start with some tsconfig paths that are generally used within CLI projects. const tsconfigPaths = new Set([ - './tsconfig.json', - './src/tsconfig.json', - './src/tsconfig.app.json', + 'tsconfig.json', + 'src/tsconfig.json', + 'src/tsconfig.app.json', ]); // Add any tsconfig directly referenced in a build or test task of the angular.json workspace. @@ -32,13 +33,13 @@ export function getProjectTsConfigPaths(tree: Tree): string[] { ['build', 'test'].forEach(targetName => { if (project.targets && project.targets[targetName] && project.targets[targetName].options && project.targets[targetName].options.tsConfig) { - tsconfigPaths.add(project.targets[targetName].options.tsConfig); + tsconfigPaths.add(normalize(project.targets[targetName].options.tsConfig)); } if (project.architect && project.architect[targetName] && project.architect[targetName].options && project.architect[targetName].options.tsConfig) { - tsconfigPaths.add(project.architect[targetName].options.tsConfig); + tsconfigPaths.add(normalize(project.architect[targetName].options.tsConfig)); } }); }