Skip to content

Commit

Permalink
fix(ngcc): handle missing sources when flattening source-maps (#35718)
Browse files Browse the repository at this point in the history
If a package has a source-map but it does not provide
the actual content of the sources, then the source-map
flattening was crashing.

Now we ignore such mappings that have no source
since we are not able to compute the merged
mapping if there is no source file.

Fixes #35709

PR Close #35718
  • Loading branch information
petebacondarwin authored and matsko committed Feb 27, 2020
1 parent bf6fbf5 commit 7ff845b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/compiler-cli/ngcc/src/sourcemaps/source_file.ts
Expand Up @@ -290,11 +290,16 @@ export function parseMappings(
const generatedLineMappings = rawMappings[generatedLine];
for (const rawMapping of generatedLineMappings) {
if (rawMapping.length >= 4) {
const originalSource = sources[rawMapping[1] !];
if (originalSource === null || originalSource === undefined) {
// the original source is missing so ignore this mapping
continue;
}
const generatedColumn = rawMapping[0];
const name = rawMapping.length === 5 ? rawMap.names[rawMapping[4]] : undefined;
const mapping: Mapping = {
generatedSegment: {line: generatedLine, column: generatedColumn},
originalSource: sources[rawMapping[1] !] !,
originalSource,
originalSegment: {line: rawMapping[2] !, column: rawMapping[3] !}, name
};
mappings.push(mapping);
Expand Down
22 changes: 22 additions & 0 deletions packages/compiler-cli/ngcc/test/sourcemaps/source_file_spec.ts
Expand Up @@ -174,6 +174,28 @@ runInEachFileSystem(() => {
},
]);
});

it('should ignore mappings to missing source files', () => {
const bSourceMap: RawSourceMap = {
mappings: encode([[[1, 0, 0, 0], [4, 0, 0, 3], [4, 0, 0, 6], [5, 0, 0, 7]]]),
names: [],
sources: ['c.js'],
version: 3
};
const bSource = new SourceFile(_('/foo/src/b.js'), 'abcdef', bSourceMap, false, [null]);
const aSourceMap: RawSourceMap = {
mappings: encode([[[0, 0, 0, 0], [2, 0, 0, 3], [4, 0, 0, 2], [5, 0, 0, 5]]]),
names: [],
sources: ['b.js'],
version: 3
};
const aSource =
new SourceFile(_('/foo/src/a.js'), 'abdecf', aSourceMap, false, [bSource]);

// These flattened mappings are just the mappings from a to b.
// (The mappings to c are dropped since there is no source file to map to.)
expect(aSource.flattenedMappings).toEqual(parseMappings(aSourceMap, [bSource]));
});
});

describe('renderFlattenedSourceMap()', () => {
Expand Down

0 comments on commit 7ff845b

Please sign in to comment.