pulumi / pulumi-random

A Pulumi provider that safely enables randomness for resources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Apply callback on RandomInteger.result is executed in `pulumi preview` with string value

DSoko2 opened this issue · comments

What happened?

Running this program with pulumi preview on a new stack should not log anything, because the result value is still unknown.

import * as random from "@pulumi/random";
new random.RandomInteger('i', {min:0 , max: 10}).result.apply(console.log);

However, the callback is executed with the string 3eeb2bf0-c639-47a8-9e75-3b44932eb421. Other resources, e.g., RandomString, do not execute the apply callbck before the resource was deployed and a random value is known.

Expected Behavior

The apply callback is only executed if the result value is known, i.e., not when pulumi preview is executed in a new stack.

Steps to reproduce

Run pulumi preview on this Pulumi TypeScript program:

import * as random from "@pulumi/random";
new random.RandomInteger('k', {min:0 , max: 10}).result.apply(console.log);

Output of pulumi about

CLI
Version 3.55.0
Go Version go1.19.5
Go Compiler gc

Plugins
NAME VERSION
aws 5.30.0
awsx 1.0.2
docker 3.6.1
nodejs unknown
random 4.11.2

Host
OS darwin
Version 13.2
Arch x86_64

This project is written in nodejs: executable='/usr/local/bin/node' version='v19.7.0'

Backend
Name eduroamverw-130-82-28-71.unisg.ch
URL file://~
User daniel
Organizations

Dependencies:
NAME VERSION
@pulumi/pulumi 3.55.0
@pulumi/random 4.11.2
@types/node 16.18.14
@pulumi/aws 5.30.0
@pulumi/awsx 1.0.2

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction.
To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

Can confirm this, and I can also confirm that this is a regression from 4.8.2 for us; we just bumped dependencies to pulumi-random v4.11.2 three days ago and already random numbers started failing.

Confirming! Thanks for reporting this, this is indeed a bug, possibly with Plugin Framework support in the bridge.

I'm digging into this. Currently can repro.

3eeb2bf0-c639-47a8-9e75-3b44932eb421 is a special sentinel value for "Unknown Number Value".

https://github.com/pulumi/pulumi/blob/b6b894ee5de1685e8b97de435f87ffae06dca5d4/sdk/go/common/resource/plugin/rpc.go#L51-#L51

IN this example, Pulumi CLI talks to the provider like this over gRPC.

PULUMI_DEBUG_GPRC=$PWD/debug.json pulumi preview

{
  "method": "/pulumirpc.ResourceProvider/Create",
  "request": {
    "urn": "urn:pulumi:dev::repro::random:index/randomInteger:RandomInteger::k",
    "properties": {
      "max": 10,
      "min": 0
    },
    "preview": true
  },
  "response": {
    "properties": {
      "id": "04da6b54-80e4-46f7-96ec-b56ff0331ba9",
      "max": 10,
      "min": 0,
      "result": "3eeb2bf0-c639-47a8-9e75-3b44932eb421"
    }
  },
  "metadata": {
    "kind": "resource",
    "mode": "client",
    "name": "random"
  }
}

"04da6b54-80e4-46f7-96ec-b56ff0331ba9" is likewise a sentinel value for UNKNOWN.

Then Pulumi CLI responds to the Node program with this:

{
  "method": "/pulumirpc.ResourceMonitor/RegisterResource",
  "request": {
    "type": "random:index/randomInteger:RandomInteger",
    "name": "k",
    "parent": "urn:pulumi:dev::repro::pulumi:pulumi:Stack::repro-dev",
    "custom": true,
    "object": {
      "max": 10,
      "min": 0
    },
    "propertyDependencies": {
      "max": {},
      "min": {}
    },
    "version": "4.11.2",
    "acceptSecrets": true,
    "customTimeouts": {},
    "supportsPartialValues": true,
    "acceptResources": true
  },
  "response": {
    "urn": "urn:pulumi:dev::repro::random:index/randomInteger:RandomInteger::k",
    "object": {
      "id": "04da6b54-80e4-46f7-96ec-b56ff0331ba9",
      "max": 10,
      "min": 0,
      "result": "3eeb2bf0-c639-47a8-9e75-3b44932eb421"
    }
  },
  "metadata": {
    "mode": "server"
  }
}

On 4.8.2 we have:

{
  "method": "/pulumirpc.ResourceProvider/Create",
  "request": {
    "urn": "urn:pulumi:dev::repro::random:index/randomInteger:RandomInteger::k",
    "properties": {
      "__defaults": [],
      "max": 10,
      "min": 0
    },
    "preview": true
  },
  "response": {
    "properties": {
      "id": "",
      "max": 10,
      "min": 0
    }
  },
  "metadata": {
    "kind": "resource",
    "mode": "client",
    "name": "random"
  }
}
{
  "method": "/pulumirpc.ResourceMonitor/RegisterResource",
  "request": {
    "type": "random:index/randomInteger:RandomInteger",
    "name": "k",
    "parent": "urn:pulumi:dev::repro::pulumi:pulumi:Stack::repro-dev",
    "custom": true,
    "object": {
      "max": 10,
      "min": 0
    },
    "propertyDependencies": {
      "max": {},
      "min": {}
    },
    "version": "4.8.2",
    "acceptSecrets": true,
    "customTimeouts": {},
    "supportsPartialValues": true,
    "acceptResources": true
  },
  "response": {
    "urn": "urn:pulumi:dev::repro::random:index/randomInteger:RandomInteger::k",
    "object": {
      "id": "",
      "max": 10,
      "min": 0
    }
  },
  "metadata": {
    "mode": "server"
  }
}

Hm curiously only the "result" sentinel has this behavior. If I try this:

import * as pulumi from "@pulumi/pulumi";
import * as random from "@pulumi/random";

const r = new random.RandomInteger('k', {min:0 , max: 10});

r.result.apply(x => console.log("result", x));
r.id.apply(x => console.log("id", x));

Then "result" gets printed, but "id" does not, as expected.