Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

fix: don't add comments in json files - #922

Closed
alan-agius4 wants to merge 1 commit into
angular:masterfrom
alan-agius4:fix_json_comments
Closed

alan-agius4 wants to merge 1 commit into
angular:masterfrom
alan-agius4:fix_json_comments

Conversation

@alan-agius4

Copy link
Copy Markdown
Contributor

TypeScript 2.9 introduced the resolveJsonModule feature, however tsickle will parse json files add the overview comment will will make them invalid json files.

Example:

/**
 * @fileoverview added by tsickle
 * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
 */
{
    "fo": true
}

This change will make sure that .json files are not parsed.

@evmar evmar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice, thanks for the patch!

Comment thread test/tsickle_test.ts Resolved
Comment thread test/tsickle_test.ts Resolved
Comment thread src/fileoverview_comment_transformer.ts Resolved
@alan-agius4
alan-agius4 force-pushed the fix_json_comments branch 2 times, most recently from eb9cc36 to a66b24f Compare October 22, 2018 06:11
Comment thread test/tsickle_test.ts Resolved
@tgv1975

tgv1975 commented Jan 29, 2019

Copy link
Copy Markdown

Can someone please review and merge this? It's breaking Angular library compilation, too, if it imports JSON files:

image

The comment on top of the file breaks rollup-plugin-json. Comments in JSON files aren't allowed to begin with, this tool actually adds comments to JSON, instantly invalidating the format.

Please prioritize this. Thank you!

@mprobst

mprobst commented Jan 29, 2019

Copy link
Copy Markdown
Contributor

@vikerman is taking a look at why these dependencies keep cropping up.

@tgv1975

tgv1975 commented Aug 31, 2019

Copy link
Copy Markdown

Any progress on this? The behavior seems to have remained the same.

@mprobst

mprobst commented Sep 2, 2019 via email

Copy link
Copy Markdown
Contributor

@alan-agius4

Copy link
Copy Markdown
Contributor Author

@mprobst, TypeScript allows to import json as modules; the problem is most likely due that users are using this TSC feature with tsickle in ngpackagr, since in ng-packagr tsickle is used to solve the TDZ of Angular’s forwardRef.

@mprobst

mprobst commented Sep 2, 2019 via email

Copy link
Copy Markdown
Contributor

@alan-agius4

Copy link
Copy Markdown
Contributor Author

I am getting more keen into adding the transformer in ngpackagr similar to what was done in the CLI

@tgv1975

tgv1975 commented Sep 2, 2019

Copy link
Copy Markdown

@mprobst Thanks for your reply. My setup is simply what Angular offers for library compilation and packaging via ngpackagr, as @alan-agius4 also mentioned.

Regardless of the use case, couldn't tsickle simply be prevented to annotate JSON files at all, in no circumstance, since that is against the JSON standard anyway?

@mprobst

mprobst commented Sep 2, 2019 via email

Copy link
Copy Markdown
Contributor

@tgv1975

tgv1975 commented Sep 2, 2019

Copy link
Copy Markdown

Thing is, I'm not explicitly using tsickle, it is apparently used by Angular to compile and package libraries. However, not allowing tsickle to add comments to JSON (and breaking the JSON spec) would actually solve my problem, since the process fails when invalid JSON comes down the pipe (namely in the Rollup JSON plugin). I have manually edited said plugin to cleanup that tsickle comment, and the compilation passes like a charm.

I am certain you are right and there may be other, hidden, implications of this, but for now I strongly believe that simply not allowing tsickle to corrupt valid JSON with comments, under any circumstances, would go a long way.

@tgv1975

tgv1975 commented Sep 2, 2019

Copy link
Copy Markdown

The PR from @alan-agius4 is addressing just that (avoid adding comments to any JSON file) in a straightforward way, as far as I can tell, it simply needs someone to review and merge.

@evmar

evmar commented Sep 5, 2019

Copy link
Copy Markdown
Contributor

What does Closure compiler do when you tell it to import a json file? I think it will fail, won't it?

