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 772033 - add a --version flag
add a --version flag
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Philip Chimento
gjs-maint
Depends on:
Blocks:
 
 
Reported: 2016-09-27 08:26 UTC by Mohammed Sadiq
Modified: 2016-11-15 23:09 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
console: Process arguments better (8.54 KB, patch)
2016-10-20 00:51 UTC, Philip Chimento
none Details | Review
console: Add --version option (2.85 KB, patch)
2016-10-20 00:51 UTC, Philip Chimento
committed Details | Review
console: Process arguments better (9.65 KB, patch)
2016-10-20 22:03 UTC, Philip Chimento
committed Details | Review
console: Check script args for stray GJS args (6.42 KB, patch)
2016-10-31 23:08 UTC, Philip Chimento
none Details | Review
console: Env vars for coverage args (4.39 KB, patch)
2016-10-31 23:08 UTC, Philip Chimento
none Details | Review
console: Check script args for stray GJS args (7.02 KB, patch)
2016-11-02 00:24 UTC, Philip Chimento
committed Details | Review
console: Env vars for coverage args (3.80 KB, patch)
2016-11-02 00:24 UTC, Philip Chimento
committed Details | Review
console: Fix wrong cast in coverage prefixes (1.19 KB, patch)
2016-11-15 07:05 UTC, Philip Chimento
none Details | Review
console: Fix wrong cast in coverage prefixes (1.20 KB, patch)
2016-11-15 21:52 UTC, Philip Chimento
committed Details | Review

Description Mohammed Sadiq 2016-09-27 08:26:23 UTC
Add a --version flag to gjs commandline which would print the version of gjs.
Comment 1 Philip Chimento 2016-10-20 00:51:19 UTC
Created attachment 338055 [details] [review]
console: Process arguments better

We now process our command line arguments as follows: all arguments
before a program name or a -c or --command argument are passed to the GJS
interpreter itself. All arguments after the program are passed into the
program's ARGV array.

This solves some weirdness where "gjs -c '...' --help" was showing the
GJS help instead of passing the --help option onto the program given with
-c, as expected.

This corresponds to the way Python processes its command line arguments,
and also opens up the possibility of --version or other arguments that
might want to be accepted both by GJS and a script.
Comment 2 Philip Chimento 2016-10-20 00:51:23 UTC
Created attachment 338056 [details] [review]
console: Add --version option

Now that we can avoid passing --version on to scripts, it is simple to
add a --version option that prints the current GJS version.
Comment 3 Cosimo Cecchi 2016-10-20 21:26:48 UTC
Review of attachment 338055 [details] [review]:

Pretty nice!
Comment 4 Cosimo Cecchi 2016-10-20 21:27:47 UTC
Review of attachment 338056 [details] [review]:

Feel free to commit with this fixed.

::: gjs/console.cpp
@@ +35,3 @@
 static char *coverage_output_path = NULL;
 static char *command = NULL;
+static gboolean print_version = false;

