Skip to content
Open
Show file tree
Hide file tree
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
69 changes: 66 additions & 3 deletions internal/app/azldev/cmds/component/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ package component

import (
"fmt"
"path/filepath"

"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/sources"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/specs"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/workdir"
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
"github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -58,6 +63,15 @@ type componentDetails struct {
specs.ComponentSpecDetails
}

type queryComponent struct {
components.Component
config projectconfig.ComponentConfig
}

func (c *queryComponent) GetConfig() *projectconfig.ComponentConfig {
return &c.config
}

// Queries env for component details, in accordance with options. Returns the found components.
func QueryComponents(
env *azldev.Env, options *QueryComponentsOptions,
Expand All @@ -74,9 +88,7 @@ func QueryComponents(
allDetails := make([]*componentDetails, 0, comps.Len())

for _, comp := range comps.Components() {
spec := comp.GetSpec()

specInfo, err := spec.Parse()
specInfo, err := parseComponentSpec(env, comp)
if err != nil {
return nil, fmt.Errorf("failed to parse spec for component %q:\n%w", comp.GetName(), err)
}
Expand All @@ -90,3 +102,54 @@ func QueryComponents(

return allDetails, nil
}

func parseComponentSpec(env *azldev.Env, comp components.Component) (*specs.ComponentSpecDetails, error) {
if comp.GetConfig().Spec.SourceType == projectconfig.SpecSourceTypeLocal {
return comp.GetSpec().Parse()
}

componentForPrep := comp
if comp.GetConfig().Spec.SourceType == projectconfig.SpecSourceTypeUnspecified {
normalizedConfig := *comp.GetConfig()
normalizedConfig.Spec.SourceType = projectconfig.SpecSourceTypeUpstream
componentForPrep = &queryComponent{
Component: comp,
config: normalizedConfig,
}
}

distro, err := sourceproviders.ResolveDistro(env, componentForPrep)
if err != nil {
return nil, fmt.Errorf("failed to resolve distro for component %q:\n%w", comp.GetName(), err)
}

sourceManager, err := sourceproviders.NewSourceManager(env, distro)
if err != nil {
return nil, fmt.Errorf("failed to create source manager:\n%w", err)
}

preparer, err := sources.NewPreparer(sourceManager, env.FS(), env, env, sources.WithSkipLookaside())
if err != nil {
return nil, fmt.Errorf("failed to create source preparer:\n%w", err)
}

workDirFactory, err := workdir.NewFactory(env.FS(), env.WorkDir(), env.ConstructionTime())
if err != nil {
return nil, fmt.Errorf("failed to create work dir factory:\n%w", err)
}

preparedSourcesDir, err := workDirFactory.Create(comp.GetName(), "query-spec")
if err != nil {
return nil, fmt.Errorf("failed to create work dir for component %#q:\n%w", comp.GetName(), err)
}

if err := preparer.PrepareSources(env, componentForPrep, preparedSourcesDir, true /* applyOverlays */); err != nil {
return nil, fmt.Errorf("failed to prepare sources for component %#q:\n%w", comp.GetName(), err)
Comment on lines +131 to +147
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

parseComponentSpec prepares upstream sources without enabling dist-git / synthetic history (sources.WithGitRepo). In other paths (e.g., component render) WithGitRepo is used so rpmautospec can expand %autorelease / %autochangelog correctly and to keep release numbering consistent with overlay commit count. As written, component query may produce incorrect release/changelog values (or fail) for specs that rely on rpmautospec because the upstream .git dir is removed and no synthetic commits are created. Consider aligning this with render by adding sources.WithGitRepo(env.Config().Project.DefaultAuthorEmail) (or otherwise preserving .git + synthetic history) for this query-only prep path, or explicitly disabling rpmautospec-dependent expansion and documenting the limitation.

Copilot uses AI. Check for mistakes.
}

preparedConfig := *componentForPrep.GetConfig()
preparedConfig.Spec.SourceType = projectconfig.SpecSourceTypeLocal
preparedConfig.Spec.Path = filepath.Join(preparedSourcesDir, comp.GetName()+".spec")

return specs.NewSpec(env, preparedConfig).Parse()
}
52 changes: 52 additions & 0 deletions internal/app/azldev/cmds/component/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,55 @@ func TestQueryComponents_OneComponent(t *testing.T) {
result := results[0]
assert.Equal(t, testComponentName, result.Name)
}

func TestQueryComponents_UpstreamComponent(t *testing.T) {
const testComponentName = "test-component"

testEnv := testutils.NewTestEnv(t)
testEnv.Config.Components[testComponentName] = projectconfig.ComponentConfig{
Name: testComponentName,
}

testEnv.CmdFactory.RegisterCommandInSearchPath(mock.MockBinary)

testEnv.CmdFactory.RunHandler = func(cmd *exec.Cmd) error {
if len(cmd.Args) >= 2 && cmd.Args[0] == "git" && cmd.Args[1] == "clone" {
cloneDir := cmd.Args[len(cmd.Args)-1]
specPath := cloneDir + "/" + testComponentName + ".spec"

return fileutils.WriteFile(
testEnv.FS(),
specPath,
[]byte("Name: "+testComponentName+"\nVersion: 1.0.0\n"),
fileperms.PublicFile,
)
}

return nil
}

testEnv.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) {
if len(cmd.Args) >= 5 &&
cmd.Args[0] == "git" &&
cmd.Args[1] == "-C" &&
cmd.Args[3] == "rev-parse" &&
cmd.Args[4] == "HEAD" {
return "head123abc\n", nil
}

return "name=test-component\nepoch=0\nversion=1.0.0\nrelease=1.azl3\n", nil
}

options := component.QueryComponentsOptions{
ComponentFilter: components.ComponentFilter{
ComponentNamePatterns: []string{testComponentName},
},
}

results, err := component.QueryComponents(testEnv.Env, &options)
require.NoError(t, err)
require.Len(t, results, 1)

result := results[0]
assert.Equal(t, testComponentName, result.Name)
Comment thread
holly-agyei marked this conversation as resolved.
Outdated
}
Loading