Skip to content

fix(@schematics/angular): resolve windows paths in tsconfig migration #16807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2020
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
Expand Up @@ -7,14 +7,20 @@
*/
import { JsonAstObject, logging } from '@angular-devkit/core';
import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics';
import { posix } from 'path';
import { dirname, relative } from 'path';
import {
findPropertyInAstObject,
insertPropertyInAstObjectInOrder,
removePropertyInAstObject,
} from '../../utility/json-utils';
import { Builders } from '../../utility/workspace-models';
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
import {
forwardSlashPath,
getAllOptions,
getTargets,
getWorkspace,
readJsonFileAsAstObject,
} from './utils';

/**
* Update the tsconfig files for applications
Expand Down Expand Up @@ -107,15 +113,18 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
const files = findPropertyInAstObject(tsConfigAst, 'files');
if (!files) {
const newFiles: string[] = [];
const tsConfigDir = dirname(forwardSlashPath(tsConfigPath));

const mainOption = findPropertyInAstObject(option, 'main');
if (mainOption && mainOption.kind === 'string') {
newFiles.push(posix.relative(posix.dirname(tsConfigPath), mainOption.value));
newFiles.push(
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(mainOption.value))));
}

const polyfillsOption = findPropertyInAstObject(option, 'polyfills');
if (polyfillsOption && polyfillsOption.kind === 'string') {
newFiles.push(posix.relative(posix.dirname(tsConfigPath), polyfillsOption.value));
newFiles.push(
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(polyfillsOption.value))));
}

if (newFiles.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { getWorkspaceTargets, updateWorkspaceTargets } from './update-workspace-config_spec';

// tslint:disable-next-line: no-any
function overrideJsonFile(tree: UnitTestTree, path: string, newContent: object) {
Expand Down Expand Up @@ -64,6 +65,32 @@ describe('Migration to version 9', () => {
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
});

it('should resolve paths correctly even if they are using windows separators', async () => {
const tree2 = await schematicRunner
.runExternalSchematicAsync(
require.resolve('../../collection.json'),
'application',
{
name: 'another-app',
},
tree,
)
.toPromise();

const tsCfgPath = 'projects/another-app/tsconfig.app.json';
overrideJsonFile(tree2, tsCfgPath, defaultTsConfigOptions);
const config = getWorkspaceTargets(tree2, 'another-app');
config.build.options.main = 'projects\\another-app\\src\\main.ts';
config.build.options.polyfills = 'projects\\another-app\\src\\polyfills.ts';
config.build.options.tsConfig = 'projects\\another-app\\tsconfig.app.json';
updateWorkspaceTargets(tree2, config, 'another-app');

const tree3 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree2.branch()).toPromise();
const { exclude, files } = JSON.parse(tree3.readContent(tsCfgPath));
expect(exclude).toBeUndefined();
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
});

it('should update apps tsConfig when tsconfig has include', async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import { WorkspaceTargets } from '../../utility/workspace-models';
import { ANY_COMPONENT_STYLE_BUDGET } from './update-workspace-config';

// tslint:disable-next-line: no-any
function getWorkspaceTargets(tree: UnitTestTree): any {
return JSON.parse(tree.readContent(workspacePath))
.projects['migration-test'].architect;
export function getWorkspaceTargets(tree: UnitTestTree, project = 'migration-test'): any {
return JSON.parse(tree.readContent(workspacePath)).projects[project].architect;
}

function updateWorkspaceTargets(tree: UnitTestTree, workspaceTargets: WorkspaceTargets) {
export function updateWorkspaceTargets(
tree: UnitTestTree,
workspaceTargets: WorkspaceTargets,
project = 'migration-test',
) {
const config = JSON.parse(tree.readContent(workspacePath));
config.projects['migration-test'].architect = workspaceTargets;
config.projects[project].architect = workspaceTargets;
tree.overwrite(workspacePath, JSON.stringify(config, undefined, 2));
}

Expand Down
6 changes: 6 additions & 0 deletions packages/schematics/angular/migrations/update-9/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,9 @@ export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean {

return true;
}

// TS represents paths internally with '/' and expects paths to be in this format.
// angular.json expects paths with '/', but doesn't enforce them.
export function forwardSlashPath(path: string) {
return path.replace(/\\/g, '/');
}