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

Migrate appends a new copy of the file each time it is executed. #362

Closed
1 task done
sheneska opened this issue Apr 23, 2024 · 1 comment · Fixed by #364
Closed
1 task done

Migrate appends a new copy of the file each time it is executed. #362

sheneska opened this issue Apr 23, 2024 · 1 comment · Fixed by #364
Assignees
Labels
bug Something isn't working
Milestone

Comments

@sheneska
Copy link

Terraform CLI and terraform-plugin-docs Versions

github.com/hashicorp/terraform-plugin-docs v0.19.0

Provider Code

N/A

Expected Behavior

When migrate is executed it creates new templates from existing documentation.

Actual Behavior

Each time the migrate command is executed, it appends a copy of the previous file to the existing template.

Steps to Reproduce

  1. tfplugindocs migrate

How much impact is this issue causing?

Medium

Logs

No response

Additional Information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@sheneska sheneska added the bug Something isn't working label Apr 23, 2024
@bflad bflad self-assigned this Apr 26, 2024
@bflad bflad added this to the v0.19.2 milestone Apr 26, 2024
@bflad
Copy link
Member

bflad commented Apr 26, 2024

Hi @sheneska 👋 Thank you for raising this and sorry for the trouble here. I was able to reproduce this in our testing and this will be fixed in the next release. Thanks again for the report!

bflad added a commit that referenced this issue Apr 26, 2024
Reference: #362

To reproduce, copied an existing test case and ensured the test script included multiple `exec tfplugindocs migrate` invocations. This generated a test failure matching the bug report:

```
--- FAIL: Test_SchemaJson_MigrateAcceptanceTests (0.00s)
    --- FAIL: Test_SchemaJson_MigrateAcceptanceTests/time_provider_multiple_runs (0.32s)
        testscript.go:558: # Copyright (c) HashiCorp, Inc.
            # SPDX-License-Identifier: MPL-2.0
            # Multiple runs of tfplugindocs -migrate to verify idempotency (0.000s)
            # Run migrate command (0.301s)
            # Check template files (0.000s)
            > cmpenv templates/index.md.tmpl exp-templates/index.md.tmpl
            diff templates/index.md.tmpl exp-templates/index.md.tmpl
            --- templates/index.md.tmpl
            +++ exp-templates/index.md.tmpl
            @@ -31,36 +31,3 @@
             `triggers` are *not* treated as sensitive attributes; a value used for `triggers` will be displayed in Terraform UI output as plaintext.

             To force a these actions to reoccur without updating `triggers`, the [`terraform taint` command](https://www.terraform.io/docs/commands/taint.html) can be used to produce the action on the next run.
            ----
            -page_title: "Provider: Time"
            -description: |-
            -  The time provider is used to interact with time-based resources.
            ----
            -
            -{{/* This template serves as a starting point for documentation generation, and can be customized with hardcoded values and/or doc gen templates.
            -
            -For example, the {{ .SchemaMarkdown }} template can be used to replace manual schema documentation if descriptions of schema attributes are added in the provider source code. */ -}}
            -
            -# Time Provider
            -
            -The time provider is used to interact with time-based resources. The provider itself has no configuration options.
            -
            -Use the navigation to the left to read about the available resources.
            -
            -## Resource "Triggers"
            -
            -Certain time resources, only perform actions during specific lifecycle actions:
            -
            -- `time_offset`: Saves base timestamp into Terraform state only when created.
            -- `time_sleep`: Sleeps when created and/or destroyed.
            -- `time_static`: Saves base timestamp into Terraform state only when created.
            -
            -These resources provide an optional map argument called `triggers` that can be populated with arbitrary key/value pairs. When the keys or values of this argument are updated, Terraform will re-perform the desired action, such as updating the base timestamp or sleeping again.
            -
            -For example:
            -
            -{{tffile "examples/example_1.tf"}}
            -
            -`triggers` are *not* treated as sensitive attributes; a value used for `triggers` will be displayed in Terraform UI output as plaintext.
            -
            -To force a these actions to reoccur without updating `triggers`, the [`terraform taint` command](https://www.terraform.io/docs/commands/taint.html) can be used to produce the action on the next run.

            FAIL: testdata/scripts/schema-json/migrate/time_provider_multiple_runs.txtar:13: templates/index.md.tmpl and exp-templates/index.md.tmpl differ
```

To fix this, changed the internal logic from opening the template file multiple times with `os.O_APPEND` to opening the template file once with only `os.O_WRONLY|os.O_CREATE` and passing the file handle around instead.
bflad added a commit that referenced this issue Apr 26, 2024
Reference: #362

To reproduce, copied an existing test case and ensured the test script included multiple `exec tfplugindocs migrate` invocations. This generated a test failure matching the bug report:

```
--- FAIL: Test_SchemaJson_MigrateAcceptanceTests (0.00s)
    --- FAIL: Test_SchemaJson_MigrateAcceptanceTests/time_provider_multiple_runs (0.32s)
        testscript.go:558: # Copyright (c) HashiCorp, Inc.
            # SPDX-License-Identifier: MPL-2.0
            # Multiple runs of tfplugindocs -migrate to verify idempotency (0.000s)
            # Run migrate command (0.301s)
            # Check template files (0.000s)
            > cmpenv templates/index.md.tmpl exp-templates/index.md.tmpl
            diff templates/index.md.tmpl exp-templates/index.md.tmpl
            --- templates/index.md.tmpl
            +++ exp-templates/index.md.tmpl
            @@ -31,36 +31,3 @@
             `triggers` are *not* treated as sensitive attributes; a value used for `triggers` will be displayed in Terraform UI output as plaintext.

             To force a these actions to reoccur without updating `triggers`, the [`terraform taint` command](https://www.terraform.io/docs/commands/taint.html) can be used to produce the action on the next run.
            ----
            -page_title: "Provider: Time"
            -description: |-
            -  The time provider is used to interact with time-based resources.
            ----
            -
            -{{/* This template serves as a starting point for documentation generation, and can be customized with hardcoded values and/or doc gen templates.
            -
            -For example, the {{ .SchemaMarkdown }} template can be used to replace manual schema documentation if descriptions of schema attributes are added in the provider source code. */ -}}
            -
            -# Time Provider
            -
            -The time provider is used to interact with time-based resources. The provider itself has no configuration options.
            -
            -Use the navigation to the left to read about the available resources.
            -
            -## Resource "Triggers"
            -
            -Certain time resources, only perform actions during specific lifecycle actions:
            -
            -- `time_offset`: Saves base timestamp into Terraform state only when created.
            -- `time_sleep`: Sleeps when created and/or destroyed.
            -- `time_static`: Saves base timestamp into Terraform state only when created.
            -
            -These resources provide an optional map argument called `triggers` that can be populated with arbitrary key/value pairs. When the keys or values of this argument are updated, Terraform will re-perform the desired action, such as updating the base timestamp or sleeping again.
            -
            -For example:
            -
            -{{tffile "examples/example_1.tf"}}
            -
            -`triggers` are *not* treated as sensitive attributes; a value used for `triggers` will be displayed in Terraform UI output as plaintext.
            -
            -To force a these actions to reoccur without updating `triggers`, the [`terraform taint` command](https://www.terraform.io/docs/commands/taint.html) can be used to produce the action on the next run.

            FAIL: testdata/scripts/schema-json/migrate/time_provider_multiple_runs.txtar:13: templates/index.md.tmpl and exp-templates/index.md.tmpl differ
```

To fix this, changed the internal logic from opening the template file multiple times with `os.O_APPEND` to opening the template file once with only `os.O_WRONLY|os.O_CREATE` and passing the file handle around instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants