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 scala.js deprecation warnings #222

Merged
merged 4 commits into from
Oct 21, 2023
Merged

Conversation

Mingun
Copy link
Contributor

@Mingun Mingun commented Nov 24, 2020

No description provided.

@generalmimon
Copy link
Member

I need to check this once I'm on my computer, if it works as before or not.

@Mingun
Copy link
Contributor Author

Mingun commented Dec 17, 2020

Hey! Let's finish this (and other PRs) before end of year! 😃 @GreyCat, @generalmimon, @dgelessus

Copy link
Member

@generalmimon generalmimon left a comment

Choose a reason for hiding this comment

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

If you compare your and upstream version of the generated bundle js/npm/kaitai-struct-compiler.js after running sbt compile fastOptJS buildNpmJsFile, you find that the path to the object MainJs has also changed:

diff --git "1/.\\kaitai-struct-compiler-upstream.js" "2/.\\kaitai-struct-compiler-Mingun-scala-js.js"
index 02dcd47..0f698e1 100644
--- "1/.\\kaitai-struct-compiler-upstream.js"
+++ "2/.\\kaitai-struct-compiler-Mingun-scala-js.js"
@@ -1,10 +1,10 @@
-(function (root, factory) {
+(function (root, compiler) {
   if (typeof define === 'function' && define.amd) {
-    define([], factory);
+    define([], function() { return compiler; });
   } else if (typeof module === 'object' && module.exports) {
-    module.exports = factory();
+    module.exports = compiler;
   } else {
-    root.KaitaiStructCompiler = factory();
+    root.KaitaiStructCompiler = compiler;
   }
 }(this, function () {
 
@@ -93251,10 +93251,7 @@ var $d_scm_ArrayBuffer = new $TypeData().initClass({
   Ljava_io_Serializable: 1
 });
 $c_scm_ArrayBuffer.prototype.$classData = $d_scm_ArrayBuffer;
-$e.io = ($e.io || {});
-$e.io.kaitai = ($e.io.kaitai || {});
-$e.io.kaitai.struct = ($e.io.kaitai.struct || {});
-$e.io.kaitai.struct.MainJs = $m_Lio_kaitai_struct_MainJs$;
+$e.MainJs = $m_Lio_kaitai_struct_MainJs$();
 }).call(this);
 //# sourceMappingURL=kaitai-struct-compiler-js-fastopt.js.map

 return exports.io.kaitai.struct.MainJs;

 }));

So return exports.io.kaitai.struct.MainJs here:

|$compiledFileContents
|
|return exports.io.kaitai.struct.MainJs;
|
|}));

also needs to be changed into:

        |$compiledFileContents 
        | 
        |return exports.MainJs; 
        | 
        |}));

@Mingun
Copy link
Contributor Author

Mingun commented Dec 17, 2020

I've found that probably changing factory to compiler is wrong. According my analysis, current master does the following:

  • root.KaitaiStructCompiler is a kaitai struct compiler constructor function, should be used as KaitaiStructCompiler() or new KaitaiStructCompiler() which gives us a compiler object
  • module.exports the same as root.KaitaiStructCompiler
  • define accepts a factory that produces kaitai struct compiler constructor function

After upgrading scala-js and do not touching factory stuff:

  • root.KaitaiStructCompiler is a compiler object itself, should be used as KaitaiStructCompiler
  • module.exports the same as root.KaitaiStructCompiler
  • define accepts a factory that produces a compiler object

Such a change seems reasonable and I think we shouldn't does anything with that. As I see, nobody in kaitai_struct repos using this generated file, but I've not checking web-IDE

@generalmimon
Copy link
Member

generalmimon commented Dec 17, 2020

Such a change seems reasonable and I think we shouldn't does anything with that.

Well, I wholeheartedly agree, as the stuff like

const KaitaiStructCompiler = require('kaitai-struct-compiler');
const compiler = new KaitaiStructCompiler();
// compiler.compile(...);

that shows the common usage at the moment actually doesn't make any sense - KaitaiStructCompiler is just some 0-arg trivial function like function() { return MainJs; } so invoking it with new is just pure nonsense, and the whole step compiler = KaitaiStructCompiler() is completely redundant as well.

But I'm reluctant to make such a daring BC break, because kaitai-struct-compiler at npm is used quite a lot in the wild (as you can see from the download stats, it has ~300 downloads per week, whatever that means) and this change is guaranteed to break all code using it.

As I see, nobody in kaitai_struct repos using this generated file, but I've not checking web-IDE

Well, at the very least, GitHub shows so-called dependents by scanning the package.json files: https://github.com/kaitai-io/kaitai_struct_compiler/network/dependents

And yeah, the Web IDE is essentially based on the Scala.js bundle of KSC, I think that actually the Web IDE was the reason why the Scala.js target of the KSC was introduced in the first place :)

So we've got a dilemma here of simplifying the usage but breaking the BC and just leaving it as it is for now (in the not-so-ideal state). I mean - the fix is incredibly simple (you just remove the calling brackets ()), but you still have to know that you need to do it, and it can be really annoying to be an unsuspecting user, run npm update and wonder why the app just stopped working. But yeah, I know - we need to do the BC breaks at some point 🙂

