adobe-photoshop / generator-core

Core Node.js library for Adobe Photoshop CC's Generator extensibility layer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useFlite set to true causes exception when saving Pixmaps

pixels4nickels opened this issue · comments

About 9 weeks ago I filed an issue about the streamPixmap function blowing up due to a missing the logger argument. I still cannot find the commit which added the logger argument. But in my logs I still get:
Uncaught exception on Thu Jul 20 2017 15:29:12 GMT-0700 (PDT): Error trace: TypeError: Cannot read property 'log' of undefined at streamPixmap (/Applications/Adobe Photoshop CC 2017/Adobe Photoshop CC 2017.app/Contents/Required/Generator-builtin/lib/convert.js:229:19) at Object.savePixmap (/Applications/Adobe Photoshop CC 2017/Adobe Photoshop CC 2017.app/Contents/Required/Generator-builtin/lib/convert.js:276:16) at Generator.savePixmap (/Applications/Adobe Photoshop CC 2017/Adobe Photoshop CC 2017.app/Contents/Required/Generator-builtin/lib/generator.js:1610:24) at /Applications/Adobe Photoshop CC 2017/Plug-ins/Generator/lsr-generator/png_generator.js:217:28 at /Applications/Adobe Photoshop CC 2017/Plug-ins/Generator/lsr-generator/node_modules/fs-extra/lib/mkdir.js:28:14 at FSReqWrap.oncomplete (evalmachine.<anonymous>:82:15)
This was happening because the logger function argument was missing but there was usage of it at line 229 Convert.js. If you set useFlite to true then you enter into that condition, which causes the exception.
I now see that the code has the logger argument yet I am still getting this error.

It almost feels like the old code is still running somewhere because I cannot see how it can be undefined now.
Current Convert.js is
function streamPixmap(binaryPaths, pixmap, outputStream, settings, logger) {

Back when I originally encountered this bug it read:
function streamPixmap(binaryPaths, pixmap, outputStream, settings) {

It almost feels like the internal Generator is keeping the old files cached or something? Really odd stuff. Thanks for any guidance in clearing the internal Generator node cache.

Okay I see why this *seems to be happening still. It actually is a different reason that can cause the exact same exception. So the savePixmap call is not passing the logger argument, which (if you useFlite) will cause the logging statement on 229 to fail.

`function savePixmap(binaryPaths, pixmap, path, settings) {
var fs = require("fs");

    // Open a stream to the output file.
    var fileStream = fs.createWriteStream(path);

    // Stream the pixmap into the file and resolve with path when successful.
    return streamPixmap(binaryPaths, pixmap, fileStream, settings)
        .thenResolve(path)
        .catch(function (err) {
            // If an error occurred, clean up the file.
            try {
                fileStream.close();
            } catch (e) {
                console.error("Error when closing file stream", e);
            }
            try {
                if (fs.existsSync(path)) {
                    fs.unlinkSync(path);
                }
            } catch (e) {
                console.error("Error when deleting file", path, e);
            }

            // Propagate the error.
            throw err;
        });
}`

The streamPixmap expects the logger instance:
function streamPixmap(binaryPaths, pixmap, outputStream, settings, logger) {

Changing line 218 from:
function streamPixmap(binaryPaths, pixmap, outputStream, settings, logger) {
to:
function streamPixmap(binaryPaths, pixmap, outputStream, settings) {

And changing line 229 from:
logger.log("Using Flite for image encoding" + (pixmap.iccProfile ? " with icc profile" : ""));

to:
console.log("Using Flite for image encoding" + (pixmap.iccProfile ? " with icc profile" : ""));

Fixes the issue.

I created a PR (#393) that I believe addresses this issue in a slightly better way than #390. @pixels4nickels please let me know if you agree.

If you want to pass the logger around, sure. More than anything I would love to see some tests being run since this issue would have never made it out into the wild if folks had at minimum "hand-tested" the feature. I am concerned about the brittleness of the access and saving of profiles to image headers. Thanks for your attention to this.

Apologies, I now see some tests when running locally. It would be great to get the flite usage in there.

Regarding tests: Our most comprehensive set of tests exercises the generator-assets plugin which no longer calls savePixmap (it uses streamPixmap directly). This is not an excuse, but an explanation at least.

fix included in Photoshop 2018