Skip to content

Commit d0037b2

Browse files
JoostKkara
authored andcommitted
fix(ivy): correct ngtsc path handling in Windows (angular#26703)
As it turns out, the usage of path.posix does not unify path handling across operating systems. Instead, canonical-path is used to ensure path handling is consistent, avoiding incorrect paths in Windows. See angular#25862 (comment) PR Close angular#26703
1 parent d568d7a commit d0037b2

File tree

10 files changed

+18
-15
lines changed

10 files changed

+18
-15
lines changed

packages/compiler-cli/src/ngtsc/annotations/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ts_library(
1717
"//packages/compiler-cli/src/ngtsc/transform",
1818
"//packages/compiler-cli/src/ngtsc/typecheck",
1919
"@ngdeps//@types/node",
20+
"@ngdeps//canonical-path",
2021
"@ngdeps//typescript",
2122
],
2223
)

packages/compiler-cli/src/ngtsc/annotations/src/component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ConstantPool, CssSelector, Expression, R3ComponentMetadata, R3DirectiveMetadata, SelectorMatcher, Statement, TmplAstNode, WrappedNodeExpr, compileComponentFromMetadata, makeBindingParser, parseTemplate} from '@angular/compiler';
10-
import * as path from 'path';
10+
import * as path from 'canonical-path';
1111
import * as ts from 'typescript';
1212

1313
import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
@@ -62,7 +62,7 @@ export class ComponentDecoratorHandler implements
6262
throw new FatalDiagnosticError(
6363
ErrorCode.VALUE_HAS_WRONG_TYPE, templateUrlExpr, 'templateUrl must be a string');
6464
}
65-
const url = path.posix.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
65+
const url = path.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
6666
return this.resourceLoader.preload(url);
6767
}
6868
return undefined;
@@ -94,7 +94,7 @@ export class ComponentDecoratorHandler implements
9494
throw new FatalDiagnosticError(
9595
ErrorCode.VALUE_HAS_WRONG_TYPE, templateUrlExpr, 'templateUrl must be a string');
9696
}
97-
const url = path.posix.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
97+
const url = path.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
9898
templateStr = this.resourceLoader.load(url);
9999
} else if (component.has('template')) {
100100
const templateExpr = component.get('template') !;
@@ -128,7 +128,7 @@ export class ComponentDecoratorHandler implements
128128
// relative path representation.
129129
const filePath = node.getSourceFile().fileName;
130130
const relativeFilePath = this.rootDirs.reduce<string|undefined>((previous, rootDir) => {
131-
const candidate = path.posix.relative(rootDir, filePath);
131+
const candidate = path.relative(rootDir, filePath);
132132
if (previous === undefined || candidate.length < previous.length) {
133133
return candidate;
134134
} else {

packages/compiler-cli/src/ngtsc/metadata/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ts_library(
1515
"//packages/compiler-cli/src/ngtsc/host",
1616
"//packages/compiler-cli/src/ngtsc/util",
1717
"@ngdeps//@types/node",
18+
"@ngdeps//canonical-path",
1819
"@ngdeps//typescript",
1920
],
2021
)

packages/compiler-cli/src/ngtsc/metadata/src/resolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
import {Expression, ExternalExpr, ExternalReference, WrappedNodeExpr} from '@angular/compiler';
15-
import * as path from 'path';
15+
import * as path from 'canonical-path';
1616
import * as ts from 'typescript';
1717

1818
import {ClassMemberKind, ReflectionHost} from '../../host';
@@ -155,7 +155,7 @@ export class ResolvedReference<T extends ts.Node = ts.Node> extends Reference<T>
155155
// TODO(alxhub): investigate the impact of multiple source roots here.
156156
// TODO(alxhub): investigate the need to map such paths via the Host for proper g3 support.
157157
let relative =
158-
path.posix.relative(path.dirname(context.fileName), this.node.getSourceFile().fileName)
158+
path.relative(path.dirname(context.fileName), this.node.getSourceFile().fileName)
159159
.replace(TS_DTS_JS_EXTENSION, '');
160160

161161
// path.relative() does not include the leading './'.

packages/compiler-cli/src/ngtsc/shims/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ts_library(
1515
"//packages/compiler-cli/src/ngtsc/metadata",
1616
"//packages/compiler-cli/src/ngtsc/util",
1717
"@ngdeps//@types/node",
18+
"@ngdeps//canonical-path",
1819
"@ngdeps//typescript",
1920
],
2021
)

packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import * as path from 'path';
9+
import * as path from 'canonical-path';
1010
import * as ts from 'typescript';
1111

1212
import {relativePathBetween} from '../../util/src/path';
@@ -29,8 +29,7 @@ export class FactoryGenerator implements ShimGenerator {
2929
getOriginalSourceOfShim(fileName: string): string|null { return this.map.get(fileName) || null; }
3030

3131
generate(original: ts.SourceFile, genFilePath: string): ts.SourceFile {
32-
const relativePathToSource =
33-
'./' + path.posix.basename(original.fileName).replace(TS_DTS_SUFFIX, '');
32+
const relativePathToSource = './' + path.basename(original.fileName).replace(TS_DTS_SUFFIX, '');
3433
// Collect a list of classes that need to have factory types emitted for them. This list is
3534
// overly broad as at this point the ts.TypeChecker hasn't been created, and can't be used to
3635
// semantically understand which decorated types are actually decorated with Angular decorators.

packages/compiler-cli/src/ngtsc/testing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ts_library(
1010
]),
1111
deps = [
1212
"//packages:types",
13+
"@ngdeps//canonical-path",
1314
"@ngdeps//typescript",
1415
],
1516
)

packages/compiler-cli/src/ngtsc/testing/in_memory_typescript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
///<reference types="jasmine"/>
1010

11-
import * as path from 'path';
11+
import * as path from 'canonical-path';
1212
import * as ts from 'typescript';
1313

1414
export function makeProgram(
@@ -93,7 +93,7 @@ export class InMemoryHost implements ts.CompilerHost {
9393
}
9494

9595
getCanonicalFileName(fileName: string): string {
96-
return path.posix.normalize(`${this.getCurrentDirectory()}/${fileName}`);
96+
return path.normalize(`${this.getCurrentDirectory()}/${fileName}`);
9797
}
9898

9999
useCaseSensitiveFileNames(): boolean { return true; }

packages/compiler-cli/src/ngtsc/util/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ load("//tools:defaults.bzl", "ts_library")
55
ts_library(
66
name = "util",
77
srcs = glob([
8-
"index.ts",
98
"src/**/*.ts",
109
]),
1110
module_name = "@angular/compiler-cli/src/ngtsc/util",
1211
deps = [
1312
"//packages:types",
1413
"@ngdeps//@types/node",
14+
"@ngdeps//canonical-path",
1515
"@ngdeps//typescript",
1616
],
1717
)

packages/compiler-cli/src/ngtsc/util/src/path.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
/// <reference types="node" />
1010

11-
import * as path from 'path';
11+
import * as path from 'canonical-path';
1212

1313
const TS_DTS_EXTENSION = /(\.d)?\.ts$/;
1414

1515
export function relativePathBetween(from: string, to: string): string|null {
16-
let relative = path.posix.relative(path.dirname(from), to).replace(TS_DTS_EXTENSION, '');
16+
let relative = path.relative(path.dirname(from), to).replace(TS_DTS_EXTENSION, '');
1717

1818
if (relative === '') {
1919
return null;
@@ -25,4 +25,4 @@ export function relativePathBetween(from: string, to: string): string|null {
2525
}
2626

2727
return relative;
28-
}
28+
}

0 commit comments

Comments
 (0)