Skip to content

Language resolution in HookConfig.validate() #7439

Description

@wbreza

Language Resolution in HookConfig.validate()

Part of #7435 — Multi-Language Hook Support

User Story

As a template author, I want azd to automatically detect that my seed.py hook is a Python script so I don't have to specify language: python in every hook definition.

Solution Approach

Modify validate() in pkg/ext/models.go. Insert resolveLanguage() step after determining file vs inline, before shell inference:

  1. If explicit Language field set → use it, set resolvedLanguage.
  2. Else if explicit Shell field set → resolvedLanguage stays Unknown (shell hook).
  3. Else if file-based → call language.InferLanguageFromPath(path):
    • Recognized language → set resolvedLanguage.
    • Not recognized → fall through to shell inference (existing behavior).
  4. Else → inline script, fall through to shell inference.

Additional logic:

  • If resolvedLanguage != Unknown AND location == ScriptLocationInline → return error: "inline scripts are not supported for language hooks (language: %s). Use a file path instead."
  • If resolvedLanguage != Unknown → set validated = true, return nil (skip shell inference).
  • Update inferScriptTypeFromFilePath() error message to acknowledge language scripts.
  • Update ErrUnsupportedScriptType error variable for clarity.

Modify pkg/ext/models_test.go — table-driven tests:

  • .pyScriptLanguagePython
  • .jsScriptLanguageJavaScript
  • .tsScriptLanguageTypeScript
  • .csScriptLanguageDotNet
  • .shScriptLanguageUnknown (shell hook)
  • .ps1ScriptLanguageUnknown
  • Explicit language: python with .sh extension → Python (override wins)
  • Explicit shell: sh with .py extension → shell hook (shell wins)
  • Inline with language: python → error
  • No extension, no shell, no language → OS default shell

Files: pkg/ext/models.go (MODIFY), pkg/ext/models_test.go (MODIFY)

Acceptance Criteria

  • All existing hook tests continue to pass (backward compatible)
  • Language correctly inferred from .py, .js, .ts, .cs extensions
  • Explicit language: field takes precedence over extension
  • Explicit shell: field takes precedence over language inference
  • Inline scripts rejected for language hooks with clear error
  • Shell hooks (.sh, .ps1, inline) completely unaffected

References

Metadata

Metadata

Assignees

Labels

area/core-cliCLI commands, cmd/, internal/cmd/area/hooksLifecycle hooksfeatureFeature request

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions