After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 689654 - Warn if we're importing an unversioned namespace
Warn if we're importing an unversioned namespace
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other All
: Normal enhancement
: ---
Assigned To: gjs-maint
gjs-maint
Depends on: 690136
Blocks:
 
 
Reported: 2012-12-04 19:54 UTC by Colin Walters
Modified: 2017-04-19 21:59 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Warn if we're importing an unversioned namespace (6.39 KB, patch)
2012-12-04 19:54 UTC, Colin Walters
needs-work Details | Review
repo: Warn when importing a namespace with >1 version (4.63 KB, patch)
2017-04-04 01:43 UTC, Philip Chimento
none Details | Review
repo: Warn when importing a namespace with >1 version (4.56 KB, patch)
2017-04-05 02:01 UTC, Philip Chimento
committed Details | Review
repo: Don't warn about 0 namespace versions (1.59 KB, patch)
2017-04-18 03:22 UTC, Philip Chimento
committed Details | Review

Description Colin Walters 2012-12-04 19:54:08 UTC
In introspection, libraries are a pair of (namespace, version).  But
it's possible to request "the latest" version of a namespace, and this
is in fact what many projects do.

The problem is that for say GTK+ which has both 2.0 and 3.0 currently,
if ever a 4.0 was introduced, suddenly everyone's scripts would
explode if GTK+ 4.0 happened to be installed.

Let's start warning about unversioned imports, and get people to do:

imports.gi.versions.Gtk = '3.0';
const Gtk = imports.gi.Gtk;
Comment 1 Colin Walters 2012-12-04 19:54:10 UTC
Created attachment 230695 [details] [review]
Warn if we're importing an unversioned namespace
Comment 2 Jasper St. Pierre (not reading bugmail) 2012-12-04 20:02:48 UTC
We need a more convenient syntax in this case. I don't want a warning if I'm just writing a dummy script, or playing with the gjs REPL.
Comment 3 Colin Walters 2012-12-05 02:25:29 UTC
(In reply to comment #2)
> We need a more convenient syntax in this case. I don't want a warning if I'm
> just writing a dummy script, or playing with the gjs REPL.

Suggestions?

We could also just make it warn for Gtk and Clutter - the two things most likely to get a second version.  But that's kind of ugly.
Comment 4 Jasper St. Pierre (not reading bugmail) 2012-12-05 21:32:53 UTC
What about warning if the major version > 1?
Comment 5 Colin Walters 2012-12-05 21:51:04 UTC
(In reply to comment #4)
> What about warning if the major version > 1?

That would work for Gtk, but not for Clutter (presently at 1.0, but I would not be surprised if there was a 2.0).

Also, just a bit too magical.

One other option I considered (and actually started implementing) - typelibs can contain a "default version".  In gtk/Makefile.am, we do SCANNER_OPTIONS += --default-version=3.0.

Then at typelib resolution time, we load the typelibs in reverse order until we find one with a default.  If found, we use that. 

The reason we'd hardcode --default-version=3.0 instead of just --default-version is so it's less likely that when Gtk is forked for 4.0, they change that 3.0 to 4.0, rather than forgetting to delete --default-version.
Comment 6 Colin Walters 2012-12-05 21:51:55 UTC
Yet another alternative is something like:

const Gtk = imports.gi.versions.get('Gtk', '3.0');
Comment 7 Philip Chimento 2017-01-06 05:45:38 UTC
Could we change it so that the warning is only logged if there was more than one version of that namespace available?
Comment 8 Philip Chimento 2017-04-02 23:49:14 UTC
Review of attachment 230695 [details] [review]:

Cosimo and I discussed that warning only if multiple versions available is the desired behaviour. I'll mark the patch needs-work accordingly and fix it up one of these days.
Comment 9 Philip Chimento 2017-04-04 01:43:58 UTC
Created attachment 349206 [details] [review]
repo: Warn when importing a namespace with >1 version

In introspection, libraries are a pair of (namespace, version).  But
it's possible to request "the latest" version of a namespace, and this
is in fact what many projects do.

The problem is that for say GTK+ which has 2.0, 3.0, and is just releasing
4.0, most people's scripts imported gi.Gtk and expected to receive 3.0, and
will explode when installing 4.0.

Let's start warning about unversioned imports when there are multiple
versions available, and get people to do:

imports.gi.versions.Gtk = '3.0';
const Gtk = imports.gi.Gtk;

(Original patch by Colin Walters; revised by Philip Chimento.)
Comment 10 Cosimo Cecchi 2017-04-04 16:38:48 UTC
Review of attachment 349206 [details] [review]:

::: gi/repo.cpp
@@ +109,3 @@
+        JS_ReportWarning(context, warn_text);
+    }
+        GjsAutoChar warn_text = g_strdup_printf("Requiring %s but it has %u "

Why not just use g_list_free_full()?
Comment 11 Philip Chimento 2017-04-05 02:01:10 UTC
Created attachment 349269 [details] [review]
repo: Warn when importing a namespace with >1 version

In introspection, libraries are a pair of (namespace, version).  But
it's possible to request "the latest" version of a namespace, and this
is in fact what many projects do.

The problem is that for say GTK+ which has 2.0, 3.0, and is just releasing
4.0, most people's scripts imported gi.Gtk and expected to receive 3.0, and
will explode when installing 4.0.

Let's start warning about unversioned imports when there are multiple
versions available, and get people to do:

imports.gi.versions.Gtk = '3.0';
const Gtk = imports.gi.Gtk;

(Original patch by Colin Walters; revised by Philip Chimento.)
Comment 12 Cosimo Cecchi 2017-04-06 02:29:36 UTC
Review of attachment 349269 [details] [review]:

Thanks, looks good now.
Comment 13 Philip Chimento 2017-04-07 15:47:29 UTC
Attachment 349269 [details] pushed as 0154c9d - repo: Warn when importing a namespace with >1 version
Comment 14 Philip Chimento 2017-04-18 03:22:24 UTC
I made a couple of errors here: we get the warning when importing a nonexistent namespace, and we also get it when running testCairo.js.
Comment 15 Philip Chimento 2017-04-18 03:22:42 UTC
Created attachment 349967 [details] [review]
repo: Don't warn about 0 namespace versions

If there are 0 namespace versions, we just need to error out. Don't print
the warning about there being more than one version.

Also, select the correct version in testCairo.js so we don't get the
warning there either.
Comment 16 Cosimo Cecchi 2017-04-18 16:40:10 UTC
Review of attachment 349967 [details] [review]:

Makes sense, thanks.
Comment 17 Philip Chimento 2017-04-19 03:37:00 UTC
Attachment 349967 [details] pushed as 1e0bbae - repo: Don't warn about 0 namespace versions