这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@walteh
Copy link
Owner

@walteh walteh commented Aug 5, 2025

Summary

  • add HCL loader using HashiCorp HCL to decode version and simple tasks
  • fall back to YAML parsing for extensionless files when HCL parsing fails
  • cover HCL parsing and discovery with new tests

Testing

  • go test ./...

Propt

Task 3: Implement Basic HCL Parsing Logic

Purpose: Develop the core logic to parse an HCL Taskfile and produce the internal Taskfile structure. In
this step, handle the basic elements (Taskfile version and simple tasks definitions) to establish the
parsing workflow. More complex substructures (commands, deps, vars) will be handled in the next task.
• Implementation (Parsing Approach):
• Use HashiCorp’s HCL library (hcl/v2) for parsing. For example, utilize hclparse.NewParser() to
read the file and produce an HCL AST (abstract syntax tree). Then traverse or decode this AST into Go
structures.
• Define HCL Structure: Decide how Taskfiles are represented in HCL syntax and map that to Go
structs. A recommended approach is to use HCL blocks for tasks. For instance:

version = "3"
task "build" {
desc = "Build the project"
cmds = ["go build ./..."]
}

Here, each task is an HCL block named “task” with the task name as a label. At the top level, version (and
other global keys like vars, env, etc.) are HCL attributes.

  •    Go Structs for HCL: Create structs to mirror this. For example, define a struct for the root of 

the HCL file:

type HCLTaskfile struct {
Version string hcl:"version"
Tasks []HCLTaskBlock hcl:"task,block"
// (Later, we'll add vars, env, includes, etc.)
}
type HCLTaskBlock struct {
Name string hcl:"name,label"
Desc *string hcl:"desc,attr"
// Other simple task fields (we will expand later)
Cmds []string hcl:"cmds,attr"
}

The HCLTaskBlock struct represents the content of each task "" { ... } block, capturing the task
name via a label. Initially, include a few basic fields like description and cmds as attributes.

  •    Decode HCL into Structs: After parsing the file with parser.ParseHCLFile, use gohcl.DecodeBody to

decode the HCL body into your HCLTaskfile struct. Check for errors/diagnostics from the HCL library and
handle them (return a user-friendly error if the file is invalid).
• Convert to Internal Taskfile: Map the decoded HCLTaskfile into the existing Taskfile structure.
For example, set Taskfile.Version and create entries in Taskfile.Tasks map for each HCLTaskBlock (using
the block’s Name as the map key, and populating a new Task struct with the parsed fields). At this stage,
you may populate only the fields we decoded (e.g. Desc, and a preliminary handling of cmds – perhaps just
store them as Shell commands in Task.Cmds).
• Leave placeholders or TODO comments for fields not yet handled (vars, env, deps, etc.), which
will be done in the next task.

  •    Validation:
  •    Create a simple HCL Taskfile example for testing: for instance, a file with a version and one 

task with a description and one command. Write a unit test that uses the HCL loader to parse this file and
returns a Taskfile struct. Verify that the Version matches and the Tasks map contains the task with
correct name, description, and command.
• Test error handling: feed invalid HCL content (e.g. malformed syntax) and ensure the HCL parser
returns an error that our code surfaces properly (e.g. error message contains the file name and position
of the parse error).
• Confirm that YAML functionality is still unaffected: run the YAML parsing tests again to ensure
no regression.


https://chatgpt.com/codex/tasks/task_e_6892047fd3d88330bd246acda2d2c536

@walteh walteh merged commit de84e84 into codex/hcl Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants