Skip to content

Commit 0794593

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

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

internal/hcp/registry/hcl.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ 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 {
169+
170170
// If we encounter the same source twice, we'll defer
171171
// its addition to later, using both the build name
172172
// and the source type as the name used for HCP Packer.
@@ -193,23 +193,46 @@ func (h *HCLRegistry) registerAllComponents() hcl.Diagnostics {
193193
// If that happens, we then use a combination of both the build name, and
194194
// the source type.
195195
for _, build := range h.configuration.Builds {
196+
// this set is used to catch duplicate sources in the same block
197+
sources := map[string]struct{}{}
196198
for _, source := range build.Sources {
199+
if _, ok := sources[source.String()]; !ok {
200+
sources[source.String()] = struct{}{}
201+
} else {
202+
diags = append(diags, &hcl.Diagnostic{
203+
Severity: hcl.DiagError,
204+
Summary: "Source conflicts",
205+
Subject: &build.HCL2Ref.DefRange,
206+
Detail: fmt.Sprintf("Two or more sources are identical inside the same "+
207+
"build block, there should be only one instance of %s", source.String()),
208+
})
209+
continue
210+
}
197211
if _, ok := conflictSources[source.String()]; !ok {
198212
continue
199213
}
200214

201-
buildName := source.String()
202-
if build.Name != "" {
203-
buildName = fmt.Sprintf("%s.%s", build.Name, buildName)
215+
if build.Name == "" {
216+
diags = append(diags, &hcl.Diagnostic{
217+
Severity: hcl.DiagError,
218+
Summary: "Missing mandatory build name",
219+
Subject: &build.HCL2Ref.DefRange,
220+
Detail: "A build name is required when using the same source in two or" +
221+
" more different builds",
222+
})
223+
continue
204224
}
205225

226+
buildName := fmt.Sprintf("%s.%s", build.Name, source.String())
227+
206228
if _, ok := h.buildNames[buildName]; ok {
207229
diags = append(diags, &hcl.Diagnostic{
208230
Severity: hcl.DiagError,
209231
Summary: "Build name conflicts",
210232
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()),
233+
Detail: fmt.Sprintf("Two sources are used in the same build block or "+
234+
"two builds have the same name, causing a conflict, there must only "+
235+
"be one instance of %s", source.String()),
213236
})
214237
}
215238
h.buildNames[buildName] = struct{}{}

internal/hcp/registry/hcl_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestNewRegisterProperBuildName(t *testing.T) {
172172
},
173173
"multiple build block with same source create conflict": {
174174
expectErr: true,
175-
diagsSummaryContains: "conflict",
175+
diagsSummaryContains: "mandatory build name",
176176
builds: hcl2template.Builds{
177177
&hcl2template.BuildBlock{
178178
Sources: []hcl2template.SourceUseBlock{
@@ -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)