Skip to content

Commit

Permalink
fix(ng-update): properly handle update from different working directory
Browse files Browse the repository at this point in the history
In some situations, developers will run `ng update` from a sub directory
of the project. This currently results in a noop migration as file
system paths were computed incorrectly. This is because ng-update
always assumed that the CWD is the workspace root in the real file
system. This is not the case and therefore threw off path resolution.

We can fix this by determining the workspace file system path from
the virtual file system host tree's. This is not ideal, but best
solution for now until we can parse/resolve TypeScript projects
purely from within the virtual file system. This is currently
not easy to implement and requires helpers from TS which are
not exposed yet. See:
microsoft/TypeScript#13793.

This is tracked with: COMP-387.

Fixes angular#19779.
  • Loading branch information
devversion committed Jul 9, 2020
1 parent 0012121 commit 1f4b934
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
22 changes: 16 additions & 6 deletions src/cdk/schematics/ng-update/devkit-migration-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
import {WorkspaceProject} from '@schematics/angular/utility/workspace-models';
import {sync as globSync} from 'glob';
Expand All @@ -17,6 +17,7 @@ import {MigrationCtor} from '../update-tool/migration';
import {TargetVersion} from '../update-tool/target-version';
import {WorkspacePath} from '../update-tool/file-system';
import {getTargetTsconfigPath, getWorkspaceConfigGracefully} from '../utils/project-tsconfig-paths';
import {getWorkspaceFileSystemPath} from '../utils/workspace-fs-path';

import {DevkitFileSystem} from './devkit-file-system';
import {DevkitContext, DevkitMigration, DevkitMigrationCtor} from './devkit-migration';
Expand Down Expand Up @@ -70,12 +71,21 @@ export function createMigrationSchematicRule(
return;
}

const workspaceFsPath = getWorkspaceFileSystemPath(tree);

// The workspace path could be `null` if the CLI changed the internals of the
// virtual file system tree. We would want users to create an issue for this
// if we for some reason did not catch the internal API change.
if (workspaceFsPath === null) {
throw new SchematicsException(
'Could not determine workspace file system path. Please report an issue ' +
'in the Angular Components repository with details on your Angular CLI version.');
}

// Keep track of all project source files which have been checked/migrated. This is
// necessary because multiple TypeScript projects can contain the same source file and
// we don't want to check these again, as this would result in duplicated failure messages.
const analyzedFiles = new Set<WorkspacePath>();
// The CLI uses the working directory as the base directory for the virtual file system tree.
const workspaceFsPath = process.cwd();
const fileSystem = new DevkitFileSystem(tree, workspaceFsPath);
const projectNames = Object.keys(workspace.projects);
const migrations: NullableDevkitMigration[] = [...cdkMigrations, ...extraMigrations];
Expand Down Expand Up @@ -124,11 +134,11 @@ export function createMigrationSchematicRule(
/** Runs the migrations for the specified workspace project. */
function runMigrations(project: WorkspaceProject, projectName: string,
tsconfigPath: string, isTestTarget: boolean) {
const projectRootFsPath = join(workspaceFsPath, project.root);
const tsconfigFsPath = join(workspaceFsPath, tsconfigPath);
const projectRootFsPath = join(workspaceFsPath!, project.root);
const tsconfigFsPath = join(workspaceFsPath!, tsconfigPath);
const program = UpdateProject.createProgramFromTsconfig(tsconfigFsPath, fileSystem);
const updateContext: DevkitContext = {
workspaceFsPath,
workspaceFsPath: workspaceFsPath!,
isTestTarget,
projectName,
project,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {join} from 'path';
import {MIGRATION_PATH} from '../../../index.spec';
import {createTestCaseSetup} from '../../../testing';

describe('ng-update working directory', () => {

it('should migrate project if working directory is not workspace root', async () => {
const {runFixers, writeFile, removeTempDir, tempPath, appTree} = await createTestCaseSetup(
'migration-v6', MIGRATION_PATH, []);
const stylesheetPath = 'projects/cdk-testing/src/test-cases/global-stylesheets-test.scss';

// Write a stylesheet that will be captured by the migration.
writeFile(stylesheetPath, `
[cdkPortalHost] {
color: red;
}
`);

// We want to switch into a given project directory. This means that the devkit
// file system tree is at workspace root while the working directory is not the
// workspace directory as previously assumed. See the following issue for more details:
// https://github.com/angular/components/issues/19779
await runFixers(join(tempPath, 'projects/cdk-testing'));

expect(appTree.readContent(stylesheetPath)).not
.toContain('[cdkPortalHost]', 'Expected migration to change stylesheet contents.');

removeTempDir();
});
});
8 changes: 4 additions & 4 deletions src/cdk/schematics/testing/test-case-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ export async function createTestCaseSetup(migrationName: string, collectionPath:

writeFile(testAppTsconfigPath, JSON.stringify(testAppTsconfig, null, 2));

const runFixers = async function() {
// Switch to the new temporary directory to simulate that "ng update" is ran
// from within the project.
process.chdir(tempPath);
const runFixers = async function(workingDir = tempPath) {
// Switch to the given working directory. By default, switches to the workspace
// root to simulate the common `ng update` usage pattern.
process.chdir(workingDir);

// Patch "executePostTasks" to do nothing. This is necessary since
// we cannot run the node install task in unit tests. Rather we just
Expand Down
30 changes: 30 additions & 0 deletions src/cdk/schematics/utils/workspace-fs-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {getSystemPath} from '@angular-devkit/core';
import {HostDirEntry, HostTree, Tree} from '@angular-devkit/schematics';

/**
* Gets the workspace file system path from the given tree. Returns
* `null` if the path could not be determined.
*/
// TODO: Remove this once we have a fully virtual TypeScript compiler host: COMP-387
export function getWorkspaceFileSystemPath(tree: Tree): string|null {
if (!(tree.root instanceof HostDirEntry)) {
return null;
}
const hostTree = tree.root['_tree'];
if (!(hostTree instanceof HostTree) || hostTree['_backend'] === undefined) {
return null;
}
const backend = hostTree['_backend'];
if (backend['_root'] !== undefined) {
return getSystemPath(backend['_root']);
}
return null;
}

0 comments on commit 1f4b934

Please sign in to comment.