@@ -31,24 +31,22 @@ import (
31
31
// introducing a circular dependency
32
32
const maxFileSizeBytes = 10 * (10 << 20 ) // 10 MB
33
33
34
- // hclparse.Parser by default keeps track of all files it has ever parsed
35
- // by filename. By re-using the same parser instance we can avoid repeating
36
- // previous work.
37
- // NOTE: we can only do this if we _just_ use ParseHCLFile().
38
- // The ParseHCL() method allows specifying the filename directly, which allows
39
- // easily getting previously cached results. Whenever we parse HCL files from disk
40
- // we do so in a unique file path.
41
- // See: provisionersdk/session.go#L51
42
- var hclFileParser ParseHCLFiler = hclparse .NewParser ()
43
-
44
- type ParseHCLFiler interface {
34
+ // ParseHCLFile() is only method of hclparse.Parser we use in this package.
35
+ // hclparse.Parser will by default cache all previous files it has ever
36
+ // parsed. While leveraging this caching behavior is nice, we _never_ want
37
+ // to end up in a situation where we end up returning stale values.
38
+ type Parser interface {
45
39
ParseHCLFile (filename string ) (* hcl.File , hcl.Diagnostics )
46
40
}
47
41
48
42
// WorkspaceTags extracts tags from coder_workspace_tags data sources defined in module.
49
43
// Note that this only returns the lexical values of the data source, and does not
50
44
// evaluate variables and such. To do this, see evalProvisionerTags below.
51
- func WorkspaceTags (module * tfconfig.Module ) (map [string ]string , error ) {
45
+ // If the provided Parser is nil, a new instance of hclparse.Parser will be used instead.
46
+ func WorkspaceTags (parser Parser , module * tfconfig.Module ) (map [string ]string , error ) {
47
+ if parser == nil {
48
+ parser = hclparse .NewParser ()
49
+ }
52
50
tags := map [string ]string {}
53
51
54
52
for _ , dataResource := range module .DataResources {
@@ -63,7 +61,7 @@ func WorkspaceTags(module *tfconfig.Module) (map[string]string, error) {
63
61
continue
64
62
}
65
63
// We know in which HCL file is the data resource defined.
66
- file , diags = hclFileParser .ParseHCLFile (dataResource .Pos .Filename )
64
+ file , diags = parser .ParseHCLFile (dataResource .Pos .Filename )
67
65
if diags .HasErrors () {
68
66
return nil , xerrors .Errorf ("can't parse the resource file: %s" , diags .Error ())
69
67
}
@@ -143,9 +141,11 @@ func WorkspaceTagDefaultsFromFile(ctx context.Context, logger slog.Logger, file
143
141
}
144
142
}()
145
143
144
+ parser := hclparse .NewParser ()
145
+
146
146
// This only gets us the expressions. We need to evaluate them.
147
147
// Example: var.region -> "us"
148
- tags , err = WorkspaceTags (module )
148
+ tags , err = WorkspaceTags (parser , module )
149
149
if err != nil {
150
150
return nil , xerrors .Errorf ("extract workspace tags: %w" , err )
151
151
}
@@ -156,7 +156,7 @@ func WorkspaceTagDefaultsFromFile(ctx context.Context, logger slog.Logger, file
156
156
if err != nil {
157
157
return nil , xerrors .Errorf ("load variable defaults: %w" , err )
158
158
}
159
- paramsDefaults , err := loadParamsDefaults (maps .Values (module .DataResources ))
159
+ paramsDefaults , err := loadParamsDefaults (parser , maps .Values (module .DataResources ))
160
160
if err != nil {
161
161
return nil , xerrors .Errorf ("load parameter defaults: %w" , err )
162
162
}
@@ -242,7 +242,7 @@ func loadVarsDefaults(variables []*tfconfig.Variable) (map[string]string, error)
242
242
}
243
243
244
244
// loadParamsDefaults returns the default values of all coder_parameter data sources data sources provided.
245
- func loadParamsDefaults (dataSources []* tfconfig.Resource ) (map [string ]string , error ) {
245
+ func loadParamsDefaults (parser Parser , dataSources []* tfconfig.Resource ) (map [string ]string , error ) {
246
246
defaultsM := make (map [string ]string )
247
247
for _ , dataResource := range dataSources {
248
248
if dataResource == nil {
@@ -261,7 +261,7 @@ func loadParamsDefaults(dataSources []*tfconfig.Resource) (map[string]string, er
261
261
}
262
262
263
263
// We know in which HCL file is the data resource defined.
264
- file , diags = hclFileParser .ParseHCLFile (dataResource .Pos .Filename )
264
+ file , diags = parser .ParseHCLFile (dataResource .Pos .Filename )
265
265
if diags .HasErrors () {
266
266
return nil , xerrors .Errorf ("can't parse the resource file %q: %s" , dataResource .Pos .Filename , diags .Error ())
267
267
}
0 commit comments