Hi, I'm trying to log a message during a stack tra...
# general
p
Hi, I'm trying to log a message during a stack transformation. Here's a dummy Pulumi program:
Copy code
"use strict";
const pulumi = require("@pulumi/pulumi");
const aws = require("@pulumi/aws");

pulumi.runtime.registerStackTransformation((args) => {
  pulumi.log.warn("dummy", args.resource);
  return undefined;
});

const bucket = new aws.s3.BucketV2("mybucket");
However, when I do
pulumi preview
(just to test it out), I get an error:
Copy code
Previewing update (dev):
     Type                 Name         Plan       Info
 +   pulumi:pulumi:Stack  test-js-dev  create     1 error

Diagnostics:
  pulumi:pulumi:Stack (test-js-dev):
    error: Running program '/home/xxx/index.js' failed with an unhandled exception:
    TypeError: Cannot read properties of undefined (reading 'promise')
        at log (/home/xxx/node_modules/@pulumi/log/index.ts:103:48)
        at Object.warn (/home/xxx/node_modules/@pulumi/log/index.ts:70:16)
        at /home/xxx/index.js:6:14
        at new Resource (/home/xxx/node_modules/@pulumi/resource.ts:410:26)
        at new CustomResource (/home/xxx/node_modules/@pulumi/resource.ts:976:9)
        at new BucketV2 (/home/xxx/node_modules/@pulumi/s3/bucketV2.ts:295:9)
        at Object.<anonymous> (/home/xxx/index.js:10:16)
        at Module._compile (node:internal/modules/cjs/loader:1369:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
        at Module.load (node:internal/modules/cjs/loader:1206:32)
Am I doing anything wrong or should I consider this a bug? The same thing works in Python without a problem.
l
Are you sure it's the stack transformation that's at fault? Other than the
return undefined
(which is technically wrong since that's a void function), your code is good. You could try using
console.log
to see if the problem is in
pulumi.log
, but I think that's unlikely. What is on line 103 of index.ts? Is there an object called promise whose value is undefined?
p
Yeah I’m pretty sure there’s a problem in the log function. See the issue I opened - https://github.com/pulumi/pulumi/issues/16654. Anyway thanks for the note about return, will take a look.
I think it's fine to return
undefined
from the transformation function, see https://github.com/pulumi/pulumi/blob/master/sdk/nodejs/resource.ts#L776.
l
Yes it's fine, JS doesn't complain about returning values from void functions. After all, it's JS, so it's completely untyped, even void doesn't mean anything :)
Given Justin's comment, the quick fix is to.use console.log instead of pulumi.log.
p
The thing is, the function that's returning
undefined
actually can return is as per the link a provided. It can return
ResourceTransformationResult | undefined
.
l
I'm looking at different code, I guess. I see
* parents walking from the resource up to the stack.
. In my editor, the intellisense is showing only that it's a void function. Odd.
p
Look again where I return undefined 🙂
👍 1