Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix(cdk/schematics): avoid runtime errors thrown by devkit tree (#22982)
TypeScript resolves modules using a rather complicated module resolution algorithm. The algorithm tries various paths to determine a possible entry-point for a module. e.g. it also respects a containing `package.json` file, or respects the closest `node_modules` parent directory. In some situations, TypeScript could end up trying a path where a parent directory segment resolves to an existent file. e.g. consider the following directory structure: ``` node_modules/my-pkg/package.json node_modules/my-pkg/styles.css ``` TypeScript could end up trying a path like: `node_modules/my-pkg/styles.css/package.json` or `node_modules/my-pkg/styles.css/a/b/package.json`. This depends on how the module resolution executes, and how the module is referenced. In the example above though, TypeScript checks if the files exist. Our update logic delegates this check to our virtual file system. The virtual file system currently would throw an error by accident as it walks up the path and discovers that `styles.css` is not a directory, _but_ a file. This results in an error as seen in #22919. Fixes #22919.
- Loading branch information
1 parent
3c140fd
commit 805d3ae
Showing
6 changed files
with
54 additions
and
32 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
24 changes: 24 additions & 0 deletions
24
src/cdk/schematics/ng-update/test-cases/misc/module-resolution.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,24 @@ | ||
import {MIGRATION_PATH} from '../../../paths'; | ||
import {createTestCaseSetup} from '../../../testing'; | ||
|
||
describe('ng update typescript program module resolution', () => { | ||
|
||
// Regression test for: https://github.com/angular/components/issues/22919. | ||
it('should not error if module resolution tries a non-existent path where a path segment ' + | ||
'matches an existing file', async () => { | ||
const {runFixers, writeFile} = await createTestCaseSetup( | ||
'migration-v6', MIGRATION_PATH, []); | ||
|
||
writeFile('/node_modules/some-other-module/package.json', `{}`); | ||
writeFile('/node_modules/some-other-module/styles.css', '') | ||
|
||
// We add an import to a non-existent sub-path of `some-other-module/styles`. The TypeScript | ||
// module resolution logic could try various sub-paths. This previously resulted in an error | ||
// as the devkit tree `getDir` logic accidentally walked up the path and threw an error if | ||
// a path segment is an actual file. | ||
writeFile('/projects/cdk-testing/src/main.ts', | ||
`import 'some-other-module/styles.css/non/existent';`); | ||
|
||
await expectAsync(runFixers()).toBeResolved(); | ||
}); | ||
}); |
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
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