@tgv1975

tgv1975 commented Sep 13, 2019

Copy link
Copy Markdown

No news on this? Basically one cannot build an Angular library if it happens to import JSON files because of this issue, as simple as that. Would not allowing tsickle corruption of JSON files with comments under any circumstance be too far fetched?

@evmar

evmar commented Sep 13, 2019

Copy link
Copy Markdown
Contributor

Rereading the above comments: I think the conclusion is that if you get into this state, something has gone wrong with your app configuration. Another way of saying this is that the correct fix for your problem is to not use tsickle.

@alexeagle

Copy link
Copy Markdown
Contributor

re-read what @alan-agius4 said above - ng-packagr is stuck using tsickle because it has a needed feature (decorator downleveling that doesn't introduce a temporal dead zone) but it should not - we ought to have implemented decorator downleveling in a different place (a typescript CustomTransformer as well as something to fix up JS files in node_modules) - @clydin did a hack to port the feature into the Angular webpack loader so that the tsickle dependency was removed from Angular apps, but @alan-agius4 proposes that we also pull it into ng-packagr in order to remove the tsickle dependency from Angular libraries.

@tgv1975

tgv1975 commented Sep 13, 2019

Copy link
Copy Markdown

@evmar My configuration is the default, generated with the ng CLI, I did not do any customization, I did not chose to use tsickle. As I said before, at no point did I explicitly involve tsickle. There's nothing I can do there, as far as I can tell.

@alexeagle Thanks for your reply. Does this mean said "hack" will at some point fix ng-packagr in this respect, by making it not use tsickle anymore?

@alexeagle

Copy link
Copy Markdown
Contributor

I referenced ng-packagr/ng-packagr#1400 - that's the actual bug

I'll leave it up to Martin and Evan if they want to close this PR

@tgv1975

tgv1975 commented Sep 13, 2019

Copy link
Copy Markdown

@alexeagle Thanks for that, hopefully it will sort out this issue. Until then I'm stuck with manually editing the Rollup JSON plugin to remove the JSON comment every time yarn overwrites it with the original.

@evmar

evmar commented Sep 13, 2019

Copy link
Copy Markdown
Contributor

The fix doesn't seem to bad so if doing the wrong thing here is the best place to fix all the other pieces, we may as well do it I guess. But now that I think about it: if we do fix this in tsickle, it would only show up in a newer Angular release, right? And in that case you might as well be using ivy which doesn't involve this(?).

Also I vaguely recall @alexeagle making the tsickle dep toggleable at runtime or something. Is it possible for @tgv1975 to hand-edit a config file somewhere to turn it off?

(Sorry in advance for my really vague comments here, I don't know much about Angular.)

@alexeagle

Copy link
Copy Markdown
Contributor

Nope, in this case it's not user controlled, the ng-packagr tool always does this.

@alan-agius4

alan-agius4 commented Sep 13, 2019 via email

Copy link
Copy Markdown
Contributor Author

@alan-agius4

alan-agius4 commented Sep 13, 2019 via email

Copy link
Copy Markdown
Contributor Author

@tgv1975

tgv1975 commented Sep 13, 2019 •

Copy link
Copy Markdown

Great stuff, guys, thank you! @alan-agius4 I found the annotateForClosureCompiler switch in the tsconfig.json of the individual libraries, under the angularCompilerOptions I guess that's where I turn it off.

@evmar I suppose no tool, ever, should corrupt JSON with comments, regardless of the context, so maybe having this fix (not parsing JSON files at all) is not ill-advised after all, and may prevent unforeseen such incidents down the road.

@alan-agius4

Copy link
Copy Markdown
Contributor Author

In all fairness I don’t think it’s a good idea to use json modules in web this is because they are not supported natively by the browsers and you’ll always be dependent on a bundler for your code to run.

I believe that resolveJsonModules was more intended for node applications.

In most cases you should use an Object Literal instead of a JSON module.

@alan-agius4
alan-agius4 deleted the fix_json_comments branch September 27, 2019 05:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants