Skip to content

fix(ng-update): properly handle update from different working directory #19933

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

Closed
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the TODO should go above the JsDoc block

export function getWorkspaceFileSystemPath(tree: Tree): string|null {
if (!(tree.root instanceof HostDirEntry)) {
return null;
}
const hostTree = tree.root['_tree'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment about this private accesses in this function?

if (!(hostTree instanceof HostTree) || hostTree['_backend'] === undefined) {
return null;
}
const backend = hostTree['_backend'];
if (backend['_root'] !== undefined) {
return getSystemPath(backend['_root']);
}
return null;
}