pulumi/pkg/cmd/pulumi
Zaid Ajaj b3546c9fa4
[cli/import] Fix undefined variable errors in code generation when imported resources use a parent or provider (#16786)
Fixes #15410 Fixes #13339

## Problem Context

When using `pulumi import` we generate code snippets for the resources
that were imported. Sometimes the user specifies `--parent
parentName=URN` or `--provider providerName=URN` which tweak the parent
or provider that the imported resources uses. When using `--parent` or
`--provider` the generated code emits a resource option `parent =
parentName` (in case of using `--parent`) where `parentName` is an
unbound variable.

Usually unbound variables would result in a _bind_ error such as `error:
undefined variable parentName` when type-checking the program however in
the import code generation we specify the bind option
`pcl.AllowMissingVariables` which turns that unbound variable errors
into warnings and code generation can continue to emit code.

This is all good and works as expected. However in the issues linked
above, we do get an _error_ for unbound variables in generated code even
though we specified `AllowMissingVariables`.

The problem as it turns out is when we are trying to generate code via
dynamically loaded `LangaugeRuntime` plugins. Specifically for NodeJS
and Python, we load `pulumi-language-nodejs` or `pulumi-language-python`
and call `GenerateProgram` to get the generated program. That function
`GenerateProgram` takes the text _SOURCE_ of the a bound program (one
that was bound using option `AllowMissingVariables`) and re-binds again
inside the implementation of the language plugin. The second time we
bind the program, we don't pass it the option `AllowMissingVariables`
and so it fails with `unboud variable` error.

I've verified that the issue above don't repro when doing an import for
dotnet (probably same for java/yaml) because we use the statically
linked function `codegen/{lang}/gen_program.go -> GenerateProgram`

## Solution

The problem can be solved by propagating the bind options from the CLI
to the language hosts during import so that they know how to bind the
program. I've extended the gRPC interface in `GenerateProgramRequest`
with a property `Strict` which follows the same logic from `pulumi
convert --strict` and made it such that the import command sends
`strict=false` to the language plugins when doing `GenerateProgram`.
This is consistent with `GenerateProject` that uses the same flag. When
`strict=false` we use `pcl.NonStrictBindOptions()` which includes
`AllowMissingVariables` .

## Repro

Once can test the before and after behaviour by running `pulumi up
--yes` on the following TypeScript program:
```ts
import * as pulumi from "@pulumi/pulumi";
import * as random from "@pulumi/random";

export class MyComponent extends pulumi.ComponentResource {
    public readonly randomPetId: pulumi.Output<string>;
    constructor(name: string, opts?: pulumi.ComponentResourceOptions) {
        super("example:index:MyComponent", name, {}, opts);

        const randomPet = new random.RandomPet("randomPet", {}, { 
            parent: this 
        });

        this.randomPetId = randomPet.id;
        this.registerOutputs({
            randomPetId: randomPet.id,
        });
    }
}

const example = new MyComponent("example");
export const randomPetId = example.randomPetId;
``` 
Then running `pulumi import -f import.json` where `import.json` contains
a resource to be imported under the created component (stack=`dev`,
project=`importerrors`)
```ts
{
    "nameTable": {
        "parentComponent": "urn:pulumi:dev::importerrors::example:index:MyComponent::example"
    },
    "resources": [
        {
            "type": "random:index/randomPassword:RandomPassword",
            "name": "randomPassword",
            "id": "supersecret",
            "parent": "parentComponent"
        }
    ]
}
```
Running this locally I get the following generated code (which
previously failed to generate)
```ts
import * as pulumi from "@pulumi/pulumi";
import * as random from "@pulumi/random";

const randomPassword = new random.RandomPassword("randomPassword", {
    length: 11,
    lower: true,
    number: true,
    numeric: true,
    special: true,
    upper: true,
}, {
    parent: parentComponent,
});
```
2024-07-25 13:53:44 +00:00
..
convert_testdata Refactor convert command to add entrypoint for pulumi convert debugging 2022-10-01 15:40:17 -07:00
pcl_convert_testdata Support PCL as a convert input (and output) 2022-11-11 10:45:40 +00:00
testdata policy publish: default to default-org if possible (#14090) 2023-10-05 16:51:06 +00:00
about.go Fix panic in `about` when a token is malformed (#16710) 2024-07-18 21:40:52 +00:00
about_env.go [cli] Colorize table headers (#14557) 2023-11-14 22:52:57 +00:00
about_test.go Move about information to language plugins (#10392) 2022-08-15 14:55:04 +01:00
ai.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
ai_web.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
cancel.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
config.go Lift context parameter for ApplyProjectConfig (#16012) 2024-04-22 06:37:34 +00:00
config_env.go Fix non-interactive flag for 'new' and 'config env' commands (#16338) 2024-06-06 14:16:45 +00:00
config_env_add.go Fix non-interactive flag for 'new' and 'config env' commands (#16338) 2024-06-06 14:16:45 +00:00
config_env_add_test.go Fix TestConfigEnvXXX flakes (#16496) 2024-06-27 03:48:41 +00:00
config_env_init.go Fix non-interactive flag for 'new' and 'config env' commands (#16338) 2024-06-06 14:16:45 +00:00
config_env_init_test.go Fix TestConfigEnvXXX flakes (#16496) 2024-06-27 03:48:41 +00:00
config_env_ls.go Fix non-interactive flag for 'new' and 'config env' commands (#16338) 2024-06-06 14:16:45 +00:00
config_env_ls_test.go Fix TestConfigEnvXXX flakes (#16496) 2024-06-27 03:48:41 +00:00
config_env_rm.go Fix non-interactive flag for 'new' and 'config env' commands (#16338) 2024-06-06 14:16:45 +00:00
config_env_rm_test.go Fix TestConfigEnvXXX flakes (#16496) 2024-06-27 03:48:41 +00:00
config_env_test.go Fix TestConfigEnvXXX flakes (#16496) 2024-06-27 03:48:41 +00:00
config_test.go Lift context parameter for ApplyProjectConfig (#16012) 2024-04-22 06:37:34 +00:00
console.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
convert-trace.go `convert-trace` handles root spans with no TimespanEvent (#15508) 2024-02-26 23:32:14 +00:00
convert.go convert: make the help texts more consistent (#16739) 2024-07-22 12:31:10 +00:00
convert_test.go Allow specifying project names with `pulumi convert` (#16708) 2024-07-19 10:57:14 +00:00
crypto.go Fix TestDestroySetsEncryptionsalt test and resulting bug (#15432) 2024-02-15 09:33:27 +00:00
deployment_run.go Reorganize deployment settings commands (#16581) 2024-07-04 13:04:23 +00:00
deployment_settings_config.go Unify user prompts + cleanups + previous feedback (#16585) 2024-07-12 16:01:13 +00:00
deployment_settings_ops.go Unify user prompts + cleanups + previous feedback (#16585) 2024-07-12 16:01:13 +00:00
destroy.go Update pu/pu to support deployment run command (#16492) 2024-07-01 14:18:44 +00:00
doc.go Document Go packages (#6009) 2021-01-11 11:07:59 -07:00
env.go [cli] Include config from ESC in `pulumi config` (#14560) 2023-11-21 10:44:45 +00:00
errors.go Clean up uses of .Error() (#14965) 2023-12-20 15:54:06 +00:00
flags_test.go Set the --continue-on-error flag with PULUMI_CONTINUE_ON_ERROR environment variable (#16442) 2024-06-25 08:28:37 +00:00
gen_completion.go chore: fix function names in comment (#16044) 2024-04-24 06:23:35 +00:00
gen_markdown.go Consecutive markdown heading levels CLI docs (#13822) 2023-09-28 17:36:24 +00:00
import.go [cli/import] Fix undefined variable errors in code generation when imported resources use a parent or provider (#16786) 2024-07-25 13:53:44 +00:00
import_test.go Add a missing test for the import system (#15664) 2024-03-13 14:13:36 +00:00
install.go Install policy or project dependencies based on nearest configuration file (#16631) 2024-07-12 08:56:18 +00:00
login.go Rename filestate to DIY (#15314) 2024-01-30 15:53:10 +00:00
logout.go Remove Logout logic from Backend interface (#13951) 2023-09-18 17:21:32 +00:00
logs.go Lift context parameter for ApplyProjectConfig (#16012) 2024-04-22 06:37:34 +00:00
logs_test.go Use `assert.NoError` rather than `assert.Nil` (#14233) 2023-10-13 09:46:07 +00:00
main.go avoid duplicate error output in some cases (#16706) 2024-07-19 08:00:51 +00:00
new.go new: capitalize prompts consistently (#16747) 2024-07-24 07:56:18 +00:00
new_acceptance_test.go new: capitalize prompts consistently (#16747) 2024-07-24 07:56:18 +00:00
new_ai.go upgrade to latest version of golangci-lint (#15977) 2024-04-19 06:20:33 +00:00
new_ai_test.go Fix AI prompt repetition on Windows (#15010) 2024-01-03 23:03:11 +00:00
new_test.go new: capitalize prompts consistently (#16747) 2024-07-24 07:56:18 +00:00
org.go Update set-default command description (#16233) 2024-05-23 19:38:54 +00:00
org_search.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
org_search_ai.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
org_search_ai_test.go Replace some more uses of assert.Contains(err.Error()) with assert.ErrorContains (#14863) 2023-12-15 17:45:32 +00:00
org_search_test.go Enable perfsprint linter (#14813) 2023-12-12 12:19:42 +00:00
package.go Refactor MissingError handling (#16689) 2024-07-18 08:33:09 +00:00
package_extract_mapping.go Normalize plugin.Provider methods to (Context, Request) -> (Response, error) (#16302) 2024-06-07 19:47:49 +00:00
package_extract_schema.go Support Parameterize in get-schema, gen-sdk (#16210) 2024-05-20 07:58:00 +00:00
package_gen_sdk.go Support Parameterize in get-schema, gen-sdk (#16210) 2024-05-20 07:58:00 +00:00
package_pack_sdk.go Add SupportPack to schemas to write out in the new style (#15713) 2024-03-22 09:25:46 +00:00
package_publish.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
plugin.go Add `plugin run` command (#12613) 2024-02-05 08:35:48 +00:00
plugin_install.go Refactor: move plugin kind to apitype (#15946) 2024-04-25 17:30:30 +00:00
plugin_install_test.go Refactor: move plugin kind to apitype (#15946) 2024-04-25 17:30:30 +00:00
plugin_ls.go [cli] Colorize table headers (#14557) 2023-11-14 22:52:57 +00:00
plugin_rm.go Refactor: move plugin kind to apitype (#15946) 2024-04-25 17:30:30 +00:00
plugin_run.go Refactor MissingError handling (#16689) 2024-07-18 08:33:09 +00:00
policy.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
policy_disable.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
policy_enable.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
policy_group_ls.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
policy_ls.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
policy_new.go Make pulumi install work for policy packs (#16438) 2024-06-21 19:19:21 +00:00
policy_new_acceptance_test.go Use context.Background in tests (#14029) 2023-09-25 12:25:26 +00:00
policy_new_test.go Use context.Background in tests (#14029) 2023-09-25 12:25:26 +00:00
policy_publish.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
policy_publish_test.go policy publish: default to default-org if possible (#14090) 2023-10-05 16:51:06 +00:00
policy_rm.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
policy_validate.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
preview.go Update pu/pu to support deployment run command (#16492) 2024-07-01 14:18:44 +00:00
preview_test.go Fix hang in --preview-file (#15203) 2024-01-25 18:03:38 +00:00
pulumi.go Disable default Cobra completion commands (#16540) 2024-07-01 14:28:33 +00:00
pulumi_test.go correct version check when we have a dev version installed (#14954) 2023-12-22 16:40:12 +00:00
query.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
refresh.go avoid duplicate error: prefix with `--expect-no-changes` (#16705) 2024-07-19 08:01:18 +00:00
replay_events.go Add `--suppress-progresss` flag to CLI (#14690) 2024-02-05 11:48:10 +00:00
schema.go [codegen/schema] Add a schema checker (#7865) 2021-08-30 19:29:24 -07:00
schema_check.go all: Drop ioutil 2023-01-06 16:35:14 -08:00
stack.go Show a fully qualified stack name in `pulumi stack --show-name -Q` (#16453) 2024-06-26 09:00:22 +00:00
stack_change_secrets_provider.go Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929) 2024-04-15 07:45:46 +00:00
stack_change_secrets_provider_test.go Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929) 2024-04-15 07:45:46 +00:00
stack_export.go Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929) 2024-04-15 07:45:46 +00:00
stack_graph.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
stack_graph_test.go Adds a flag to graph command to insert fragment (#14858) 2024-01-08 22:03:08 +00:00
stack_history.go Revert "The `--expect-no-changes` flag checks for output diffs" (#16131) 2024-05-06 17:34:24 +00:00
stack_import.go Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929) 2024-04-15 07:45:46 +00:00
stack_init.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
stack_init_test.go service to cloud 2023-04-08 09:49:37 -07:00
stack_ls.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
stack_ls_test.go [test] fix `TestCreatingProjectWithEmptyConfig` flakiness (#14777) 2023-12-08 23:00:49 +00:00
stack_output.go Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929) 2024-04-15 07:45:46 +00:00
stack_output_fuzz_test.go cli/stack-ouput/test: Fix Powershell test failures 2023-01-30 07:58:47 -08:00
stack_output_test.go fix(cli/stack output): Don't escape HTML characters 2023-06-23 09:11:20 -07:00
stack_rename.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
stack_rm.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
stack_select.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
stack_tag.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
stack_test.go Show a fully qualified stack name in `pulumi stack --show-name -Q` (#16453) 2024-06-26 09:00:22 +00:00
stack_unselect.go include error in formatted message and explicitly check for an empty stack 2022-03-14 11:32:33 +01:00
state.go implement skeleton command for `pulumi state move` (#16493) 2024-06-28 09:43:19 +00:00
state_delete.go State: fix panic when deleting non-unique Provider (#15322) 2024-02-05 16:21:38 +00:00
state_edit.go upgrade to latest version of golangci-lint (#15977) 2024-04-19 06:20:33 +00:00
state_edit_encoder.go Lift context parameter to SerializeDeployment/Resource/Operations/Properties (#15929) 2024-04-15 07:45:46 +00:00
state_edit_test.go Fix context in state edit command (#15354) 2024-02-07 09:01:56 +00:00
state_move.go state_move improve output slightly (#16780) 2024-07-24 14:30:38 +00:00
state_move_test.go state_move improve output slightly (#16780) 2024-07-24 14:30:38 +00:00
state_rename.go Fix state renames involving `DeletedWith` (#16260) 2024-05-24 15:09:33 +00:00
state_rename_test.go Allow anything in resource names (#14107) 2023-11-20 08:59:00 +00:00
state_unprotect.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
state_upgrade.go Add --yes to state upgrade (#15648) 2024-03-12 19:57:41 +00:00
state_upgrade_test.go Add --yes to state upgrade (#15648) 2024-03-12 19:57:41 +00:00
terminal.go crypto/ssh/terminal is deprecated 2023-01-12 09:07:34 -08:00
terminal_test.go Fix negative page size panic (#10475) 2022-08-23 16:45:47 -04:00
up.go avoid duplicate error: prefix with `--expect-no-changes` (#16705) 2024-07-19 08:01:18 +00:00
up_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
util.go Unify user prompts + cleanups + previous feedback (#16585) 2024-07-12 16:01:13 +00:00
util_remote.go Update pu/pu to support deployment run command (#16492) 2024-07-01 14:18:44 +00:00
util_remote_test.go [cli] Experimental support for remote operations 2022-10-27 16:15:59 -07:00
util_test.go chore: fix function names in comment (#16044) 2024-04-24 06:23:35 +00:00
version.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
view-trace.go Reimport appdash from our mirror (#14701) 2023-11-30 14:21:35 +00:00
watch.go Change `pulumi refresh` to report diff relative to desired state instead of relative to only output changes (#16146) 2024-06-12 16:17:05 +00:00
whoami.go Propagte cobra command context correctly (#15312) 2024-01-30 15:50:39 +00:00
whoami_test.go Fix panic in whoami (#14108) 2023-10-06 16:56:36 +00:00