Skip to content

Commit 721a0b8

Browse files
committed
hcl: fix issue with partially named builds
1 parent 746f394 commit 721a0b8

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

internal/hcp/registry/hcl.go

+14-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,27 @@ 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: "Missing mandatory build name",
204+
Subject: &build.HCL2Ref.DefRange,
205+
Detail: "A build name is required when using the same source in two or" +
206+
" more different builds",
207+
})
208+
continue
204209
}
205210

211+
buildName := fmt.Sprintf("%s.%s", build.Name, source.String())
212+
206213
if _, ok := h.buildNames[buildName]; ok {
207214
diags = append(diags, &hcl.Diagnostic{
208215
Severity: hcl.DiagError,
209216
Summary: "Build name conflicts",
210217
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()),
218+
Detail: fmt.Sprintf("Two sources are used in the same build block or "+
219+
"two builds have the same name, causing a conflict, there must only "+
220+
"be one instance of %s", source.String()),
213221
})
214222
}
215223
h.buildNames[buildName] = struct{}{}

internal/hcp/registry/hcl_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ 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,
242241
builds: hcl2template.Builds{
243242
&hcl2template.BuildBlock{
244243
Name: "build",

0 commit comments

Comments
 (0)