terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Confusing extra quoting in import error message when resource configuration isn't present.

Open bschaatsbergen opened this issue 1 year ago • 3 comments

It seems that when Terraform asks a provider to import something and the resource configuration isn't present in the working directory Terraform is producing an error message which includes the resource address wrapped in escaped quotes, rather than in the typical "naked" form:

~/go/bin/terraform import 'google_service_account.bqowner["bqowner"]' "projects/1238328119/serviceAccounts/foobar123"
Error: resource address "google_service_account.bqowner[\"bqowner\"]" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "google_service_account" "bqowner" {
  # (resource arguments)
}

Resource instance addresses should not be presented in quotes, because they aren't written in quotes in the configuration and because (as shown here) they sometimes contain quotes themselves, making the result misleading for someone who isn't familiar with Terraform's internals.

Similar to #34496, this was caused by an accidental use of %q instead of %s.

bschaatsbergen avatar Jan 07 '24 18:01 bschaatsbergen

Hi @bschaatsbergen,

Thanks for filing the issue. In this case, the quoted output was intentionally chosen because nearly all reports filed by users were due to incorrect command line arguments, rather than configuration, and is intended to show a format compatible with typical shell quoting rules. Most users seeing this error understand already how resource addresses are formatted in the configuration, but didn't properly quote the address argument.

I can see how this could be confusing if you were expecting the standard diagnostic format, but the import command is already a bit of an outlier in its operation. The new plan-able import blocks offer more flexibility, safety, and better diagnostic output.

jbardin avatar Jan 08 '24 13:01 jbardin

I see there are a couple PRs associated with the issue, but they do not fix the quoting problem, rather they remove the escaping of inner quotes which would result in incorrect quoting as opposed to extra quoting.

I think if we are going to change this, it would be to remove the quoting altogether, but I'll have others chime in if that's worth the change. The output from your example does correspond to the command shown, with "google_service_account.bqowner[\"bqowner\"]" being equivalent to 'google_service_account.bqowner["bqowner"]' directly above it. We could format the error in single quotes, but then a user inputting the former escape style would also get the same type of mismatch.

jbardin avatar Jan 08 '24 14:01 jbardin

Thank you for the reply, @jbardin. I'll keep this issue open until the team decides on a way to address it.

bschaatsbergen avatar Jan 28 '24 19:01 bschaatsbergen