forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ng-update): properly handle update from different working directory
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
1 parent
0012121
commit 1f4b934
Showing
4 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/cdk/schematics/ng-update/test-cases/misc/working-directory.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |