GNOME Bugzilla – Bug 591991
clean valac error for "using XYZ" when xyz-dev package is not installed
Last modified: 2018-05-22 13:22:25 UTC
When a certain package is not installed Vala generates C code which is not compilable. For newbies these errors will be non-intuitive and they force people to deal with the "implementation detail" that is C. Besides a pony, I also wish for a feature in Vala that detects un-installed libraries and prints a nice newbie friendly error message. To repro the issue, make sure that "libgoocanvas3" is not installed on your machine. and then compile this Vala code: using Goo; int main(string[] args) { var x = new Goo.Canvas(); return 0; } You'll see something like: $ valac --pkg goocanvas --pkg gtk+-2.0 main.vala main.vala:5.6-5.25: warning: local variable `x' declared but never used var x = new Goo.Canvas(); ^^^^^^^^^^^^^^^^^^^^ /some/path/main.vala.c:4:23: error: goocanvas.h: No such file or directory /some/path/main.vala.c: In function ‘_main’: /some/path/main.vala.c:17: error: ‘GooCanvas’ undeclared (first use in this function) /some/path/main.vala.c:17: error: (Each undeclared identifier is reported only once /some/path/main.vala.c:17: error: for each function it appears in.) /some/path/main.vala.c:17: error: ‘x’ undeclared (first use in this function) /some/path/main.vala.c:18: error: expected expression before ‘)’ token error: cc exited with status 256 Compilation failed: 1 error(s), 1 warning(s) The change I would like to request is that valac stops compilation right at "using Goo" (whenever the vapi is available but the .pc file is not installed or something along those lines) and then just print "error: Development files for library 'Goo' is not installed." The Vala program should see a Vala equivalent to "error: goocanvas.h: No such file or directory" instead of making it through Vala compilation and being left with some obscure C compiler error instead.
That's unfortunately not all that easy. There are many, many .vapi files that do *not* have corresponding .pc files (posix.vapi, zlib.vapi, ...). IMHO this is job for vala support in the build system. I can imagine autoconf, waf, SCons and similar tools to have a 'VALA_CHECK_PKG' function similar to 'PKG_CHECK_MODULE', which would look at the vapi, find the header names mentioned therein and than: - Try to find corresponding .pc file, but continue anyway if not found and - Try to find the headers, using the include options from the .pc if any and fail if the headers are not found. The compiler is probably not the right place to care. The C compiler does not care either -- it simply says it does not see the headers. That said, it would probably be possible to have some annotation in the .vapi file declaring that a corresponding .pc should exist, so valac can error out, but it's not such a trivial work.
Good point. I prefer the VAPI metadata idea though because while I recognize that sometimes you just need a full build system; I also value being able to do a quick "valac *.vala" and still get a good user experience.
Compiling: using Goo; int main(string[] args) { var x = new Goo.Canvas(); return 0; } with current git master version of Vala produces: test.vala:1.7-1.9: error: The namespace name `Goo' could not be found using Goo; ^^^
This requires the VAPI to be installed, but not the C headers. So compiling with: valac --pkg goocanvas test.vala still produces: gtk+-2.0.vapi:5871.40-5871.42: warning: Gtk is deprecated. Use gtk+-3.0 test.vala:4.14-4.16: warning: Goo is deprecated. Use goocanvas-2.0 test.vala:4.6-4.25: warning: local variable `x' declared but never used var x = new Goo.Canvas(); ^^^^^^^^^^^^^^^^^^^^ /home/al/test.vala.c:9:23: fatal error: goocanvas.h: No such file or directory compilation terminated. error: cc exited with status 256 Compilation failed: 1 error(s), 3 warning(s) So valac would have to cope with three states: 1. producing C code only by using --ccode or -C switch, this should have no header check 2. producing C code and using the C compiler where the headers have a corresponding pkgconfig file, as suggested earlier this would probably have to have something like a [CCode (pkgconfig_name="goocanvas")] annotation detail added to the namespace 3. producing C code and using the C compiler where the headers have no corresponding pkgconfig file
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/40.