AMP components like extensions/amp-subscriptions/ include third party code that is checked in to our repo under third_party/. When we run the type checker via gulp check-types or minify code via gulp dist, closure compiler treats the code in third_party/ as if it were closure code.
third_party/ may interfere with compilation optimizations around inlining, devirtualization, minification, etc.@dvoytenko recently discovered this due to following events:
v20190301 (https://github.com/ampproject/amphtml/pull/21618)third_party/subscriptions-project/swg.js contains such an annotationTo fix this, we must no longer treat code in third_party/ as closure code.
This issue is meant to discuss and track a fix.
/cc @erwinmombay @dvoytenko @choumx @cramforce for visibility and for ideas.
s/\/\*\*/\/*x/g may do it. (Remove the JSDoc /** open marker.
@cramforce yes, /** -> /* removes the problem.
I'd just keep the number of chars the same for source maps :)
On Thu, Apr 11, 2019 at 1:57 PM Dima Voytenko notifications@github.com
wrote:
@cramforce https://github.com/cramforce yes, /* -> / removes the
problem.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ampproject/amphtml/issues/21828#issuecomment-482310910,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAV4TZAJ6MLENWJTDAOYJ3PP6QZZANCNFSM4HFLHCDA
.
I hit this during the last CC upgrade, which started failing on missing type paths. So this might've been an issue even before Raghu's PR.
If this regex is still being used, we should just update it to catch this instance.
Add a |< to the first capture group so it catches Promise<./foo> in addition to !./foo and ?./foo:
(!|\?|<)\.[^>)}]*
In #21830, I tried what at first seemed like an elegant solution to replace /** with /* in the gulp stream that's passed to gulp-closure-compiler, but it didn't work because gulp-closure-compiler ignores the contents of the file stream passed to it, and instead, picks up the filename and re-reads it from disc.
@choumx
swg.jsswg.js.I'll pick this up tomorrow. I'm still in search of a good way to replace the contents of source files before passing them to closure compiler. (I believe multi-pass gulp dist compiles files in-place, so we can't modify files in the source directory.)
@rsimha , you might need to write the swg files into build folder (transformed), and change the paths. this trick might not work for singlepass though, ill check
I think @jridgewell has looked into modifying source files pre-compile to fix source maps due to version string replacement and license minification, though IIRC there isn't a good way yet.
Yah, I looked into the source code of the gulp-closure-compiler and it was rereading files directly from disk. When I tried writing the modifications to a temp directory before passing to the complier, it was getting other errors (or just ignoring them completely).
Switching to Closure's official gulp plugin might work. It looks like they take the modifications buffer and apply it before running the compiler.
Crap, it seems like there's no way to get closure's official gulp plugin to use our custom runner.jar. Digging in...
/cc @erwinmombay
should be possible, see https://github.com/ampproject/amphtml/blob/master/build-system/single-pass.js#L61
With ~#21868~ #21900, we are no longer blocked by the weird behavior of gulp-closure-compiler. The coast is now clear to replace JSDoc comments in third_party/. This is coming up in #21878.
Update: A mechanism for removing JSDoc from third party code is now available in #21878. We now need a strategy for fixing type errors in extensions code that uses third party code. See https://github.com/ampproject/amphtml/pull/21878#issuecomment-483908074
(I work on JS within Google and have investigated this problem in depth; Googlers can read go/tpl-js .)
In general you cannot compile arbitrary third-party JS in Closure because the code may use patterns that the optimizer eats. If you are ok with this and only care about errors, I believe the --hide_warnings_for=<path prefix> flag actually turns off all error checking in files matching the path. I am not confident on this but I think this has worked for us and from a glance at the code (which I also don't understand too well) it appears it turns checks off:
Using --hide_warnings_for=third_party/ is very common within Google.
@evmar thanks a lot!
@rsimha Do you recollect what were the kind of errors we saw with --hide_warnings_for?
@evmar Generally, our goals are:
Do you recollect what were the kind of errors we saw with
--hide_warnings_for?
@dvoytenko I tried @evmar's suggestion and added third_party/subscriptions-project/ to the set of --hide_warnings_for directories.
third_party/, we still see a couple dozen type errors in extensions/amp-subscriptions-google/ and extensions/amp-subscriptions/.third_party/, and merely add third_party/subscriptions-project/ to the set of --hide_warnings_for directories, it's a no-op because we aren't seeing any type errors at present. Also, I don't think this will prevent the compiler optimizations to third party code that you were trying to avoid.The latest code is available at #21878.
@rsimha what happens if we keep the annotations and set the hide_warnings_for. TBH, I was under the impression that we always had hide_warnings_for set up for the whole third_party.
what happens if we keep the annotations and set the
hide_warnings_for. TBH, I was under the impression that we always hadhide_warnings_forset up for the wholethird_party.
See item 2 in https://github.com/ampproject/amphtml/issues/21828#issuecomment-493533602.
@rsimha , yes, but i was wondering if you had the log from before. There are no longer errors because we compensated on the SwG's side by changing it at the source. In other words, it's likely to happen again.
The only logs I have are at https://github.com/ampproject/amphtml/pull/21878#issuecomment-483908074, but I'm not sure if that's what you're looking for. Maybe you can roll back swg.js to a revision that had errors and rerun gulp check-types?
After discussing offline with @dvoytenko, there's no pressing need to do something as drastic as removing all type annotations from swg.js.
Part of the reason behind this discussion was https://github.com/ampproject/amphtml/pull/21827#pullrequestreview-225734306, which turned out to be a red herring due to an unnecessary global find-replace I did while upgrading closure compiler.
For now, we are fine with unknown types in swg.js because we ignore errors that stem from them:
This issue can be closed until we face other problems due to types in third party code.