@@ -21,8 +21,12 @@ import (
2121
2222func usage () {
2323 fmt .Fprintf (os .Stderr ,
24- `%s is a wrapper script that installs dependencies and calls the extractor, or if '--identify-environment'
25- is passed then it produces a file 'environment.json' which specifies what go version is needed.
24+ `When '--identify-environment' is passed then %s produces a file which specifies what Go
25+ version is needed. The location of this file is controlled by the environment variable
26+ CODEQL_EXTRACTOR_ENVIRONMENT_JSON, or defaults to "environment.json" if that is not set.
27+
28+ When no command line arguments are passed, %[1]s is a wrapper script that installs dependencies and
29+ calls the extractor.
2630
2731When LGTM_SRC is not set, the script installs dependencies as described below, and then invokes the
2832extractor in the working directory.
@@ -724,16 +728,15 @@ func checkForUnsupportedVersions(v versionInfo) (msg, version string) {
724728
725729func checkForVersionsNotFound (v versionInfo ) (msg , version string ) {
726730 if ! v .goInstallationFound && ! v .goDirectiveFound {
727- msg = "No version of Go installed and no `go.mod` file found. Writing an " +
728- "`environment.json` file specifying the maximum supported version of Go (" +
729- maxGoVersion + ")."
731+ msg = "No version of Go installed and no `go.mod` file found. Writing an environment " +
732+ "file specifying the maximum supported version of Go (" + maxGoVersion + ")."
730733 version = maxGoVersion
731734 diagnostics .EmitNoGoModAndNoGoEnv (msg )
732735 }
733736
734737 if ! v .goInstallationFound && v .goDirectiveFound {
735- msg = "No version of Go installed. Writing an ` environment.json` file specifying the " +
736- "version of Go found in the `go.mod` file (" + v .goModVersion + ")."
738+ msg = "No version of Go installed. Writing an environment file specifying the version " +
739+ "of Go found in the `go.mod` file (" + v .goModVersion + ")."
737740 version = v .goModVersion
738741 diagnostics .EmitNoGoEnv (msg )
739742 }
@@ -751,8 +754,8 @@ func compareVersions(v versionInfo) (msg, version string) {
751754 if semver .Compare ("v" + v .goModVersion , "v" + v .goEnvVersion ) > 0 {
752755 msg = "The version of Go installed in the environment (" + v .goEnvVersion +
753756 ") is lower than the version found in the `go.mod` file (" + v .goModVersion +
754- ").\n Writing an ` environment.json` file specifying the version of Go from the " +
755- "`go.mod` file (" + v .goModVersion + ")."
757+ ").\n Writing an environment file specifying the version of Go from the `go.mod` " +
758+ "file (" + v .goModVersion + ")."
756759 version = v .goModVersion
757760 diagnostics .EmitVersionGoModHigherVersionEnvironment (msg )
758761 } else {
@@ -788,22 +791,27 @@ func writeEnvironmentFile(version string) {
788791 content = `{ "include": [ { "go": { "version": "` + version + `" } } ] }`
789792 }
790793
791- targetFile , err := os .Create ("environment.json" )
794+ filename , ok := os .LookupEnv ("CODEQL_EXTRACTOR_ENVIRONMENT_JSON" )
795+ if ! ok {
796+ filename = "environment.json"
797+ }
798+
799+ targetFile , err := os .Create (filename )
792800 if err != nil {
793- log .Println ("Failed to create environment.json : " )
801+ log .Println ("Failed to create environment file " + filename + " : " )
794802 log .Println (err )
795803 return
796804 }
797805 defer func () {
798806 if err := targetFile .Close (); err != nil {
799- log .Println ("Failed to close environment.json :" )
807+ log .Println ("Failed to close environment file " + filename + " :" )
800808 log .Println (err )
801809 }
802810 }()
803811
804812 _ , err = targetFile .WriteString (content )
805813 if err != nil {
806- log .Println ("Failed to write to environment.json : " )
814+ log .Println ("Failed to write to environment file " + filename + " : " )
807815 log .Println (err )
808816 }
809817}
0 commit comments