Skip to content

Commit bdbf21d

Browse files
crisbetopkozlowski-opensource
authored andcommitted
fix(migrations): avoid generating imports with forward slashes (#48993)
We're using Node's `path` utilities to remap existing imports and create new ones which can yield paths with forward slash separators. These changes add some logic to ensure that we only generate forward slashes. PR Close #48993
1 parent c7926b5 commit bdbf21d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

packages/core/schematics/ng-generate/standalone-migration/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {canMigrateFile, createProgramOptions} from '../../utils/typescript/compi
1818
import {pruneNgModules} from './prune-modules';
1919
import {toStandaloneBootstrap} from './standalone-bootstrap';
2020
import {toStandalone} from './to-standalone';
21-
import {ChangesByFile} from './util';
21+
import {ChangesByFile, normalizePath} from './util';
2222

2323
enum MigrationMode {
2424
toStandalone = 'convert-to-standalone',
@@ -31,8 +31,6 @@ interface Options {
3131
mode: MigrationMode;
3232
}
3333

34-
const normalizePath = (path: string): string => path.replace(/\\/g, '/');
35-
3634
export default function(options: Options): Rule {
3735
return async (tree) => {
3836
const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree);

packages/core/schematics/ng-generate/standalone-migration/util.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,16 @@ export class ChangeTracker {
107107
addImport(
108108
sourceFile: ts.SourceFile, symbolName: string, moduleName: string,
109109
alias: string|null = null): ts.Expression {
110-
return this._importManager.addImportToSourceFile(
111-
sourceFile, symbolName,
112-
this._importRemapper ? this._importRemapper(moduleName) : moduleName, alias);
110+
if (this._importRemapper) {
111+
moduleName = this._importRemapper(moduleName);
112+
}
113+
114+
// It's common for paths to be manipulated with Node's `path` utilties which
115+
// can yield a path with back slashes. Normalize them since outputting such
116+
// paths will also cause TS to escape the forward slashes.
117+
moduleName = normalizePath(moduleName);
118+
119+
return this._importManager.addImportToSourceFile(sourceFile, symbolName, moduleName, alias);
113120
}
114121

115122
/**
@@ -257,5 +264,11 @@ export function getRelativeImportPath(fromFile: string, toFile: string): string
257264
path = './' + path;
258265
}
259266

260-
return path;
267+
// Using the Node utilities can yield paths with forward slashes on Windows.
268+
return normalizePath(path);
269+
}
270+
271+
/** Normalizes a path to use posix separators. */
272+
export function normalizePath(path: string): string {
273+
return path.replace(/\\/g, '/');
261274
}

0 commit comments

Comments
 (0)