@@ -15,8 +15,8 @@ import (
1515 "github.com/invopop/jsonschema"
1616
1717 "github.com/anchore/syft/internal"
18+ "github.com/anchore/syft/internal/packagemetadata"
1819 syftJsonModel "github.com/anchore/syft/syft/format/syftjson/model"
19- "github.com/anchore/syft/syft/internal/packagemetadata"
2020)
2121
2222/*
@@ -26,6 +26,17 @@ are not captured (empty interfaces). This means that pkg.Package.Metadata is not
2626can be extended to include specific package metadata struct shapes in the future.
2727*/
2828
29+ var repoRoot string
30+
31+ func init () {
32+ var err error
33+ repoRoot , err = packagemetadata .RepoRoot ()
34+ if err != nil {
35+ fmt .Println ("unable to determine repo root" )
36+ os .Exit (1 )
37+ }
38+ }
39+
2940func main () {
3041 write (encode (build ()))
3142}
@@ -60,7 +71,7 @@ func assembleTypeContainer(items []any) (any, map[string]string) {
6071 }
6172
6273 if len (typesMissingNames ) > 0 {
63- fmt .Println ("the following types are missing JSON names (manually curated in ./syft/ internal/packagemetadata/names.go):" )
74+ fmt .Println ("the following types are missing JSON names (manually curated in ./internal/packagemetadata/names.go):" )
6475 for _ , t := range typesMissingNames {
6576 fmt .Println (" - " , t .Name ())
6677 }
@@ -86,25 +97,30 @@ func build() *jsonschema.Schema {
8697 // note: AddGoComments parses from the module root and creates keys like "syft/pkg.TypeName",
8798 // but the reflector expects fully qualified paths like "github.com/anchore/syft/syft/pkg.TypeName".
8899 // We fix up the keys after extraction to match the expected format.
89- if err := reflector .AddGoComments ("github.com/anchore/syft" , "../../.." ); err != nil {
100+ if err := reflector .AddGoComments ("github.com/anchore/syft" , repoRoot ); err != nil {
90101 fmt .Fprintf (os .Stderr , "warning: failed to extract Go comments: %v\n " , err )
91102 } else {
92103 // fix up comment map keys to use fully qualified import paths
104+ // note: AddGoComments includes the absolute repo path WITHOUT the leading slash
105+ repoRootNoSlash := strings .TrimPrefix (repoRoot , "/" )
93106 fixedMap := make (map [string ]string )
94107 for k , v := range reflector .CommentMap {
95108 newKey := k
96109 if ! strings .HasPrefix (k , "github.com/" ) {
110+ // key doesn't have module prefix, add it
97111 newKey = "github.com/anchore/syft/" + k
112+ } else if strings .Contains (k , repoRootNoSlash ) {
113+ // key has the absolute repo path embedded, strip it
114+ // format: github.com/anchore/syft/Users/wagoodman/code/syft-manual/syft/pkg.Type
115+ // should be: github.com/anchore/syft/syft/pkg.Type
116+ newKey = strings .Replace (k , repoRootNoSlash + "/" , "" , 1 )
98117 }
99118 fixedMap [newKey ] = v
100119 }
101120 reflector .CommentMap = fixedMap
102121
103122 // copy field comments for type aliases (e.g., type RpmArchive RpmDBEntry)
104- repoRoot , err := packagemetadata .RepoRoot ()
105- if err == nil {
106- copyAliasFieldComments (reflector .CommentMap , repoRoot )
107- }
123+ copyAliasFieldComments (reflector .CommentMap , repoRoot )
108124 }
109125
110126 pkgMetadataContainer , pkgMetadataMapping := assembleTypeContainer (packagemetadata .AllTypes ())
@@ -178,11 +194,6 @@ func encode(schema *jsonschema.Schema) []byte {
178194}
179195
180196func write (schema []byte ) {
181- repoRoot , err := packagemetadata .RepoRoot ()
182- if err != nil {
183- fmt .Println ("unable to determine repo root" )
184- os .Exit (1 )
185- }
186197 schemaPath := filepath .Join (repoRoot , "schema" , "json" , fmt .Sprintf ("schema-%s.json" , internal .JSONSchemaVersion ))
187198 latestSchemaPath := filepath .Join (repoRoot , "schema" , "json" , "schema-latest.json" )
188199
0 commit comments