Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ngcc): do not collect private declarations from external packages #34811

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Jan 16, 2020

Previously, while trying to build an NgccReflectionHost's privateDtsDeclarationMap, computePrivateDtsDeclarationMap() would try to collect exported declarations from all source files of the program (i.e. without checking whether they were within the target entry-point, as happens for declarations in .d.ts files).

Most of the time, that would not be a problem, because external packages would be represented as .d.ts files in the program. But when an external package had no typings, the JS files would be used instead. As a result, the ReflectionHost would try to (unnecessarilly) parse the file in order to extract exported declarations, which in turn would be harmless in most cases.

There are certain cases, though, where the ReflectionHost would throw an error, because it cannot not parse the external package's JS file. This could happen, for example, in UmdReflectionHost, which expects the file to contain exactly one statement. See #34544 for more details on a real-world failure.

This commit fixes the issue by ensuring that computePrivateDtsDeclarationMap() will only collect exported declarations from files within the target entry-point.

Jira issue: FW-1794

Fixes #34544.

For good measure, I have also tested it on the ngcc-validation repo (angular/ngcc-validation#777) and nothing broke.

@gkalpak gkalpak added type: bug/fix state: WIP target: patch This PR is targeted for the next patch release comp: ngcc labels Jan 16, 2020
@ngbot ngbot bot modified the milestone: needsTriage Jan 16, 2020
@gkalpak gkalpak force-pushed the fix-ngcc-ignore-nodejs-builtins branch from 53c2d7b to a8b07f6 Compare January 16, 2020 12:05
@mary-poppins
Copy link

You can preview a8b07f6 at https://pr34811-a8b07f6.ngbuilds.io/.

gkalpak added a commit to gkalpak/ngcc-validation that referenced this pull request Jan 16, 2020
gkalpak added a commit to gkalpak/ngcc-validation that referenced this pull request Jan 16, 2020
@gkalpak gkalpak force-pushed the fix-ngcc-ignore-nodejs-builtins branch 2 times, most recently from eb0c337 to 38e9d9e Compare January 16, 2020 18:54
@gkalpak gkalpak added action: merge The PR is ready for merge by the caretaker and removed state: WIP labels Jan 16, 2020
@mary-poppins
Copy link

You can preview 38e9d9e at https://pr34811-38e9d9e.ngbuilds.io/.

gkalpak added a commit to gkalpak/ngcc-validation that referenced this pull request Jan 16, 2020
gkalpak added a commit to gkalpak/ngcc-validation that referenced this pull request Jan 16, 2020
gkalpak added a commit to gkalpak/ngcc-validation that referenced this pull request Jan 16, 2020
@gkalpak gkalpak marked this pull request as ready for review January 16, 2020 20:39
@gkalpak gkalpak requested review from a team as code owners January 16, 2020 20:39
Copy link
Member

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of nits/questions.

Please change references to "within the entry-point" to "within the package" in the commit message. We do not (and cannot reliably) check whether source files are within a specific entry-point.

@@ -38,7 +38,7 @@ fi

# Workaround https://github.com/yarnpkg/yarn/issues/2165
# Yarn will cache file://dist URIs and not update Angular code
readonly cache=.yarn_local_cache
export readonly cache=.yarn_local_cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOC what difference does this make?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting it? It makes it available to other scripts called by this script (i.e. the ngcc/test.sh script).


loadTestFiles(TYPINGS_SRC_FILES);
loadTestFiles(TYPINGS_DTS_FILES);
const externalLibWithoutTypingsIndex = _('/an_external_lib_without_typings/index.js');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: could we move this declaration to above the TestEsm2015ReflectionHost class declaration since it is used in that class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

class TestEsm2015ReflectionHost extends Esm2015ReflectionHost {
getExportsOfModule(node: ts.Node) {
if (ts.isSourceFile(node) && (node.fileName === externalLibWithoutTypingsIndex)) {
throw new Error('Test error');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this error more descriptive. Getting a failure in a test that just says "Test error" is not so helpful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

loadTestFiles(TYPINGS_DTS_FILES);
const externalLibWithoutTypingsIndex = _('/an_external_lib_without_typings/index.js');
const bundle = makeTestBundleProgram(
getRootFiles(TYPINGS_SRC_FILES)[0], false, [externalLibWithoutTypingsIndex]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you actually need to add this as an additional file? Since it is imported from the entry-point, I would expect it to be included in the program's source files automatically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I expected too. But it is not included 😕

@petebacondarwin petebacondarwin removed the action: merge The PR is ready for merge by the caretaker label Jan 17, 2020
@petebacondarwin petebacondarwin added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jan 17, 2020
Previously, while trying to build an `NgccReflectionHost`'s
`privateDtsDeclarationMap`, `computePrivateDtsDeclarationMap()` would
try to collect exported declarations from all source files of the
program (i.e. without checking whether they were within the target
package, as happens for declarations in `.d.ts` files).

Most of the time, that would not be a problem, because external packages
would be represented as `.d.ts` files in the program. But when an
external package had no typings, the JS files would be used instead. As
a result, the `ReflectionHost` would try to (unnecessarilly) parse the
file in order to extract exported declarations, which in turn would be
harmless in most cases.

There are certain cases, though, where the `ReflectionHost` would throw
an error, because it cannot parse the external package's JS file. This
could happen, for example, in `UmdReflectionHost`, which expects the
file to contain exactly one statement. See angular#34544 for more details on a
real-world failure.

This commit fixes the issue by ensuring that
`computePrivateDtsDeclarationMap()` will only collect exported
declarations from files within the target package.

Jira issue: [FW-1794](https://angular-team.atlassian.net/browse/FW-1794)

Fixes angular#34544
@gkalpak gkalpak force-pushed the fix-ngcc-ignore-nodejs-builtins branch from 38e9d9e to 74e8273 Compare January 18, 2020 19:15
@gkalpak gkalpak added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jan 18, 2020
@mary-poppins
Copy link

You can preview 74e8273 at https://pr34811-74e8273.ngbuilds.io/.

@kainiedziela
Copy link

Would love to see this included in 9.0.0-rc.11

@alxhub alxhub self-assigned this Jan 23, 2020
Copy link
Member

@josephperrott josephperrott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approval for integration

@AndrewKushnir
Copy link
Contributor

JFYI, the only change that affected g3 is the error message text (function name correction) in packages/compiler-cli/src/ngtsc/reflection/src/typescript.ts, so no g3 presubmit is required.

AndrewKushnir pushed a commit that referenced this pull request Jan 23, 2020
AndrewKushnir pushed a commit that referenced this pull request Jan 23, 2020
…#34811)

Previously, while trying to build an `NgccReflectionHost`'s
`privateDtsDeclarationMap`, `computePrivateDtsDeclarationMap()` would
try to collect exported declarations from all source files of the
program (i.e. without checking whether they were within the target
package, as happens for declarations in `.d.ts` files).

Most of the time, that would not be a problem, because external packages
would be represented as `.d.ts` files in the program. But when an
external package had no typings, the JS files would be used instead. As
a result, the `ReflectionHost` would try to (unnecessarilly) parse the
file in order to extract exported declarations, which in turn would be
harmless in most cases.

There are certain cases, though, where the `ReflectionHost` would throw
an error, because it cannot parse the external package's JS file. This
could happen, for example, in `UmdReflectionHost`, which expects the
file to contain exactly one statement. See #34544 for more details on a
real-world failure.

This commit fixes the issue by ensuring that
`computePrivateDtsDeclarationMap()` will only collect exported
declarations from files within the target package.

Jira issue: [FW-1794](https://angular-team.atlassian.net/browse/FW-1794)

Fixes #34544

PR Close #34811
AndrewKushnir pushed a commit that referenced this pull request Jan 23, 2020
…#34811)

Previously, while trying to build an `NgccReflectionHost`'s
`privateDtsDeclarationMap`, `computePrivateDtsDeclarationMap()` would
try to collect exported declarations from all source files of the
program (i.e. without checking whether they were within the target
package, as happens for declarations in `.d.ts` files).

Most of the time, that would not be a problem, because external packages
would be represented as `.d.ts` files in the program. But when an
external package had no typings, the JS files would be used instead. As
a result, the `ReflectionHost` would try to (unnecessarilly) parse the
file in order to extract exported declarations, which in turn would be
harmless in most cases.

There are certain cases, though, where the `ReflectionHost` would throw
an error, because it cannot parse the external package's JS file. This
could happen, for example, in `UmdReflectionHost`, which expects the
file to contain exactly one statement. See #34544 for more details on a
real-world failure.

This commit fixes the issue by ensuring that
`computePrivateDtsDeclarationMap()` will only collect exported
declarations from files within the target package.

Jira issue: [FW-1794](https://angular-team.atlassian.net/browse/FW-1794)

Fixes #34544

PR Close #34811
@gkalpak gkalpak deleted the fix-ngcc-ignore-nodejs-builtins branch January 23, 2020 22:04
sonukapoor pushed a commit to sonukapoor/angular that referenced this pull request Feb 13, 2020
sonukapoor pushed a commit to sonukapoor/angular that referenced this pull request Feb 13, 2020
…angular#34811)

Previously, while trying to build an `NgccReflectionHost`'s
`privateDtsDeclarationMap`, `computePrivateDtsDeclarationMap()` would
try to collect exported declarations from all source files of the
program (i.e. without checking whether they were within the target
package, as happens for declarations in `.d.ts` files).

Most of the time, that would not be a problem, because external packages
would be represented as `.d.ts` files in the program. But when an
external package had no typings, the JS files would be used instead. As
a result, the `ReflectionHost` would try to (unnecessarilly) parse the
file in order to extract exported declarations, which in turn would be
harmless in most cases.

There are certain cases, though, where the `ReflectionHost` would throw
an error, because it cannot parse the external package's JS file. This
could happen, for example, in `UmdReflectionHost`, which expects the
file to contain exactly one statement. See angular#34544 for more details on a
real-world failure.

This commit fixes the issue by ensuring that
`computePrivateDtsDeclarationMap()` will only collect exported
declarations from files within the target package.

Jira issue: [FW-1794](https://angular-team.atlassian.net/browse/FW-1794)

Fixes angular#34544

PR Close angular#34811
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

platform-server errors with ivy-ngcc - UMD module - Angular 9.0.0-rc.7
9 participants