Go项目中包含Typescript AWS-CDK目录,go.mod文件混乱。

huangapple go评论90阅读模式
英文:

Go project containing Typescript AWS-CDK directory has cluttered go.mod file

问题

我有一个包含Typescript AWS-CDK目录的Go项目,用于定义包含Go二进制文件的堆栈。

这个示例的最简项目是:

mkdir example
cd example
mkdir infrastructure
cd infrastructure
cdk init app --language typescript
cd ..
go mod init example
go mod tidy

到这一步,go.mod中充满了aws-cdk-go的引用(尽管我使用的是Typescript)。理想情况下,此时go.mod不应该有任何依赖项。看起来,在node_modules/aws-cdk/....路径中有几个Go文件可能会影响go mod tidy

我还不打算使用Go CDK库。除了将所有的Go代码放入一个子文件夹中,并只在该子文件夹中运行go mod init之外,是否有办法阻止go mod tidy引用那些被隐藏的文件?

英文:

I have a Go project containing a Typescript AWS-CDK directory to define the Stack that contains the Go binary.

The simplest project for this example is:

mkdir example
cd example
mkdir infrastructure
cd infrastructure
cdk init app --language typescript
cd ..
go mod init example
go mod tidy

At this point, go.mod is full of aws-cdk-go references (despite the fact I'm using Typescript). Ideally, go.mod shouldn't have any dependencies at this point. It appears that there are several Go files buried in the node_modules/aws-cdk/.... path that may be influencing go mod tidy.

I'm not yet interested in using the Go CDK libraries. Besides throwing all of my Go code into a subfolder and only running go mod init in that subfolder, is there a way to prevent go mod tidy from referencing those buried files?

答案1

得分: 1

infrastructure目录中,使用touch go.mod命令创建一个空文件,如果我在根目录中运行go mod tidy命令,go.mod文件将只包含我项目实际使用的模块。

英文:

https://github.com/golang/go/wiki/Modules#can-an-additional-gomod-exclude-unnecessary-content-do-modules-have-the-equivalent-of-a-gitignore-file

> An empty go.mod in a directory will cause that directory and all of
> its subdirectories to be excluded from the top-level Go module.

In the infrastructure directory, touch go.mod creates an empty file and if I go mod tidy in the root folder, the go.mod file only contains the modules my project actually uses.

答案2

得分: 0

go mod init 查找配置文件,如果找到,则安装模块。

通过在子目录中运行 go mod init [Edit] <s>before cdk init app</s>,可以避免这种“有帮助”的行为:

# ...
npx cdk init app --language typescript
cd ..
mkdir src
cd src
go mod init example
go mod tidy
cd ..

这将创建一个预期的空的 go.mod 文件。

英文:

go mod init looks for configuration files and, if found, installs the modules.

Avoid this "helpful" behaviour by running go mod init [Edit] <s>before cdk init app</s> in a subdirectory:

# ...
npx cdk init app --language typescript
cd ..
mkdir src
cd src
go mod init example
go mod tidy
cd ..

This creates an empty go.mod, as expected.

huangapple
  • 本文由 发表于 2022年5月4日 22:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/72115180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定