@@ -343,7 +343,7 @@ async function installToolWithGo(
343343
344344 let version : semver . SemVer | string | undefined | null = tool . version ;
345345 if ( ! version && tool . usePrereleaseInPreviewMode && extensionInfo . isPreview ) {
346- version = await latestToolVersion ( tool , true ) ;
346+ version = await latestModuleVersion ( tool . modulePath , true ) ;
347347 }
348348 // TODO(hyangah): should we allow to choose a different version of the tool
349349 // depending on the project's go version (i.e. getGoVersion())? For example,
@@ -733,17 +733,28 @@ async function suggestDownloadGo() {
733733}
734734
735735/**
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.
737738 */
738739interface 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 [ ] ;
741749}
742750
743751/**
744- * Returns the latest version of the tool .
752+ * Returns the latest published versions of a Go module .
745753 */
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 > {
747758 const goCmd = getBinPath ( 'go' ) ;
748759 const tmpDir = await tmpDirForToolInstallation ( ) ;
749760 const execFile = util . promisify ( cp . execFile ) ;
@@ -753,27 +764,28 @@ export async function latestToolVersion(tool: Tool, includePrerelease?: boolean)
753764 try {
754765 const env = toolInstallationEnvironment ( ) ;
755766 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+
771782 ret = semver . parse ( latest ) ;
772783 } 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 } ` ) ;
774785 } finally {
775786 rmdirRecursive ( tmpDir ) ;
776787 }
788+
777789 return ret ;
778790}
779791
0 commit comments