Skip to content

(init templates): (go example causes an error using go tools) #33554

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

Open
1 task
4k1k0 opened this issue Feb 22, 2025 · 2 comments
Open
1 task

(init templates): (go example causes an error using go tools) #33554

4k1k0 opened this issue Feb 22, 2025 · 2 comments
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort jsii This issue originates in jsii, or this feature must be implemented in jsii. p2 package/tools Related to AWS CDK Tools or CLI

Comments

@4k1k0
Copy link

4k1k0 commented Feb 22, 2025

Describe the bug

The project contains examples of AWS CDK projects inside node_modules/aws-cdk/lib/init-templates, including an example for a Go project. This setup causes Go tools to incorrectly scan these files, leading to failures. Specifically, golangci-lint encounters a compile error and cannot generate expected warnings and errors.

aws-cdk version 2.1000.2
golangci-lint has version 1.59.0 built with go1.22.3 from 2059b18a on 2024-05-26T18:13:27Z
go version go1.22.3 darwin/arm64

There is an open discussion about this topic in Go's repository.
golang/go#42965

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

Do not include go code that can interfere with a Go project.

Current Behavior

$ golangci-lint run
WARN The linter 'exportloopref' is deprecated (since v1.60.2) due to: Since Go1.22 (loopvar) this linter is no longer relevant. Replaced by copyloopvar.
WARN The linter 'tenv' is deprecated (since v1.64.0) due to: Duplicate feature another linter. Replaced by usetesting.
WARN The linter 'execinquery' is deprecated (since v1.58.0) due to: The repository of the linter has been archived by the owner.
ERRO [linters_context] execinquery: This linter is fully inactivated: it will not produce any reports.
ERRO [linters_context] exportloopref: This linter is fully inactivated: it will not produce any reports.
node_modules/aws-cdk/lib/init-templates/app/go/%name%.template.go:1: : invalid input file name "%name%.template.go" (typecheck)
package main
node_modules/aws-cdk/lib/init-templates/app/go/%name%.template.go:10:6: expected 'IDENT', found '%' (typecheck)
type %name.PascalCased%StackProps struct {
     ^
node_modules/aws-cdk/lib/init-templates/app/go/%name%.template.go:16:2: expected declaration, found 'if' (typecheck)
        if props != nil {
        ^
node_modules/aws-cdk/lib/init-templates/sample-app/go/%name%.template.go:1: : invalid input file name "%name%.template.go" (typecheck)
package main
node_modules/aws-cdk/lib/init-templates/sample-app/go/%name%.template.go:12:6: expected 'IDENT', found '%' (typecheck)
type %name.PascalCased%StackProps struct {
     ^
node_modules/aws-cdk/lib/init-templates/sample-app/go/%name%.template.go:18:2: expected declaration, found 'if' (typecheck)
        if props != nil {
        ^
node_modules/aws-cdk/lib/init-templates/sample-app/go/%name%_test.template.go:11:10: expected '(', found '%' (typecheck)
func Test%name.PascalCased%Stack(t *testing.T) {
         ^
node_modules/aws-cdk/test/commands/test-resources/stacks/s3.go:1: : # sdui/node_modules/aws-cdk/test/commands/test-resources/stacks
node_modules/aws-cdk/test/commands/test-resources/stacks/s3.go:35:27: undefined: WebsiteConfigurationProperty
node_modules/aws-cdk/test/commands/test-resources/stacks/s3.go:57:32: cannot use GoodGoStackProps{…} (value of type GoodGoStackProps) as *GoodGoStackProps value in argument to NewGoodGoStack (typecheck)
package main

Reproduction Steps

Create a new project using Go
Include a Golagci-lint config file
Install aws-cdk via npm
Execute $ golangci-lint run

Possible Solution

One solution would be to rename the directory from init-templates to _init-templates

Additional Information/Context

No response

CDK CLI Version

2.1000.2

Framework Version

No response

Node.js Version

v20.11.1

OS

MacOS Sequoia v15.3

Language

Go

Language Version

go version go1.22.3 darwin/arm64

Other information

No response

@4k1k0 4k1k0 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 22, 2025
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Feb 22, 2025
@pahud
Copy link
Contributor

pahud commented Feb 24, 2025

Looks like renaming init-templates to _init-templates might be a solution, which follows Go's convention where directories starting with _ are ignored by Go tools.

I'm bringing it up to the team for further investigations.

@pahud pahud added p2 effort/medium Medium work item – several days of effort jsii This issue originates in jsii, or this feature must be implemented in jsii. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 24, 2025
@cetteup
Copy link

cetteup commented May 20, 2025

@pahud Any feedback/progress on this?

Some additional context: This does not just affect linters or other 3rd party tools. If you have aws-cdk in your node_modules folder, running go mod tidy will pull aws-cdk-go and other packages used in the app templates into your project's dependencies (go.mod/go.sum). Unless you notice and revert that, you suddenly have some unrelated (and unused) dependencies in your go project.

Unfortunately, there does not seem to be any easy workaround. There's no option for e.g. go mod tidy to just ignore node_modules. Also, using something like .node_modules (leading dot) instead causes a lot of issues on the node side of things, such as breaking module/type resolution when using TypeScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort jsii This issue originates in jsii, or this feature must be implemented in jsii. p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests

3 participants