From 797f5bbbed4802f57feb6cb61c1f4977843cea35 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 22 Mar 2024 16:18:06 +0800 Subject: [PATCH] update:correct absolute path output when goptest's cl error --- src/testUtils.ts | 10 +++++++++- src/utils/pathUtils.ts | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/testUtils.ts b/src/testUtils.ts index 526516b227..51206d1497 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -18,6 +18,7 @@ import { getCurrentPackage } from './goModules'; import { GoDocumentSymbolProvider } from './goDocumentSymbols'; import { getNonVendorPackages } from './goPackages'; import { getBinPath, getCurrentGoPath, getTempFilePath, LineBuffer, resolvePath } from './util'; +import { getModPath } from './utils/pathUtils'; import { parseEnvFile } from './utils/envUtils'; import { getEnvPath, @@ -345,8 +346,15 @@ export async function goTest(testconfig: TestConfig): Promise { } }); + const modPath = getModPath(testconfig.dir)||testconfig.dir; + // go test emits build errors on stderr, which contain paths relative to the cwd - errBuf.onLine((line) => outputChannel.appendLine(expandFilePathInOutput(line, testconfig.dir))); + errBuf.onLine((line) => { + return outputChannel.appendLine( + // When go+ test output error occurs, the path thrown by cl is relative to the project root directory + expandFilePathInOutput(line, testconfig.isGop ? testconfig.dir : modPath) + ); + }); errBuf.onDone((last) => last && outputChannel.appendLine(expandFilePathInOutput(last, testconfig.dir))); tp.stdout.on('data', (chunk) => outBuf.append(chunk.toString())); diff --git a/src/utils/pathUtils.ts b/src/utils/pathUtils.ts index 86f9ed3df1..c73cd5aea4 100644 --- a/src/utils/pathUtils.ts +++ b/src/utils/pathUtils.ts @@ -275,4 +275,24 @@ export function expandFilePathInOutput(output: string, cwd: string): string { } } return lines.join('\n'); +} + +/** + * Returns the path to the go.mod file for the given directory + * @param dir + * @returns + */ +export function getModPath(dir: string): string | undefined { + let currentDir = dir; + + while (currentDir !== '/') { + const modFilePath = path.join(currentDir, 'go.mod'); + if (fs.existsSync(modFilePath)) { + return currentDir; + } + + currentDir = path.dirname(currentDir); + } + + return undefined; } \ No newline at end of file