@@ -36,7 +36,7 @@ const (
3636)
3737
3838func BuildDependencyTree (params technologies.BuildInfoBomGeneratorParams , technology techutils.Technology ) (dependencyTree []* clientutils.GraphNode , uniqueDeps []string , downloadUrls map [string ]string , err error ) {
39- dependenciesGraph , directDependenciesList , pipUrls , errGetTree := getDependencies (params , technology )
39+ rootDetected , dependenciesGraph , directDependenciesList , pipUrls , errGetTree := getDependencies (params , technology )
4040 if errGetTree != nil {
4141 err = errGetTree
4242 return
@@ -52,16 +52,40 @@ func BuildDependencyTree(params technologies.BuildInfoBomGeneratorParams, techno
5252 populatePythonDependencyTree (directDependency , dependenciesGraph , uniqueDepsSet )
5353 directDependencies = append (directDependencies , directDependency )
5454 }
55- root := & clientutils.GraphNode {
56- Id : "root" ,
57- Nodes : directDependencies ,
58- }
59- dependencyTree = []* clientutils.GraphNode {root }
55+ dependencyTree = getRootNodes (directDependencies , rootDetected )
6056 uniqueDeps = uniqueDepsSet .ToSlice ()
6157 return
6258}
6359
64- func getDependencies (params technologies.BuildInfoBomGeneratorParams , technology techutils.Technology ) (dependenciesGraph map [string ][]string , directDependencies []string , pipUrls map [string ]string , err error ) {
60+ func getRootNodes (directDependencies []* clientutils.GraphNode , rootDetected bool ) (roots []* clientutils.GraphNode ) {
61+ if ! rootDetected {
62+ return []* clientutils.GraphNode {{
63+ Id : "root" ,
64+ Nodes : directDependencies ,
65+ }}
66+ }
67+ // root was detected. in Pip, the pip version is also detected as root component.
68+ // In this case, we need to append the pip node to the actual roots.
69+ roots = []* clientutils.GraphNode {}
70+ var pipNode * clientutils.GraphNode
71+ // Search if pip is one of the direct dependencies.
72+ for _ , dep := range directDependencies {
73+ if strings .HasPrefix (dep .Id , PythonPackageTypeIdentifier + techutils .Pip .String ()+ ":" ) {
74+ pipNode = dep
75+ } else {
76+ roots = append (roots , dep )
77+ }
78+ }
79+ if pipNode != nil {
80+ // Append pip node to actual roots.
81+ for _ , root := range roots {
82+ root .Nodes = append (root .Nodes , pipNode )
83+ }
84+ }
85+ return
86+ }
87+
88+ func getDependencies (params technologies.BuildInfoBomGeneratorParams , technology techutils.Technology ) (rootDetected bool , dependenciesGraph map [string ][]string , directDependencies []string , pipUrls map [string ]string , err error ) {
6589 wd , err := os .Getwd ()
6690 if errorutils .CheckError (err ) != nil {
6791 return
@@ -95,7 +119,7 @@ func getDependencies(params technologies.BuildInfoBomGeneratorParams, technology
95119 pythonTool := pythonutils .PythonTool (technology )
96120 if technology == techutils .Pipenv || ! params .SkipAutoInstall {
97121 var restoreEnv func () error
98- restoreEnv , err = runPythonInstall (params , pythonTool )
122+ rootDetected , restoreEnv , err = runPythonInstall (params , pythonTool )
99123 defer func () {
100124 err = errors .Join (err , restoreEnv ())
101125 }()
@@ -180,7 +204,7 @@ type pypiMetaData struct {
180204 Version string `json:"version"`
181205}
182206
183- func runPythonInstall (params technologies.BuildInfoBomGeneratorParams , tool pythonutils.PythonTool ) (restoreEnv func () error , err error ) {
207+ func runPythonInstall (params technologies.BuildInfoBomGeneratorParams , tool pythonutils.PythonTool ) (rootDetected bool , restoreEnv func () error , err error ) {
184208 switch tool {
185209 case pythonutils .Pip :
186210 return installPipDeps (params )
@@ -192,28 +216,28 @@ func runPythonInstall(params technologies.BuildInfoBomGeneratorParams, tool pyth
192216 return
193217}
194218
195- func installPoetryDeps (params technologies.BuildInfoBomGeneratorParams ) (restoreEnv func () error , err error ) {
219+ func installPoetryDeps (params technologies.BuildInfoBomGeneratorParams ) (rootDetected bool , restoreEnv func () error , err error ) {
196220 restoreEnv = func () error {
197221 return nil
198222 }
199223 if params .DependenciesRepository != "" {
200224 rtUrl , username , password , err := artifactoryutils .GetPypiRepoUrlWithCredentials (params .ServerDetails , params .DependenciesRepository , false )
201225 if err != nil {
202- return restoreEnv , err
226+ return false , restoreEnv , err
203227 }
204228 if password != "" {
205229 err = artifactoryutils .ConfigPoetryRepo (rtUrl .Scheme + "://" + rtUrl .Host + rtUrl .Path , username , password , params .DependenciesRepository )
206230 if err != nil {
207- return restoreEnv , err
231+ return false , restoreEnv , err
208232 }
209233 }
210234 }
211235 // Run 'poetry install'
212236 _ , err = executeCommand ("poetry" , "install" )
213- return restoreEnv , err
237+ return false , restoreEnv , err
214238}
215239
216- func installPipenvDeps (params technologies.BuildInfoBomGeneratorParams ) (restoreEnv func () error , err error ) {
240+ func installPipenvDeps (params technologies.BuildInfoBomGeneratorParams ) (rootDetected bool , restoreEnv func () error , err error ) {
217241 // Set virtualenv path to venv dir
218242 err = os .Setenv ("WORKON_HOME" , ".jfrog" )
219243 if err != nil {
@@ -223,14 +247,14 @@ func installPipenvDeps(params technologies.BuildInfoBomGeneratorParams) (restore
223247 return os .Unsetenv ("WORKON_HOME" )
224248 }
225249 if params .DependenciesRepository != "" {
226- return restoreEnv , runPipenvInstallFromRemoteRegistry (params .ServerDetails , params .DependenciesRepository )
250+ return false , restoreEnv , runPipenvInstallFromRemoteRegistry (params .ServerDetails , params .DependenciesRepository )
227251 }
228252 // Run 'pipenv install -d'
229253 _ , err = executeCommand ("pipenv" , "install" , "-d" )
230- return restoreEnv , err
254+ return false , restoreEnv , err
231255}
232256
233- func installPipDeps (params technologies.BuildInfoBomGeneratorParams ) (restoreEnv func () error , err error ) {
257+ func installPipDeps (params technologies.BuildInfoBomGeneratorParams ) (setupFileUsed bool , restoreEnv func () error , err error ) {
234258 restoreEnv , err = SetPipVirtualEnvPath ()
235259 if err != nil {
236260 return
@@ -256,7 +280,7 @@ func installPipDeps(params technologies.BuildInfoBomGeneratorParams) (restoreEnv
256280 }
257281 reportFileName = pythonReportFile
258282 }
259-
283+ setupFileUsed = params . PipRequirementsFile == ""
260284 pipInstallArgs := getPipInstallArgs (params .PipRequirementsFile , remoteUrl , curationCachePip , reportFileName , params .InstallCommandArgs ... )
261285 var reqErr error
262286 _ , err = executeCommand ("python" , pipInstallArgs ... )
@@ -269,6 +293,7 @@ func installPipDeps(params technologies.BuildInfoBomGeneratorParams) (restoreEnv
269293 } else {
270294 err = nil
271295 }
296+ setupFileUsed = false
272297 }
273298 if err != nil || reqErr != nil {
274299 if msgToUser := technologies .GetMsgToUserForCurationBlock (params .IsCurationCmd , techutils .Pip , errors .Join (err , reqErr ).Error ()); msgToUser != "" {
0 commit comments