Skip to content

Commit

Permalink
src/debugAdapter: make filepaths in errors absolute
Browse files Browse the repository at this point in the history
Errors from delve often contain relative paths, which may not be
relative to the workspace directory resulting in bad links. Try to
make the paths absolute before displaying to the user.

Fixes #456

Change-Id: I44c715db7b05c7dbb252a10bb1b5b4dd2f4efc21
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/252978
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
  • Loading branch information
suzmue committed Sep 10, 2020
1 parent 52260e0 commit f2fd351
Show file tree
Hide file tree
Showing 23 changed files with 47 additions and 36 deletions.
8 changes: 6 additions & 2 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ import { DebugProtocol } from 'vscode-debugprotocol';
import { parseEnvFiles } from '../utils/envUtils';
import {
envPath,
expandFilePathInOutput,
fixDriveCasingInWindows,
getBinPathWithPreferredGopathGoroot,
getCurrentGoWorkspaceFromGOPATH,
getInferredGopath,
} from '../utils/goPath';
import { killProcessTree } from '../utils/processUtils';
} from '../utils/pathUtils';
import {killProcessTree} from '../utils/processUtils';

const fsAccess = util.promisify(fs.access);
const fsUnlink = util.promisify(fs.unlink);
Expand Down Expand Up @@ -1763,6 +1764,9 @@ export class GoDebugSession extends LoggingDebugSession {
this.sendEvent(new OutputEvent(str, 'stdout'));
};
this.delve.onstderr = (str: string) => {
if (localPath.length > 0) {
str = expandFilePathInOutput(str, localPath);
}
this.sendEvent(new OutputEvent(str, 'stderr'));
};
this.delve.onclose = (code) => {
Expand Down
8 changes: 5 additions & 3 deletions src/debugAdapter2/goDlvDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TerminatedEvent
} from 'vscode-debugadapter';
import { DebugProtocol } from 'vscode-debugprotocol';
import { envPath, getBinPathWithPreferredGopathGoroot } from '../utils/goPath';
import { envPath, expandFilePathInOutput, getBinPathWithPreferredGopathGoroot } from '../utils/pathUtils';
import { killProcessTree } from '../utils/processUtils';

