Skip to content

Commit

Permalink
fix(migrations): normalize paths to posix (angular#48850)
Browse files Browse the repository at this point in the history
Both TypeScript and Angular Schematic rely on posix system paths which can cause issues on Windows if paths are not normalized correctly.

Such as `sourceFile.fileName.startsWith(pathToMigrate)` on Windows will always return falsey.

PR Close angular#48850
  • Loading branch information
alan-agius4 authored and trekladyone committed Feb 1, 2023
1 parent 139f451 commit aae7f2f
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ interface Options {
mode: MigrationMode;
}

const normalizePath = (path: string): string => path.replace(/\\/g, '/');

export default function(options: Options): Rule {
return async (tree) => {
const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree);
Expand Down Expand Up @@ -59,7 +61,11 @@ function standaloneMigration(tree: Tree, tsconfigPath: string, basePath: string,
options: {_enableTemplateTypeChecker: true, compileNonExportedClasses: true}
}) as NgtscProgram;
const printer = ts.createPrinter();
const pathToMigrate = join(basePath, options.path);

// TS and Schematic use paths in POSIX format even on Windows.
// This is needed as otherwise string matching such as
// `sourceFile.fileName.startsWith(pathToMigrate)` will not work correctly.
const pathToMigrate = normalizePath(join(basePath, options.path));

if (existsSync(pathToMigrate) && !statSync(pathToMigrate).isDirectory()) {
throw new SchematicsException(`Migration path ${
Expand Down

0 comments on commit aae7f2f

Please sign in to comment.