Skip to content

fix(provider-generator): stop a provider_functions resource clobbering the functions submodule - #403

Open
jsteinich wants to merge 1 commit into
open-constructs:mainfrom
jsteinich:fix/provider-functions-folder-collision
Open

fix(provider-generator): stop a provider_functions resource clobbering the functions submodule#403
jsteinich wants to merge 1 commit into
open-constructs:mainfrom
jsteinich:fix/provider-functions-folder-collision

Conversation

@jsteinich

Copy link
Copy Markdown
Contributor

Related issue

Follow-up from the review on #400, point 1. #400 is closed; this is the part of it worth keeping, re-scoped and without the submodule rename.

Description

A provider that declares provider-defined functions emits them to providers/<provider>/provider-functions/index.ts. A resource or data source named <provider>_provider_functions sanitizes to the base name provider_functions, which getFileName maps to that same directory — so the resource silently overwrites the functions wrapper.

This is a live gap on main, independent of #400's rename. sanitizeClassOrNamespaceName already reserves function (singular), license, version and the TypeScript keywords, and gives a resource named provider the _resource suffix for exactly this reason — but nothing covers provider_functions.

What actually happens today

Generating the new fixture (a provider with one function plus an example_provider_functions resource) against main produces:

providers/example/index.ts
providers/example/lazy-index.ts
providers/example/provider-functions/README.md
providers/example/provider-functions/index.ts

provider-functions/index.ts contains export class ProviderFunctions extends cdktn.TerraformResource — the resource, not the functions wrapper. The provider's functions are gone, and index.ts reads:

export * as providerFunctions from './provider-functions/index';
export * as providerFunctions from './provider-functions/index';

The duplicate name is a TypeScript error, so the generated bindings do not compile. lazy-index.ts gets the same duplication.

The fix

Extend the existing provider special case to provider_functions, so the resource lands in provider-functions-resource/ and both are exported under distinct names.

Only provider_functions is reserved. functions is not a directory the generator emits, so a resource by that name does not collide today — and as noted in the #400 review, provider_functions is a far less likely resource name than functions, which is a point in favour of the current layout.

Testing

New fixture and test asserting the three things that break without the guard: the functions submodule holds the wrapper rather than the resource, the resource gets its own directory, and the provider index exports no duplicate names. Plus unit assertions on sanitizeClassOrNamespaceName.

Confirmed to be a real regression guard — with the guard reverted, 3 of the 6 fail; with it, all pass.

Full @cdktn/provider-generator suite: 23 suites / 112 tests / 101 snapshots pass. nx lint clean.

Checklist

  • I have updated the PR title to match CDKTN's style guide
  • I have run the linter on my code locally
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation if applicable — n/a
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes

🤖 Generated with Claude Code

…g the functions submodule

A provider that declares provider-defined functions emits them to
providers/<provider>/provider-functions/index.ts. A resource or data source
named `<provider>_provider_functions` sanitizes to the base name
`provider_functions`, which `getFileName` maps to that same directory - so
the resource silently overwrites the functions wrapper.

Generating a schema with both (new fixture) produces, before this change:

  providers/example/provider-functions/index.ts   <- the RESOURCE, not the
                                                     functions wrapper
  providers/example/index.ts                      <- exports the name
                                                     `providerFunctions` twice

The duplicate `export * as providerFunctions` is a TypeScript error, so the
generated bindings do not compile, and the provider's functions are gone.

`sanitizeClassOrNamespaceName` already gives a resource named `provider` the
`_resource` suffix for exactly this reason; this extends that to
`provider_functions`, so the resource lands in
`provider-functions-resource/` and both are exported under distinct names.

Only `provider_functions` is reserved. `functions` is not a directory the
generator emits, so a resource by that name does not collide today, and
reserving names speculatively would rename resources for no benefit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

1 participant