Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for built-in functions #145

Merged
merged 16 commits into from
Feb 20, 2023
Merged

Add support for built-in functions #145

merged 16 commits into from
Feb 20, 2023

Conversation

dbanck
Copy link
Member

@dbanck dbanck commented Sep 23, 2022

This PR adds the function signatures for all previous Terraform versions, starting with 0.12. For versions >= 1.4, we use the new terraform metadata functions -json command to generate the signatures.

The changes are currently a no-op for users. However, we will follow up with modifications to the language server to obtain the functions for a specific Terraform version using FunctionsForVersion.

I've created a list of all Terraform functions with their introduction versions for a better overview and used that as a source for all the signatures.

TODO


@dbanck dbanck force-pushed the f-function-support branch 3 times, most recently from 3240078 to 2edfec1 Compare February 15, 2023 13:56
@dbanck dbanck marked this pull request as ready for review February 15, 2023 14:02
@dbanck dbanck requested a review from a team as a code owner February 15, 2023 14:02
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is now expected, until 1.4.0 final actually lands, just surfacing it here for posterity:

2023/02/16 12:39:16 ensuring terraform is installed
2023/02/16 12:39:17 failed to obtain product version from "https://releases.hashicorp.com/terraform/1.4.0/index.json": 404 Not Found
exit status 1
internal/funcs/generated/doc.go:4: running "go": exit status 1

I left some comments in-line, mostly minor, but the escaping one should certainly be resolved before we merge it, especially if it affects upstream as well (i.e. the JSON TF Core produces is HTML escaped).

},
},
ReturnType: cty.String,
Description: "`base64sha256` computes the SHA256 hash of a given string and encodes it with Base64. This is not equivalent to `base64encode(sha256("test"))` since `sha256()` returns hexadecimal representation.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where this came from and whether it affects other places in Core as well, but I see some HTML escaping in effect (&#...) which probably shouldn't be.

The context in which we need to escape the (Markdown) description text would be single-line Go string, i.e. " would need to be \" but that's about it. Single quotes and probably most other characters can stay as-is.

I don't know if there's any escaping function we could use in this context in the generator, here I suppose it's just a simple matter of manual Find+Replace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I fixed it in Terraform in hashicorp/terraform#32710 and replaced all occurrences here.


This package can generate function signature files for Terraform >= 1.4 automatically.

It is intended to run whenever HashiCorp releases a new Terraform. If the Terraform version contains updated function signatures, it generates a new file for that version. When no changes are detected, one should commit the version bump in `gen/gen.go`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It is intended to run whenever HashiCorp releases a new Terraform. If the Terraform version contains updated function signatures, it generates a new file for that version. When no changes are detected, one should commit the version bump in `gen/gen.go`.
It is intended to run whenever HashiCorp releases a new Terraform version. If the Terraform version contains updated function signatures, it generates a new file for that version. When no changes are detected, one should commit the version bump in `gen/gen.go`.

"escapeDescription": escapeDescription,
}).Parse(outputTpl)
if err != nil {
log.Fatal(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I know this is a script which will be executed by a maintainer, but it's still a good practice for any downstream functions to return error and only the top level to actually deal with surfacing it somehow.

This enables testability and reusability and helps readability (it's easier to understand the flow of the program as a whole), even though admittedly we probably won't be testing or reusing this code any time soon.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, missed updating the error handling for the two templating functions. Thanks for pointing it out.

}

func escapeDescription(description string) string {
return strings.ReplaceAll(description, `\`, `\\`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again - not sure if the HTML escape characters come already in the JSON output - which would be something to address upstream, but either way we should make sure that double quotes are escaped correctly here too.

}
}

return nil, NoCompatibleFunctionsErr{Constraints: vc}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this should never happen, I'd consider not exporting this error - since we should never be catching it in LS.

We could go as far as panicing here, but it's probably still better not to, since it's a library and as such it should not decide how errors are surfaced. LS could however turn that error into panic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — replaced it with a simple error string which we can turn into a panic upstream.

)

func FunctionsForVersion(v *version.Version) (map[string]schema.FunctionSignature, error) {
ver, err := semVer(v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the semVer predates the Core() method upstream, which could now be used directly and we could get rid of semVer().

Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants