@@ -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 {}{}
0 commit comments