fix(compiler-cli): do not add references to files outside of `rootDir` (#19770)

References to resources (such as .css files) that are generated into
the `outDir` directory outside of `rootDir` would cause a spurious
compiler error about not being able to find a files that ends in
'.ngstyle.ts'.

Also fixed a minor issue in compiler error reporting

Fixes: #19765, #19767

PR Close #19770
This commit is contained in:
Chuck Jazdzewski 2017-10-17 16:10:15 -07:00 committed by Tobias Bosch
parent 3861ba2929
commit 25cbc98979
4 changed files with 46 additions and 3 deletions

View File

@ -363,7 +363,8 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter extends
genFileNames = cachedGenFiles;
} else {
if (!this.options.noResolve && this.shouldGenerateFilesFor(fileName)) {
genFileNames = this.codeGenerator.findGeneratedFileNames(fileName);
genFileNames = this.codeGenerator.findGeneratedFileNames(fileName).filter(
fileName => this.shouldGenerateFile(fileName).generate);
}
this.generatedCodeFor.set(fileName, genFileNames);
}

View File

@ -1350,4 +1350,46 @@ describe('ngc transformer command-line', () => {
it('should recompile when the css file changes',
expectRecompile(() => { write('greet.css', `p.greeting { color: blue }`); }));
});
describe('regressions', () => {
// #19765
it('should not report an error when the resolved .css file is in outside rootDir', () => {
write('src/tsconfig.json', `{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"outDir": "../dist",
"rootDir": ".",
"rootDirs": [
".",
"../dist"
]
},
"files": ["test-module.ts"]
}`);
write('src/lib/test.component.ts', `
import {Component} from '@angular/core';
@Component({
template: '<p>hello</p>',
styleUrls: ['./test.component.css']
})
export class TestComponent {}
`);
write('dist/dummy.txt', ''); // Force dist to be created
write('dist/lib/test.component.css', `
p { color: blue }
`);
write('src/test-module.ts', `
import {NgModule} from '@angular/core';
import {TestComponent} from './lib/test.component';
@NgModule({declarations: [TestComponent]})
export class TestModule {}
`);
const messages: string[] = [];
const exitCode =
main(['-p', path.join(basePath, 'src/tsconfig.json')], message => messages.push(message));
expect(exitCode).toBe(0, 'Compile failed unexpectedly.\n ' + messages.join('\n '));
});
});
});

View File

@ -252,10 +252,10 @@ describe('NgCompilerHost', () => {
it('should clear old generated references if the original host cached them', () => {
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
const ngHost = createNgHost();
const sfText = `
/// <reference path="main.ts"/>
`;
const ngHost = createNgHost({files: {'tmp': {'src': {'index.ts': sfText}}}});
const sf = ts.createSourceFile('/tmp/src/index.ts', sfText, ts.ScriptTarget.Latest);
ngHost.getSourceFile = () => sf;

View File

@ -113,7 +113,7 @@ export class AotCompiler {
compMeta.template !.styleUrls.forEach((styleUrl) => {
const normalizedUrl = this._host.resourceNameToFileName(styleUrl, file.fileName);
if (!normalizedUrl) {
throw new Error(`Couldn't resolve resource ${styleUrl} relative to ${file.fileName}`);
throw syntaxError(`Couldn't resolve resource ${styleUrl} relative to ${file.fileName}`);
}
const needsShim = (compMeta.template !.encapsulation ||
this._config.defaultEncapsulation) === ViewEncapsulation.Emulated;