@@ -343,7 +343,7 @@ async function installToolWithGo(
343
343
344
344
let version : semver . SemVer | string | undefined | null = tool . version ;
345
345
if ( ! version && tool . usePrereleaseInPreviewMode && extensionInfo . isPreview ) {
346
- version = await latestToolVersion ( tool , true ) ;
346
+ version = await latestModuleVersion ( tool . modulePath , true ) ;
347
347
}
348
348
// TODO(hyangah): should we allow to choose a different version of the tool
349
349
// depending on the project's go version (i.e. getGoVersion())? For example,
@@ -733,17 +733,28 @@ async function suggestDownloadGo() {
733
733
}
734
734
735
735
/**
736
- * The output of `go list -m -versions -json`.
736
+ * Interface for the expected JSON output from `go list -m -versions -json`.
737
+ * See https://go.dev/ref/mod#go-list-m for details.
737
738
*/
738
739
interface ListVersionsOutput {
739
- Version : string ; // module version
740
- Versions ?: string [ ] ; // available module versions (with -versions)
740
+ /**
741
+ * The latest tagged release version (e.g., v1.2.3). Excludes pre-releases.
742
+ */
743
+ Version : string ;
744
+ /**
745
+ * All known versions of the module, sorted semantically from earliest to
746
+ * latest. Includes pre-release versions.
747
+ */
748
+ Versions ?: string [ ] ;
741
749
}
742
750
743
751
/**
744
- * Returns the latest version of the tool .
752
+ * Returns the latest published versions of a Go module .
745
753
*/
746
- export async function latestToolVersion ( tool : Tool , includePrerelease ?: boolean ) : Promise < semver . SemVer | null > {
754
+ export async function latestModuleVersion (
755
+ modulePath : string ,
756
+ includePrerelease ?: boolean
757
+ ) : Promise < semver . SemVer | null > {
747
758
const goCmd = getBinPath ( 'go' ) ;
748
759
const tmpDir = await tmpDirForToolInstallation ( ) ;
749
760
const execFile = util . promisify ( cp . execFile ) ;
@@ -753,27 +764,28 @@ export async function latestToolVersion(tool: Tool, includePrerelease?: boolean)
753
764
try {
754
765
const env = toolInstallationEnvironment ( ) ;
755
766
env [ 'GO111MODULE' ] = 'on' ;
756
- // Run go list in a temp directory to avoid altering go.mod
757
- // when using older versions of go (<1.16).
758
- const version = 'latest' ; // TODO(hyangah): use 'master' for delve-dap.
759
- const { stdout } = await execFile (
760
- goCmd ,
761
- [ 'list' , '-m' , '--versions' , '-json' , ` ${ tool . modulePath } @ ${ version } ` ] ,
762
- {
763
- env ,
764
- cwd : tmpDir
765
- }
766
- ) ;
767
- const m = < ListVersionsOutput > JSON . parse ( stdout ) ;
768
- // Versions field is a list of all known versions of the module,
769
- // ordered according to semantic versioning, earliest to latest.
770
- const latest = includePrerelease && m . Versions && m . Versions . length > 0 ? m . Versions . pop ( ) : m . Version ;
767
+ // Run go list in a temp directory to avoid altering go.mod when using
768
+ // older versions of go (<1.16).
769
+ const { stdout } = await execFile ( goCmd , [ 'list' , '-m' , '--versions' , '-json' , ` ${ modulePath } @latest` ] , {
770
+ env ,
771
+ cwd : tmpDir
772
+ } ) ;
773
+ const moduleInfo = JSON . parse ( stdout ) as ListVersionsOutput ;
774
+
775
+ let latest : string ;
776
+ if ( includePrerelease && moduleInfo . Versions && moduleInfo . Versions . length > 0 ) {
777
+ latest = moduleInfo . Versions [ moduleInfo . Versions . length - 1 ] ;
778
+ } else {
779
+ latest = moduleInfo . Version ;
780
+ }
781
+
771
782
ret = semver . parse ( latest ) ;
772
783
} catch ( e ) {
773
- console . log ( `failed to retrieve the latest tool ${ tool . name } version : ${ e } ` ) ;
784
+ console . log ( `failed to retrieve the latest version of module ${ modulePath } : ${ e } ` ) ;
774
785
} finally {
775
786
rmdirRecursive ( tmpDir ) ;
776
787
}
788
+
777
789
return ret ;
778
790
}
779
791
0 commit comments