Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing module from any filesystem #49

Merged
merged 2 commits into from
Aug 6, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions tfconfig/load_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ func loadModule(fs FS, dir string) (*Module, Diagnostics) {
for _, filename := range primaryPaths {
var file *hcl.File
var fileDiags hcl.Diagnostics

b, err := fs.ReadFile(filename)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Failed to read file",
Detail: fmt.Sprintf("The configuration file %q could not be read.", filename),
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error matches the one from ParseHCLFile: https://github.com/hashicorp/hcl/blob/350d663f3c09e5a6d72ca11cb3250c1ce395bc8f/hclparse/parser.go#L70-L79

There is a small drift from ParseJSONFile in that json.ParseFile first opens the file and reports early diagnostic from there. This difference seems insignificant to me, but I'm open to retrofitting the extra Open call with that extra dedicated diagnostic here if deemed necessary.

continue
}
if strings.HasSuffix(filename, ".json") {
file, fileDiags = parser.ParseJSONFile(filename)
file, fileDiags = parser.ParseJSON(b, filename)
} else {
file, fileDiags = parser.ParseHCLFile(filename)
file, fileDiags = parser.ParseHCL(b, filename)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These original Parse*File methods also provided "cache" of AST for each file which this change effectively removes.

It's just filename-based cache and so in order to get a hit we'd need two duplicate files in a directory, which is unlikely to ever happen anyway, so this change should in that sense be harmless and shouldn't affect performance in any way.

}
diags = append(diags, fileDiags...)
if file == nil {
Expand Down