Should probably be bool?
Comment 5 Philip Chimento 2016-10-20 21:49:25 UTC
(In reply to Cosimo Cecchi from comment #4)
> Review of attachment 338056 [details] [review] [review]:
> 
> Feel free to commit with this fixed.
> 
> ::: gjs/console.cpp
> @@ +35,3 @@
>  static char *coverage_output_path = NULL;
>  static char *command = NULL;
> +static gboolean print_version = false;
> 
> Should probably be bool?

I prefer not, because G_OPTION_ARG_NONE would cause g_option_context_parse() to treat the arg_data field of GOptionEntry as a gboolean*, so we'd be casting bool* -> void* -> gboolean*. Probably would work nonetheless, since I'm pretty sure bool and gboolean would be the same size everywhere, but better not to take a chance.

I'll hold off committing to give you a chance to disagree :-)

Also I realized this probably needs a NEWS entry since it's a slight incompatibility with how it worked before.
Comment 6 Cosimo Cecchi 2016-10-20 21:54:00 UTC
(In reply to Philip Chimento from comment #5)
> (In reply to Cosimo Cecchi from comment #4)
> > Review of attachment 338056 [details] [review] [review] [review]:
> > 
> > Feel free to commit with this fixed.
> > 
> > ::: gjs/console.cpp
> > @@ +35,3 @@
> >  static char *coverage_output_path = NULL;
> >  static char *command = NULL;
> > +static gboolean print_version = false;
> > 
> > Should probably be bool?
> 
> I prefer not, because G_OPTION_ARG_NONE would cause g_option_context_parse()
> to treat the arg_data field of GOptionEntry as a gboolean*, so we'd be
> casting bool* -> void* -> gboolean*. Probably would work nonetheless, since
> I'm pretty sure bool and gboolean would be the same size everywhere, but
> better not to take a chance.
> 
> I'll hold off committing to give you a chance to disagree :-)


That's OK. I guess it makes sense since we're using the GLib API, to abide by its rules :-)

> Also I realized this probably needs a NEWS entry since it's a slight
> incompatibility with how it worked before.

Sure, feel free to add one.
Comment 7 Philip Chimento 2016-10-20 22:03:20 UTC
Created attachment 338133 [details] [review]
console: Process arguments better

We now process our command line arguments as follows: all arguments
before a program name or a -c or --command argument are passed to the GJS
interpreter itself. All arguments after the program are passed into the
program's ARGV array.

This solves some weirdness where "gjs -c '...' --help" was showing the
GJS help instead of passing the --help option onto the program given with
-c, as expected.

This corresponds to the way Python processes its command line arguments,
and also opens up the possibility of --version or other arguments that
might want to be accepted both by GJS and a script.
Comment 8 Philip Chimento 2016-10-20 22:04:32 UTC
Attachment 338056 [details] pushed as 0b9f702 - console: Add --version option
Attachment 338133 [details] pushed as 7ea4903 - console: Process arguments better
Comment 9 Philip Chimento 2016-10-20 22:05:04 UTC
I attached the version of the patch with the NEWS file, for reference.
Comment 10 Philip Chimento 2016-10-28 22:15:52 UTC
Unfortunately, this change breaks executable JS scripts that specify GJS in their shebang line, because it's not possible to stick any command line arguments in between the GJS binary name and the script name.

So, for example,

script:

    #!/usr/bin/env gjs
    print(imports.mymodule.something);

Whereas previously you could do

    ./script -I /path/to/mymodule --coverage-output=coverage.out

Those flags now won't have the desired effect, and it's impossible to convey the options to script unless you run it as "gjs script -I ..."

Looking at how Python does things I think it would be good to deprecate these command line arguments in favour of environment variables. We already have GJS_PATH, and we could add GJS_COVERAGE_PREFIXES and GJS_COVERAGE_OUTPUT.

The question is how to do this while causing the least inconvenience to app authors who use the command line arguments.

1. We could revert the patch for now, and issue a warning when the command line options are used, suggesting to use the environment variables instead. Then in the next-after-next minor release we could remove the command line options.

2. We could keep the patch, and scan the command line arguments passed to the script to see if they contain -I, --include, --coverage-prefix, -C, or --coverage-output. Then we could issue a warning. Modules would break, but at least the module authors would know how to fix it.

3. We could keep the patch, scan the command line arguments passed to the script, but mimic the old behaviour too (that is, if the script gets -I, we still pass -I on to the script, but also add a path to the include paths.) Also in this case we would issue a warning. After a suitable amount of time the mimicking behaviour would be removed and we'd go back to #2 but hopefully after all users of the command line options had switched to environment variables instead.
Comment 11 Philip Chimento 2016-10-28 22:19:22 UTC
For that matter, with #2 and #3 we could still continue to support the command line options if they were passed before the script, that's what Python does.
Comment 12 Philip Chimento 2016-10-31 23:08:04 UTC
Created attachment 338864 [details] [review]
console: Check script args for stray GJS args

In order to ease the pain of migration, check to see if any
--include-path, --coverage-prefix, or --coverage-output arguments are
being passed to the JS script. If so, treat them as if they had been
passed to GJS while issuing a warning that this behaviour will go away in
the future.
Comment 13 Philip Chimento 2016-10-31 23:08:07 UTC
Created attachment 338865 [details] [review]
console: Env vars for coverage args

Encourage use of environment variables for coverage command line
arguments, GJS_COVERAGE_PREFIXES instead of --coverage-prefix, and
GJS_COVERAGE_OUTPUT instead of --coverage-output.

This is because we want to pass arguments occurring after the script on
the command line as arguments to the script and not to GJS. Without
environment variables, it would otherwise be impossible to turn on
coverage, for example, for a script that started with #!/usr/bin/gjs.
Comment 14 Philip Chimento 2016-10-31 23:08:36 UTC
Above are two patches that implement #3, which is the solution that I personally prefer.
Comment 15 Cosimo Cecchi 2016-11-01 19:07:59 UTC
Review of attachment 338864 [details] [review]:

::: gjs/console.cpp
@@ +97,3 @@
+
+    if (coverage_prefixes != NULL &&
+    int ix, argc_copy = argc + 1;

I think this approach has a couple of issues:
- in case the arguments are both before and after the script, those before will get ignored
- furthermore, they will get ignored silently if they happen to be the same number of arguments
- furthermore, the old include_path and coverage_prefixes will be leaked

In general I would prefer to always use new variables for the compat parsing, always warn when the new variables are set, and concatenating the two sets of arguments.
Comment 16 Cosimo Cecchi 2016-11-01 19:10:36 UTC
Review of attachment 338865 [details] [review]:

::: gjs/console.cpp
@@ +230,3 @@
+    env_coverage_prefixes = g_getenv("GJS_COVERAGE_PREFIXES");
+    if (env_coverage_prefixes != NULL) {
+        coverage_output_path = g_strdup(env_coverage_output_path);

This can be moved inside the else block below.

@@ +237,3 @@
+            unsigned new_nprefixes = g_strv_length(env_prefixes);
+            char **new_coverage_prefixes =
+    if (env_coverage_prefixes != NULL) {

I find it odd that GJS_COVERAGE_PREFIXES is combined with those passed on the command line... Usually environment variables completely override the behavior instead of adding to the default.
Comment 17 Philip Chimento 2016-11-02 00:24:26 UTC
Created attachment 338917 [details] [review]
console: Check script args for stray GJS args

In order to ease the pain of migration, check to see if any
--include-path, --coverage-prefix, or --coverage-output arguments are
being passed to the JS script. If so, treat them as if they had been
passed to GJS while issuing a warning that this behaviour will go away in
the future.
Comment 18 Philip Chimento 2016-11-02 00:24:30 UTC
Created attachment 338918 [details] [review]
console: Env vars for coverage args

Encourage use of environment variables for coverage command line
arguments, GJS_COVERAGE_PREFIXES instead of --coverage-prefix, and
GJS_COVERAGE_OUTPUT instead of --coverage-output.

This is because we want to pass arguments occurring after the script on
the command line as arguments to the script and not to GJS. Without
environment variables, it would otherwise be impossible to turn on
coverage, for example, for a script that started with #!/usr/bin/gjs.
Comment 19 Philip Chimento 2016-11-02 00:25:02 UTC
(In reply to Cosimo Cecchi from comment #15)
> Review of attachment 338864 [details] [review] [review]:
> 
> ::: gjs/console.cpp
> @@ +97,3 @@
> +
> +    if (coverage_prefixes != NULL &&
> +    int ix, argc_copy = argc + 1;
> 
> I think this approach has a couple of issues:
> - in case the arguments are both before and after the script, those before
> will get ignored

For --coverage-output I think that's OK, since you can only have one coverage output, it seems logical to have the rightmost one on the command line take precedence.

> - furthermore, they will get ignored silently if they happen to be the same
> number of arguments
> - furthermore, the old include_path and coverage_prefixes will be leaked
> 
> In general I would prefer to always use new variables for the compat
> parsing, always warn when the new variables are set, and concatenating the
> two sets of arguments.

I assumed that storing pointers to non-NULL strvs in GOptionEntry would reallocate and append to them instead of overwriting and leaking them, but if that is not the case, then your way is better.
Comment 20 Cosimo Cecchi 2016-11-02 01:28:42 UTC
Review of attachment 338917 [details] [review]:

Thanks, looks good now.
Comment 21 Cosimo Cecchi 2016-11-02 01:29:24 UTC
Review of attachment 338918 [details] [review]:

Looks good.
Comment 22 Philip Chimento 2016-11-02 02:08:46 UTC
Attachment 338917 [details] pushed as 27feb41 - console: Check script args for stray GJS args
Attachment 338918 [details] pushed as a8887e3 - console: Env vars for coverage args
Comment 23 Philip Chimento 2016-11-15 07:05:48 UTC
Created attachment 339894 [details] [review]
console: Fix wrong cast in coverage prefixes

I'm not sure what I was thinking here, but env_coverage_prefixes is a
string, not a strv, and needs to be split up.
Comment 24 Philip Chimento 2016-11-15 07:06:08 UTC
Reopening again, I found a bug in my code
Comment 25 Cosimo Cecchi 2016-11-15 18:29:11 UTC
Review of attachment 339894 [details] [review]:

::: gjs/console.cpp
@@ +259,2 @@
         if (coverage_prefixes == NULL) {
+            coverage_prefixes = g_strdupv(env_prefixes);

Is env_prefixes not leaked in this case?
Comment 26 Philip Chimento 2016-11-15 21:52:33 UTC
Created attachment 339966 [details] [review]
console: Fix wrong cast in coverage prefixes

I'm not sure what I was thinking here, but env_coverage_prefixes is a
string, not a strv, and needs to be split up.
Comment 27 Philip Chimento 2016-11-15 21:53:08 UTC
(In reply to Cosimo Cecchi from comment #25)
> Review of attachment 339894 [details] [review] [review]:
> 
> ::: gjs/console.cpp
> @@ +259,2 @@
>          if (coverage_prefixes == NULL) {
> +            coverage_prefixes = g_strdupv(env_prefixes);
> 
> Is env_prefixes not leaked in this case?

Yes, I realized it could be made even simpler as well
Comment 28 Cosimo Cecchi 2016-11-15 22:14:03 UTC
Review of attachment 339966 [details] [review]:

Looks good now.
Comment 29 Philip Chimento 2016-11-15 23:09:54 UTC
Attachment 339966 [details] pushed as 735d816 - console: Fix wrong cast in coverage prefixes