Skip to content

Commit 17f7b43

Browse files
committed
hcl: fix issue with partially named builds
1 parent 5e2809a commit 17f7b43

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

internal/hcp/registry/hcl.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ func (h *HCLRegistry) registerAllComponents() hcl.Diagnostics {
164164

165165
conflictSources := map[string]struct{}{}
166166

167-
// we currently support only one build block but it will change in the near future
168167
for _, build := range h.configuration.Builds {
169168
for _, source := range build.Sources {
170169
// If we encounter the same source twice, we'll defer
@@ -198,18 +197,26 @@ func (h *HCLRegistry) registerAllComponents() hcl.Diagnostics {
198197
continue
199198
}
200199

201-
buildName := source.String()
202-
if build.Name != "" {
203-
buildName = fmt.Sprintf("%s.%s", build.Name, buildName)
200+
if build.Name == "" {
201+
diags = append(diags, &hcl.Diagnostic{
202+
Severity: hcl.DiagError,
203+
Summary: "Build name conflicts",
204+
Subject: &build.HCL2Ref.DefRange,
205+
Detail: "build name is required when using the same source in two different builds",
206+
})
207+
continue
204208
}
205209

210+
buildName := fmt.Sprintf("%s.%s", build.Name, source.String())
211+
206212
if _, ok := h.buildNames[buildName]; ok {
207213
diags = append(diags, &hcl.Diagnostic{
208214
Severity: hcl.DiagError,
209215
Summary: "Build name conflicts",
210216
Subject: &build.HCL2Ref.DefRange,
211-
Detail: fmt.Sprintf("Two sources are used in the same build block, causing "+
212-
"a conflict, there must only be one instance of %s", source.String()),
217+
Detail: fmt.Sprintf("Two sources are used in the same build block or "+
218+
"two build has the same name causing a conflict, there must only "+
219+
"be one instance of %s", source.String()),
213220
})
214221
}
215222
h.buildNames[buildName] = struct{}{}

internal/hcp/registry/hcl_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ func TestNewRegisterProperBuildName(t *testing.T) {
237237
},
238238
},
239239
"multiple build block with same source but with only one declared build name": {
240-
expectErr: false,
241-
expectedBuilds: []string{"docker.ubuntu", "build.docker.ubuntu"},
240+
expectErr: true,
241+
// expectedBuilds: []string{"docker.ubuntu", "build.docker.ubuntu"},
242242
builds: hcl2template.Builds{
243243
&hcl2template.BuildBlock{
244244
Name: "build",

0 commit comments

Comments
 (0)