Skip to content

Commit 5814355

Browse files
alxhubIgorMinar
authored andcommitted
fix(compiler-cli): strictMetadataEmit should not break on non-compliant libraries (#23275)
rxjs 6.0.0 breaks strictMetadataEmit as they now publish a .d.ts file with a structure like: declare export class Subscription { static EMPTY: Subscription; } This generates metadata which contains an error, and fails the strictMetadataEmit validation. There is nothing a library author can do in this situation except to set strictMetadataEmit to false. The spirit of strictMetadataEmit is to validate that the author's library doesn't do anything that will break downstream users. This failure is a corner case which causes more harm than good, so this commit disables validation for metadata collected from .d.ts files. Fixes #22210 PR Close #23275
1 parent 60e8392 commit 5814355

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/compiler-cli/src/transformers/metadata_cache.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as ts from 'typescript';
1111
import {MetadataCollector, MetadataValue, ModuleMetadata} from '../metadata/index';
1212

1313
import {MetadataProvider} from './compiler_host';
14+
import {TS} from './util';
1415

1516
export type ValueTransform = (value: MetadataValue, node: ts.Node) => MetadataValue;
1617

@@ -26,7 +27,7 @@ export class MetadataCache implements MetadataProvider {
2627
private metadataCache = new Map<string, ModuleMetadata|undefined>();
2728

2829
constructor(
29-
private collector: MetadataCollector, private strict: boolean,
30+
private collector: MetadataCollector, private readonly strict: boolean,
3031
private transformers: MetadataTransformer[]) {
3132
for (let transformer of transformers) {
3233
if (transformer.connect) {
@@ -59,7 +60,8 @@ export class MetadataCache implements MetadataProvider {
5960
}
6061
}
6162

62-
const result = this.collector.getMetadata(sourceFile, this.strict, substitute);
63+
const isTsFile = TS.test(sourceFile.fileName);
64+
const result = this.collector.getMetadata(sourceFile, this.strict && isTsFile, substitute);
6365
this.metadataCache.set(sourceFile.fileName, result);
6466
return result;
6567
}

packages/compiler-cli/test/ngc_spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -2420,4 +2420,31 @@ describe('ngc transformer command-line', () => {
24202420
expect(moduleSource).toMatch(/inject\(i0\.INJECTOR/);
24212421
});
24222422
});
2423+
2424+
it('libraries should not break strictMetadataEmit', () => {
2425+
// first only generate .d.ts / .js / .metadata.json files
2426+
writeConfig(`{
2427+
"extends": "./tsconfig-base.json",
2428+
"angularCompilerOptions": {
2429+
"skipTemplateCodegen": true,
2430+
"strictMetadataEmit": true,
2431+
"fullTemplateTypeCheck": true
2432+
},
2433+
"compilerOptions": {
2434+
"outDir": "lib"
2435+
},
2436+
"files": ["main.ts", "test.d.ts"]
2437+
}`);
2438+
write('main.ts', `
2439+
import {Test} from './test';
2440+
export const bar = Test.bar;
2441+
`);
2442+
write('test.d.ts', `
2443+
declare export class Test {
2444+
static bar: string;
2445+
}
2446+
`);
2447+
let exitCode = main(['-p', path.join(basePath, 'tsconfig.json')], errorSpy);
2448+
expect(exitCode).toEqual(0);
2449+
});
24232450
});

0 commit comments

Comments
 (0)