import { DAPClient } from './dapClient';
Expand Down Expand Up @@ -684,13 +684,15 @@ class DelveClient extends DAPClient {

log(`Running: ${dlvPath} ${dlvArgs.join(' ')}`);

const dir = parseProgramArgSync(launchArgs).dirname;
this.debugProcess = spawn(dlvPath, dlvArgs, {
cwd: parseProgramArgSync(launchArgs).dirname,
cwd: dir,
env
});

this.debugProcess.stderr.on('data', (chunk) => {
const str = chunk.toString();
let str = chunk.toString();
str = expandFilePathInOutput(str, dir);
this.emit('stderr', str);
});

Expand Down
2 changes: 1 addition & 1 deletion src/diffUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import jsDiff = require('diff');
import { Position, Range, TextEditorEdit, Uri, WorkspaceEdit } from 'vscode';
import { getBinPathFromEnvVar } from './utils/goPath';
import { getBinPathFromEnvVar } from './utils/pathUtils';

let diffToolAvailable: boolean | null = null;

Expand Down
2 changes: 1 addition & 1 deletion src/goBrowsePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import path = require('path');
import vscode = require('vscode');
import { getAllPackages } from './goPackages';
import { getBinPath, getCurrentGoPath, getImportPath } from './util';
import { envPath, getCurrentGoRoot } from './utils/goPath';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';

export function browsePackages() {
let workDir = '';
Expand Down
2 changes: 1 addition & 1 deletion src/goBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ICheckResult,
runTool
} from './util';
import { getCurrentGoWorkspaceFromGOPATH } from './utils/goPath';
import { getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';

/**
* Builds current package or workspace.
Expand Down
2 changes: 1 addition & 1 deletion src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
isPositionInString,
runGodoc
} from './util';
import { getCurrentGoRoot } from './utils/goPath';
import { getCurrentGoRoot } from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';

const missingToolMsg = 'Missing tool: ';
Expand Down
2 changes: 1 addition & 1 deletion src/goEnvironmentStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { toolInstallationEnvironment } from './goEnv';
import { hideGoStatus, outputChannel, showGoStatus } from './goStatus';
import { getFromGlobalState, getFromWorkspaceState, updateGlobalState, updateWorkspaceState } from './stateUtils';
import { getBinPath, getGoConfig, getGoVersion, getTempFilePath, GoVersion, rmdirRecursive } from './util';
import { correctBinname, getBinPathFromEnvVar, getCurrentGoRoot, pathExists } from './utils/goPath';
import { correctBinname, getBinPathFromEnvVar, getCurrentGoRoot, pathExists } from './utils/pathUtils';

export class GoEnvironmentOption {
public static fromQuickPickItem({ description, label }: vscode.QuickPickItem): GoEnvironmentOption {
Expand Down
2 changes: 1 addition & 1 deletion src/goGetPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import vscode = require('vscode');
import { buildCode } from './goBuild';
import { outputChannel } from './goStatus';
import { getBinPath, getCurrentGoPath, getImportPath } from './util';
import { envPath, getCurrentGoRoot } from './utils/goPath';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';

export function goGetPackage() {
const editor = vscode.window.activeTextEditor;
Expand Down
2 changes: 1 addition & 1 deletion src/goImplementations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getGoConfig,
getWorkspaceFolderPath
} from './util';
import { envPath, getCurrentGoRoot } from './utils/goPath';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';
import {killProcessTree} from './utils/processUtils';

interface GoListOutput {
Expand Down
2 changes: 1 addition & 1 deletion src/goImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { promptForMissingTool } from './goInstallTools';
import { documentSymbols, GoOutlineImportsOptions } from './goOutline';
import { getImportablePackages } from './goPackages';
import { getBinPath, getImportPath, parseFilePrelude } from './util';
import { envPath, getCurrentGoRoot } from './utils/goPath';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';

const missingToolMsg = 'Missing tool: ';

Expand Down
2 changes: 1 addition & 1 deletion src/goInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { toolExecutionEnvironment } from './goEnv';
import { isModSupported } from './goModules';
import { outputChannel } from './goStatus';
import { getBinPath, getCurrentGoPath, getGoConfig, getModuleCache } from './util';
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/goPath';
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';

export async function installCurrentPackage(): Promise<void> {
const editor = vscode.window.activeTextEditor;
Expand Down
2 changes: 1 addition & 1 deletion src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
GoVersion,
rmdirRecursive,
} from './util';
import { envPath, getCurrentGoRoot, getToolFromToolPath, setCurrentGoRoot } from './utils/goPath';
import { envPath, getCurrentGoRoot, getToolFromToolPath, setCurrentGoRoot } from './utils/pathUtils';

// declinedUpdates tracks the tools that the user has declined to update.
const declinedUpdates: Tool[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { getTool, Tool } from './goTools';
import { GoTypeDefinitionProvider } from './goTypeDefinition';
import { getFromGlobalState, updateGlobalState } from './stateUtils';
import { getBinPath, getCurrentGoPath, getGoConfig } from './util';
import { getToolFromToolPath } from './utils/goPath';
import { getToolFromToolPath } from './utils/pathUtils';

interface LanguageServerConfig {
serverName: string;
Expand Down
2 changes: 1 addition & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
isGoPathSet,
resolvePath,
} from './util';
import { clearCacheForTools, envPath, fileExists, getCurrentGoRoot, setCurrentGoRoot } from './utils/goPath';
import { clearCacheForTools, envPath, fileExists, getCurrentGoRoot, setCurrentGoRoot } from './utils/pathUtils';

export let buildDiagnosticCollection: vscode.DiagnosticCollection;
export let lintDiagnosticCollection: vscode.DiagnosticCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/goModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { installTools } from './goInstallTools';
import { getTool } from './goTools';
import { getFromGlobalState, updateGlobalState } from './stateUtils';
import { getBinPath, getGoConfig, getGoVersion, getModuleCache } from './util';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot } from './utils/goPath';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot } from './utils/pathUtils';

export let GO111MODULE: string;

Expand Down
2 changes: 1 addition & 1 deletion src/goPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getBinPath, getCurrentGoPath, getGoVersion, isVendorSupported } from './util';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/goPath';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';

type GopkgsDone = (res: Map<string, PackageInfo>) => void;
interface Cache {
Expand Down
2 changes: 1 addition & 1 deletion src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
parseFilePrelude,
runGodoc
} from './util';
import { getCurrentGoWorkspaceFromGOPATH } from './utils/goPath';
import { getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';

function vscodeKindFromGoCodeClass(kind: string, type: string): vscode.CompletionItemKind {
switch (kind) {
Expand Down
2 changes: 1 addition & 1 deletion src/goSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getBinPath, getGoConfig, getWorkspaceFolderPath} from './util';
import { getCurrentGoRoot } from './utils/goPath';
import { getCurrentGoRoot } from './utils/pathUtils';
import {killProcessTree} from './utils/processUtils';

// Keep in sync with github.com/acroca/go-symbols'
Expand Down
13 changes: 1 addition & 12 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
resolvePath
} from './util';
import { parseEnvFile } from './utils/envUtils';
import { envPath, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/goPath';
import { envPath, expandFilePathInOutput, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';

const testOutputChannel = vscode.window.createOutputChannel('Go Tests');
Expand Down Expand Up @@ -494,17 +494,6 @@ export function cancelRunningTests(): Thenable<boolean> {
});
}

function expandFilePathInOutput(output: string, cwd: string): string {
const lines = output.split('\n');
for (let i = 0; i < lines.length; i++) {
const matches = lines[i].match(/\s*(\S+\.go):(\d+):/);
if (matches && matches[1] && !path.isAbsolute(matches[1])) {
lines[i] = lines[i].replace(matches[1], path.join(cwd, matches[1]));
}
}
return lines.join('\n');
}

/**
* Get the test target arguments.
*
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getCurrentGoRoot,
getInferredGopath,
resolveHomeDir,
} from './utils/goPath';
} from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';

let userNameHash: number = 0;
Expand Down
16 changes: 16 additions & 0 deletions src/utils/goPath.ts → src/utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,19 @@ export function getToolFromToolPath(toolPath: string): string | undefined {
}
return tool;
}

/**
* Returns output with relative filepaths expanded using the provided directory
* @param output
* @param cwd
*/
export function expandFilePathInOutput(output: string, cwd: string): string {
const lines = output.split('\n');
for (let i = 0; i < lines.length; i++) {
const matches = lines[i].match(/\s*(\S+\.go):(\d+):/);
if (matches && matches[1] && !path.isAbsolute(matches[1])) {
lines[i] = lines[i].replace(matches[1], path.join(cwd, matches[1]));
}
}
return lines.join('\n');
}
2 changes: 1 addition & 1 deletion test/integration/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { toolInstallationEnvironment } from '../../src/goEnv';
import { installTools } from '../../src/goInstallTools';
import { allToolsInformation, getTool, getToolAtVersion } from '../../src/goTools';
import { getBinPath, getGoVersion, rmdirRecursive } from '../../src/util';
import { correctBinname } from '../../src/utils/goPath';
import { correctBinname } from '../../src/utils/pathUtils';

suite('Installation Tests', function () {
// Disable timeout when we are running slow tests.
Expand Down
2 changes: 1 addition & 1 deletion test/integration/statusbar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { updateGoVarsFromConfig } from '../../src/goInstallTools';
import { getWorkspaceState, setWorkspaceState } from '../../src/stateUtils';
import ourutil = require('../../src/util');
import { getCurrentGoRoot } from '../../src/utils/goPath';
import { getCurrentGoRoot } from '../../src/utils/pathUtils';
import { MockMemento } from '../mocks/MockMemento';

describe('#initGoStatusBar()', function () {
Expand Down

0 comments on commit f2fd351

Please sign in to comment.