@@ -39,7 +39,7 @@ func (e *Golang) Namespaces() []string {
3939// Unpack the module into the proxy directory.
4040// We also inject the GOPROXY environment variable into runtime.json to force offline use.
4141// We also inject GOMODCACHE to avoid polluting the default user cache.
42- func (e * Golang ) Add (artifact * buildplan.Artifact , artifactSrcPath string ) ([]string , error ) {
42+ func (e * Golang ) Add (artifact * buildplan.Artifact , artifactSrcPath string ) (_ []string , rerr error ) {
4343 installedFiles := []string {}
4444
4545 files , err := fileutils .ListDir (artifactSrcPath , false )
@@ -96,13 +96,35 @@ func (e *Golang) Add(artifact *buildplan.Artifact, artifactSrcPath string) ([]st
9696 if err != nil {
9797 return nil , errs .Wrap (err , "Unable to unpack downloaded module" )
9898 }
99- err = fileutils .CopyFile (filepath .Join (unpackDir , artifact .NameAndVersion (), "go.mod" ), filepath .Join (vDir , artifact .Version ()+ ".mod" ))
99+ defer func () {
100+ err := os .RemoveAll (unpackDir )
101+ if err != nil {
102+ rerr = errs .Pack (rerr , errs .Wrap (err , "Unable to remove unpacked module in %s" , unpackDir ))
103+ }
104+ }()
105+ zipFiles , err := fileutils .ListDirSimple (unpackDir , false )
100106 if err != nil {
101- return nil , errs .Wrap (err , "Unable to copy go.mod from unpacked module" )
107+ return nil , errs .Wrap (err , "Unable to list unpacked module" )
102108 }
103- err = os .RemoveAll (unpackDir )
104- if err != nil {
105- return nil , errs .Wrap (err , "Unable to remove unpacked module" )
109+ goModFound := false
110+ for _ , file := range zipFiles {
111+ if filepath .Base (file ) != "go.mod" {
112+ continue
113+ }
114+ err = fileutils .CopyFile (file , filepath .Join (vDir , artifact .Version ()+ ".mod" ))
115+ if err != nil {
116+ return nil , errs .Wrap (err , "Unable to copy go.mod from unpacked module" )
117+ }
118+ goModFound = true
119+ break
120+ }
121+
122+ // If the archive did not have a go.mod to copy, create a versioned one in the @v directory.
123+ if ! goModFound {
124+ err = fileutils .WriteFile (filepath .Join (vDir , artifact .Version ()+ ".mod" ), []byte ("module " + artifact .Name ()))
125+ if err != nil {
126+ return nil , errs .Wrap (err , "Unable to write go.mod file" )
127+ }
106128 }
107129
108130 installedFiles = append (installedFiles , relativeProxied )
0 commit comments