Skip to content

Commit 0d1f3c3

Browse files
authored
fix: support trailing slash in basePath (angular#10533)
1 parent 83e2d3d commit 0d1f3c3

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

modules/@angular/compiler-cli/src/reflector_host.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
3030
private metadataCollector = new MetadataCollector();
3131
private context: ReflectorHostContext;
3232
private isGenDirChildOfRootDir: boolean;
33+
private basePath: string;
34+
private genDir: string;
3335
constructor(
3436
private program: ts.Program, private compilerHost: ts.CompilerHost,
3537
private options: AngularCompilerOptions, context?: ReflectorHostContext) {
38+
// normalize the path so that it never ends with '/'.
39+
this.basePath = path.normalize(path.join(this.options.basePath, '.'));
40+
this.genDir = path.normalize(path.join(this.options.genDir, '.'));
41+
3642
this.context = context || new NodeReflectorHostContext();
37-
var genPath: string = path.relative(options.basePath, options.genDir);
43+
var genPath: string = path.relative(this.basePath, this.genDir);
3844
this.isGenDirChildOfRootDir = genPath === '' || !genPath.startsWith('..');
3945
}
4046

@@ -108,7 +114,7 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
108114
// rewrite to genDir path
109115
if (importModule) {
110116
// it is generated, therefore we do a relative path to the factory
111-
return this.dotRelative(containingDir, this.options.genDir + NODE_MODULES + importModule);
117+
return this.dotRelative(containingDir, this.genDir + NODE_MODULES + importModule);
112118
} else {
113119
// assume that import is also in `genDir`
114120
importedFile = this.rewriteGenDirPath(importedFile);
@@ -121,7 +127,7 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
121127
} else {
122128
if (!this.isGenDirChildOfRootDir) {
123129
// assume that they are on top of each other.
124-
importedFile = importedFile.replace(this.options.basePath, this.options.genDir);
130+
importedFile = importedFile.replace(this.basePath, this.genDir);
125131
}
126132
return this.dotRelative(containingDir, importedFile);
127133
}
@@ -140,11 +146,11 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
140146
var nodeModulesIndex = filepath.indexOf(NODE_MODULES);
141147
if (nodeModulesIndex !== -1) {
142148
// If we are in node_modulse, transplant them into `genDir`.
143-
return path.join(this.options.genDir, filepath.substring(nodeModulesIndex));
149+
return path.join(this.genDir, filepath.substring(nodeModulesIndex));
144150
} else {
145151
// pretend that containing file is on top of the `genDir` to normalize the paths.
146152
// we apply the `genDir` => `rootDir` delta through `rootDirPrefix` later.
147-
return filepath.replace(this.options.basePath, this.options.genDir);
153+
return filepath.replace(this.basePath, this.genDir);
148154
}
149155
}
150156

@@ -156,7 +162,7 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
156162
throw new Error('Resolution of relative paths requires a containing file.');
157163
}
158164
// Any containing file gives the same result for absolute imports
159-
containingFile = path.join(this.options.basePath, 'index.ts');
165+
containingFile = path.join(this.basePath, 'index.ts');
160166
}
161167

162168
try {

modules/@angular/compiler-cli/test/reflector_host_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('reflector_host', () => {
3535
}
3636
reflectorNestedGenDir = new ReflectorHost(
3737
program, host, {
38-
genDir: '/tmp/project/src/gen',
38+
genDir: '/tmp/project/src/gen/',
3939
basePath: '/tmp/project/src',
4040
skipMetadataEmit: false,
4141
skipTemplateCodegen: false,
@@ -45,7 +45,7 @@ describe('reflector_host', () => {
4545
reflectorSiblingGenDir = new ReflectorHost(
4646
program, host, {
4747
genDir: '/tmp/project/gen',
48-
basePath: '/tmp/project/src',
48+
basePath: '/tmp/project/src/',
4949
skipMetadataEmit: false,
5050
skipTemplateCodegen: false,
5151
trace: false

0 commit comments

Comments
 (0)