This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
Sort import packages by std lib first #2803
Merged
ramya-rao-a
merged 10 commits into
microsoft:master
from
karthikraobr:fix/imports-stdlib-first
Nov 4, 2019
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d451d5
Sort import packages by std lib first
karthikraobr 8a8aae9
Use DIR directive to determine if package is part of stdlib
karthikraobr e8d97af
Store stdlib inforrmation from gopkg
karthikraobr f316b68
Merge master
karthikraobr 9288947
Handle goroot not set
karthikraobr 6e32f8b
Fi sorrting
karthikraobr 4e4ee62
Merge master
karthikraobr 926b291
Improve sorting perf
ramya-rao-a f4bdf41
Cater to old gopkgs tool
ramya-rao-a 29790f4
Fix linting issues
ramya-rao-a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,12 +10,17 @@ import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools'; | |||||
import { envPath, fixDriveCasingInWindows, getCurrentGoWorkspaceFromGOPATH } from './goPath'; | ||||||
import { getBinPath, getCurrentGoPath, getGoVersion, getToolsEnvVars, isVendorSupported, sendTelemetryEvent } from './util'; | ||||||
|
||||||
type GopkgsDone = (res: Map<string, string>) => void; | ||||||
type GopkgsDone = (res: Map<string, PackageInfo>) => void; | ||||||
interface Cache { | ||||||
entry: Map<string, string>; | ||||||
entry: Map<string, PackageInfo>; | ||||||
lastHit: number; | ||||||
} | ||||||
|
||||||
export interface PackageInfo { | ||||||
name: string; | ||||||
isStd: boolean; | ||||||
} | ||||||
|
||||||
let gopkgsNotified: boolean = false; | ||||||
let cacheTimeout: number = 5000; | ||||||
|
||||||
|
@@ -26,16 +31,17 @@ const allPkgsCache: Map<string, Cache> = new Map<string, Cache>(); | |||||
|
||||||
const pkgRootDirs = new Map<string, string>(); | ||||||
|
||||||
function gopkgs(workDir?: string): Promise<Map<string, string>> { | ||||||
|
||||||
function gopkgs(workDir?: string): Promise<Map<string, PackageInfo>> { | ||||||
const gopkgsBinPath = getBinPath('gopkgs'); | ||||||
if (!path.isAbsolute(gopkgsBinPath)) { | ||||||
promptForMissingTool('gopkgs'); | ||||||
return Promise.resolve(new Map<string, string>()); | ||||||
return Promise.resolve(new Map<string, PackageInfo>()); | ||||||
} | ||||||
|
||||||
const t0 = Date.now(); | ||||||
return new Promise<Map<string, string>>((resolve, reject) => { | ||||||
const args = ['-format', '{{.Name}};{{.ImportPath}}']; | ||||||
return new Promise<Map<string, PackageInfo>>((resolve, reject) => { | ||||||
const args = ['-format', '{{.Name}};{{.ImportPath}};{{.Dir}}']; | ||||||
if (workDir) { | ||||||
args.push('-workDir', workDir); | ||||||
} | ||||||
|
@@ -48,7 +54,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> { | |||||
cmd.stderr.on('data', d => errchunks.push(d)); | ||||||
cmd.on('error', e => err = e); | ||||||
cmd.on('close', () => { | ||||||
const pkgs = new Map<string, string>(); | ||||||
const pkgs = new Map<string, PackageInfo>(); | ||||||
if (err && err.code === 'ENOENT') { | ||||||
return promptForMissingTool('gopkgs'); | ||||||
} | ||||||
|
@@ -64,7 +70,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> { | |||||
console.log(`Running gopkgs failed with "${errorMsg}"\nCheck if you can run \`gopkgs -format {{.Name}};{{.ImportPath}}\` in a terminal successfully.`); | ||||||
return resolve(pkgs); | ||||||
} | ||||||
|
||||||
const goroot = process.env['GOROOT'] | ||||||
const output = chunks.join(''); | ||||||
if (output.indexOf(';') === -1) { | ||||||
// User might be using the old gopkgs tool, prompt to update | ||||||
|
@@ -75,19 +81,23 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> { | |||||
} | ||||||
const index = pkgPath.lastIndexOf('/'); | ||||||
const pkgName = index === -1 ? pkgPath : pkgPath.substr(index + 1); | ||||||
pkgs.set(pkgPath, pkgName); | ||||||
pkgs.set(pkgPath, { | ||||||
name: pkgName, | ||||||
isStd: goroot === null ? false : pkgPath.startsWith(goroot) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So, we might as well set this to I'll soon retire this part of the code as we don't expect anyone to be using the old There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or another option is to use this very crude logic I currently use for the auto completion widget
Suggested change
|
||||||
}); | ||||||
}); | ||||||
return resolve(pkgs); | ||||||
} | ||||||
|
||||||
output.split('\n').forEach((pkgDetail) => { | ||||||
if (!pkgDetail || !pkgDetail.trim() || pkgDetail.indexOf(';') === -1) { | ||||||
return; | ||||||
} | ||||||
const [pkgName, pkgPath] = pkgDetail.trim().split(';'); | ||||||
pkgs.set(pkgPath, pkgName); | ||||||
const [pkgName, pkgPath, pkgDir] = pkgDetail.trim().split(';'); | ||||||
pkgs.set(pkgPath, { | ||||||
name: pkgName, | ||||||
isStd: goroot === null ? false : pkgDir.startsWith(goroot) | ||||||
}); | ||||||
}); | ||||||
|
||||||
const timeTaken = Date.now() - t0; | ||||||
/* __GDPR__ | ||||||
"gopkgs" : { | ||||||
|
@@ -102,10 +112,10 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> { | |||||
}); | ||||||
} | ||||||
|
||||||
function getAllPackagesNoCache(workDir: string): Promise<Map<string, string>> { | ||||||
return new Promise<Map<string, string>>((resolve, reject) => { | ||||||
function getAllPackagesNoCache(workDir: string): Promise<Map<string, PackageInfo>> { | ||||||
return new Promise<Map<string, PackageInfo>>((resolve, reject) => { | ||||||
// Use subscription style to guard costly/long running invocation | ||||||
const callback = function(pkgMap: Map<string, string>) { | ||||||
const callback = function (pkgMap: Map<string, PackageInfo>) { | ||||||
resolve(pkgMap); | ||||||
}; | ||||||
|
||||||
|
@@ -134,7 +144,7 @@ function getAllPackagesNoCache(workDir: string): Promise<Map<string, string>> { | |||||
* @argument workDir. The workspace directory of the project. | ||||||
* @returns Map<string, string> mapping between package import path and package name | ||||||
*/ | ||||||
export async function getAllPackages(workDir: string): Promise<Map<string, string>> { | ||||||
export async function getAllPackages(workDir: string): Promise<Map<string, PackageInfo>> { | ||||||
const cache = allPkgsCache.get(workDir); | ||||||
const useCache = cache && (new Date().getTime() - cache.lastHit) < cacheTimeout; | ||||||
if (useCache) { | ||||||
|
@@ -163,32 +173,32 @@ export async function getAllPackages(workDir: string): Promise<Map<string, strin | |||||
* @param useCache. Force to use cache | ||||||
* @returns Map<string, string> mapping between package import path and package name | ||||||
*/ | ||||||
export function getImportablePackages(filePath: string, useCache: boolean = false): Promise<Map<string, string>> { | ||||||
export function getImportablePackages(filePath: string, useCache: boolean = false): Promise<Map<string, PackageInfo>> { | ||||||
filePath = fixDriveCasingInWindows(filePath); | ||||||
const fileDirPath = path.dirname(filePath); | ||||||
|
||||||
let foundPkgRootDir = pkgRootDirs.get(fileDirPath); | ||||||
const workDir = foundPkgRootDir || fileDirPath; | ||||||
const cache = allPkgsCache.get(workDir); | ||||||
|
||||||
const getAllPackagesPromise: Promise<Map<string, string>> = useCache && cache | ||||||
const getAllPackagesPromise: Promise<Map<string, PackageInfo>> = useCache && cache | ||||||
? Promise.race([getAllPackages(workDir), cache.entry]) | ||||||
: getAllPackages(workDir); | ||||||
|
||||||
return Promise.all([isVendorSupported(), getAllPackagesPromise]).then(([vendorSupported, pkgs]) => { | ||||||
const pkgMap = new Map<string, string>(); | ||||||
const pkgMap = new Map<string, PackageInfo>(); | ||||||
if (!pkgs) { | ||||||
return pkgMap; | ||||||
} | ||||||
|
||||||
const currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath); | ||||||
pkgs.forEach((pkgName, pkgPath) => { | ||||||
if (pkgName === 'main') { | ||||||
pkgs.forEach((info, pkgPath) => { | ||||||
if (info.name === 'main') { | ||||||
return; | ||||||
} | ||||||
|
||||||
if (!vendorSupported || !currentWorkspace) { | ||||||
pkgMap.set(pkgPath, pkgName); | ||||||
pkgMap.set(pkgPath, info); | ||||||
return; | ||||||
} | ||||||
|
||||||
|
@@ -208,7 +218,7 @@ export function getImportablePackages(filePath: string, useCache: boolean = fals | |||||
|
||||||
const allowToImport = isAllowToImportPackage(fileDirPath, currentWorkspace, relativePkgPath); | ||||||
if (allowToImport) { | ||||||
pkgMap.set(relativePkgPath, pkgName); | ||||||
pkgMap.set(relativePkgPath, info); | ||||||
} | ||||||
}); | ||||||
return pkgMap; | ||||||
|
@@ -301,4 +311,4 @@ function isAllowToImportPackage(toDirPath: string, currentWorkspace: string, pkg | |||||
return toDirPath.startsWith(rootProjectForInternalPkg + path.sep) || toDirPath === rootProjectForInternalPkg; | ||||||
} | ||||||
return true; | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sorting will only ensure that the standard libraries appear before the others.
But among the standard libraries themselves, we lose the sorting
Similarly, we also lose the sorting among the non standard libraries