I see that you added an info to the README, that's great, but I wonder whether we can also automatically show a warning in the console right after a user updates the kaitai-struct-compiler package to the version where the BC break happens. Would something like this work?

warn-about-constructor-bc-break.js

console.warn(
'kaitai-struct-compiler: WARNING - you\'ve just installed the 0.10.0-SNAPSHOT20201217+ version. \
If you\'re coming from an older version, make sure to update your code - \
the usage changed from "(new KaitaiStructCompiler()).compile(...)" to "KaitaiStructCompiler.compile(...)".\n'
);

package.json

{
  "version": "0.10.0-SNAPSHOT20201217-...",
  "scripts": {
    "postinstall": "node warn-about-constructor-bc-break.js"
  }
}

I think that this warning can be quite useful for the users.

RELEASE_NOTES.md Outdated Show resolved Hide resolved
js/README.md Outdated Show resolved Hide resolved
RELEASE_NOTES.md Outdated Show resolved Hide resolved
@Mingun
Copy link
Contributor Author

Mingun commented Dec 21, 2020

But I'm reluctant to make such a daring BC break, because kaitai-struct-compiler at npm is used quite a lot in the wild (as you can see from the download stats, it has ~300 downloads per week, whatever that means) and this change is guaranteed to break all code using it.

Well, kaitai-struct-compiler follows semver semantics, so that's not a big problem. Until version 1.0 breaking changes is allowed when changing minor version. In the long-term perspective is is better to fix such kind of things before we reaching version 1.0 because versions 0.x are just for this! Anyway, in production code you shouldn't depends on the unstable tags, so if you depends on something like latest version, I think, you are understands all pros & cons.

So we've got a dilemma here of simplifying the usage but breaking the BC and just leaving it as it is for now (in the not-so-ideal state). I mean - the fix is incredibly simple (you just remove the calling brackets ()), but you still have to know that you need to do it, and it can be really annoying to be an unsuspecting user, run npm update and wonder why the app just stopped working. But yeah, I know - we need to do the BC breaks at some point 🙂

I definitely sure that we should do that. In any case when you upgrade your dependencies to next minor/major version you shouldn't expect that all will work as before 🙂

I see that you added an info to the README, that's great, but I wonder whether we can also automatically show a warning in the console right after a user updates the kaitai-struct-compiler package to the version where the BC break happens. Would something like this work?

I've add this to the PR, but I think it will not be so useful, because it seems that this script will not run on npm update but these users is the main contingent that should be notified. I was unable to find instructions to display a message when npm update, not when npm install. It is possible, of course, that the update includes installation... For that reason I've add this warning as separate commit, so If after all you decide that it useless, just skip it when merge.

@Mingun Mingun requested a review from generalmimon December 21, 2020 09:14
js/warn-about-constructor-bc-break.js Outdated Show resolved Hide resolved
js/README.md Outdated
@@ -53,23 +53,24 @@ Our [examples repository](https://github.com/kaitai-io/kaitai_struct_examples) c

## Plugging in

> :information_source: Before the version 0.10.0 the `kaitai-struct-compiler` module returned a _compiler constructor_ (called `KaitaiStructCompiler`) and you used it as `new KaitaiStructCompiler()`, but since 0.10.0 release this is the _compiler object_ itself and using `new` is not required anymore (see [#222](https://github.com/kaitai-io/kaitai_struct_compiler/pull/222)).
Copy link
Member

@generalmimon generalmimon Dec 27, 2020

Choose a reason for hiding this comment

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

Suggested change
> :information_source: Before the version 0.10.0 the `kaitai-struct-compiler` module returned a _compiler constructor_ (called `KaitaiStructCompiler`) and you used it as `new KaitaiStructCompiler()`, but since 0.10.0 release this is the _compiler object_ itself and using `new` is not required anymore (see [#222](https://github.com/kaitai-io/kaitai_struct_compiler/pull/222)).
> :information_source: Before the version 0.10.0 the `kaitai-struct-compiler` module returned a _compiler constructor_ (called `KaitaiStructCompiler`) and you would use it as `(new KaitaiStructCompiler()).compile(...)`, but since 0.10.0 release it returns the _compiler object_ itself and you just need to do `KaitaiStructCompiler.compile(...)`. Make sure to adapt your code.

I deleted the PR reference from my suggestion right away as I suggested in #222 (comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reworded as you suggest

js/warn-about-constructor-bc-break.js Outdated Show resolved Hide resolved
@Mingun
Copy link
Contributor Author

Mingun commented May 13, 2021

Ping

`npm` since `v7` doesn't display the output of lifecycle scripts (see
https://docs.npmjs.com/cli/v9/using-npm/logging?v=true#foreground-scripts),
so the automatic notification no longer works. The note in README will
have to suffice.

This reverts commit 1be1580.
Copy link
Member

@generalmimon generalmimon left a comment

Choose a reason for hiding this comment

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

@Mingun Thanks!

@generalmimon generalmimon merged commit 746d773 into kaitai-io:master Oct 21, 2023
@Mingun Mingun deleted the scala-js branch October 21, 2023 18:50
generalmimon added a commit to kaitai-io/kaitai_struct_webide that referenced this pull request Nov 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants