aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.61k stars 3.9k forks source link

(aws-cdk): go template breaks go list #13971

Open mguterl opened 3 years ago

mguterl commented 3 years ago

13840 added an app template for go and the filenames break go list ./... when cdk project is embedded inside of a Go project.

Reproduction Steps

mkdir project
cd project
cdk init app --language=typescript
go mod init github.com/test/project
echo "package main\nfunc main() { return }\n" > main.go
go list ./... # fails because of template files

package github.com/test/project/node_modules/aws-cdk/lib/init-templates/v1/app/go: invalid input file name "%name%.template.go"

What did you expect to happen?

I expected go list ./... to return a list of package names.

What actually happened?

List of package name was not returned and an error was returned instead.

package github.com/test/project/node_modules/aws-cdk/lib/init-templates/v1/app/go: invalid input file name "%name%.template.go"

Environment

Other

I think it might be possible to add an empty go.mod file into the template directory to prevent this from happening, but I'm hardly an expert in Go.


This is :bug: Bug Report

MrArnoldPalmer commented 3 years ago

we will investigate if there is an easy fix from our side. I don't know if generally this is a use case that we want to think about compatibility with. The user can put their cdk app anywhere, and node_modules can have a lot of stuff in it. Thinking about what unrelated commands that users can invoke that may conflict with any of the files within node_modules feels hard. In this case though if adding an empty go.mod or otherwise solves this seems easy enough.

tgrobinson commented 3 years ago

The root cause is the filename doesn't conform to the Golang filename standards by being prefixed with a %. It looks like the standards allow for . and _ prefixed filenames which may be better suited.

As a side note, I've also hit this exact error while using the go vet and go test commands on my Golang project.

davidsteed commented 3 years ago

This is causing us problems too with ci cd tooling and annoying errors in VSCode... If the files were .go_tempate or something that these files would not be recognised this would not be a problem. Should be an easy fix.

mlafeldt commented 3 years ago

I agree that an easy fix might be to rename the template from %name%.template.go to %name%.go.template.

Update: Actually, it's not that easy since the templating is generic and used for all languages.

mlafeldt commented 3 years ago

Here's my dirty quickfix for now:

--- package.json
+++ package.json
@@ -8,7 +8,8 @@
     "build": "tsc",
     "watch": "tsc -w",
     "test": "jest",
-    "cdk": "cdk"
+    "cdk": "cdk",
+    "postinstall": "rm -rf node_modules/aws-cdk/lib/init-templates/v1/app/go"
   },

(We don't need those templates in already initialized projects anyway.)

mlafeldt commented 3 years ago

Since this is still a problem and there's now a v2 template for Go, I had to update my workaround a bit:

"postinstall": "rm -rf node_modules/aws-cdk/lib/init-templates/*/app/go"
tenjaa commented 1 year ago

Using postinstall cannot be the solution. Running in our pipeline we need to run npm ci which does not trigger that hook. It also looks like that postci does not exist.

daschaa commented 1 year ago

@tenjaa Which npm version do you use? postinstall should trigger on npm ci too. There was an issue that in a minor version of npm v7 it was not triggered correctly but it was fixed afaik - See here: https://github.com/npm/cli/pull/2316. Maybe you have to upgrade your npm version in your pipeline?