Skip to content

feat(terraform): read installed module versions from .terraform/modules/modules.json - #705

Closed
noahkhomer18 wants to merge 1 commit into
vibgrate:mainfrom
noahkhomer18:feat/621-terraform-modules-json
Closed

noahkhomer18 wants to merge 1 commit into
vibgrate:mainfrom
noahkhomer18:feat/621-terraform-modules-json

Conversation

@noahkhomer18

Copy link
Copy Markdown
Contributor

Pull Request

Summary

vg scan now takes a registry module's resolvedVersion from .terraform/modules/modules.json, the file terraform init / tofu init writes, and computes drift from that installed version instead of the lowest version the constraint allows. Without the file, rows are unchanged.

As you suggested in #621, Source goes through publicRegistryModuleAddress (host stripped, as in #618) before matching, the file is read through the FileCache when there is one, and a missing or broken file falls back silently.

Three choices beyond the issue, all to keep the version from landing on the wrong row:

  • Each root reads its own file. Module pins now remember the directory of the file that declares them, and the scanner reads <that dir>/.terraform/modules/modules.json. A scan with envs/staging on ~> 4.0 and envs/prod on ~> 5.0 (the case the module-pin comment describes) resolves each from its own init, and a root that was never initialised stays unresolved even when another root has a modules.json.
  • Only the root's own calls count. modules.json also lists modules called from inside other modules (Key like network.vpc). Those entries are skipped, so a nested call's version doesn't attach to a root-level pin with the same source.
  • Two versions of one source in one root stay unresolved. With two module blocks on the same source at different versions, the source alone can't tell which entry is which, so neither gets a resolvedVersion.

Related issues

Closes #621

Checklist

  • pnpm test: 596 of 597 files and 5939 of 5940 tests pass. The one failure was test/freshness.test.ts › "serves the rebuilt graph after files change", a file-watch timing test that passes on its own (15/15, three runs in a row) and doesn't touch the Terraform scanner.
  • pnpm lint: 0 errors. The 5 warnings are the existing ones, none in files touched here.
  • pnpm exec tsc --noEmit is clean.
  • Docs updated: a sentence in DOCS.md under vg drift, next to the .terraform.lock.hcl note, and a CHANGELOG entry under Fixed.
  • No proprietary or internal references.
  • Commits use Conventional Commits and are signed off.

Tests

In test/terraform-scanner.test.ts, a new scanTerraform module resolved versions block:

  • ~> 5.0 with a modules.json entry at 5.8.1 reports resolvedVersion: '5.8.1' and drift: 'current' (the acceptance case);
  • envs/staging and envs/prod, each with its own modules.json, resolve to 4.0.2 and 5.8.1;
  • a root-level modules.json is not applied to a module declared in envs/staging;
  • no modules.json: resolvedVersion: null, drift: 'minor-behind', as before;
  • a malformed modules.json falls back, with and without a FileCache;
  • an entry for a different module, a nested call (network.vpc), and one source installed at two versions in one root all leave resolvedVersion: null.

With terraform-scanner.ts put back to main, the three tests that expect a resolved version fail; the others pass on both, as guards.

$ npx vitest run test/terraform-scanner.test.ts test/terraform-scanner-registry.test.ts test/terraform-module-source.test.ts
 Test Files  3 passed (3)
      Tests  41 passed (41)

…es/modules.json

After terraform init or tofu init, modules.json records the version installed for each module call. A registry module row now takes that Version as resolvedVersion, and drift is computed from it. Each root reads the modules.json next to the files that call the module, nested module calls are not matched, and a source installed at two versions in one root stays unresolved. A missing or malformed file leaves the row unchanged. Closes vibgrate#621.

Signed-off-by: Noah Khomer <108771853+noahkhomer18@users.noreply.github.com>
@noahkhomer18

Copy link
Copy Markdown
Contributor Author

Closing this: I missed that @hamid-digitalmarketer claimed #621 on #144, and you kept it with them when #655 was closed. Sorry for the noise. The branch stays on my fork in case it's ever useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: read resolved Terraform module versions from .terraform/modules/modules.json

1 participant