From de8d6b4c46afa1a0ddca63a7af0db38a89233827 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 22 Mar 2024 16:14:41 +0800 Subject: [PATCH 1/7] update:match gop path when get relative path --- src/utils/pathUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/pathUtils.ts b/src/utils/pathUtils.ts index ead04ac328..86f9ed3df1 100644 --- a/src/utils/pathUtils.ts +++ b/src/utils/pathUtils.ts @@ -269,10 +269,10 @@ export function getToolFromToolPath(toolPath: string): string | undefined { 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+):/); + const matches = lines[i].match(/\s*(\S+\.(go|gop)):(\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'); -} +} \ No newline at end of file From 19b97a769b9c774216269c33560d5cd45bf8130b Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 22 Mar 2024 16:18:06 +0800 Subject: [PATCH 2/7] 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..5d57e53390 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 ? modPath : testconfig.dir) + ); + }); 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 From f173ccbe589602efdbd7923e04eb2f149ce9b0d3 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Sun, 24 Mar 2024 10:04:47 +0800 Subject: [PATCH 3/7] style:format code --- src/utils/pathUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/pathUtils.ts b/src/utils/pathUtils.ts index c73cd5aea4..0f2854e425 100644 --- a/src/utils/pathUtils.ts +++ b/src/utils/pathUtils.ts @@ -295,4 +295,4 @@ export function getModPath(dir: string): string | undefined { } return undefined; -} \ No newline at end of file +} From 0f7a73156e32433ff34ad1e6a488d3aa1e085f4d Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Sun, 24 Mar 2024 13:31:33 +0800 Subject: [PATCH 4/7] fix:use getModFolderPath --- src/testUtils.ts | 14 +++++++------- src/utils/pathUtils.ts | 20 -------------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/testUtils.ts b/src/testUtils.ts index 5d57e53390..8d54deade5 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -18,7 +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 { getModFolderPath } from './goModules'; import { parseEnvFile } from './utils/envUtils'; import { getEnvPath, @@ -313,7 +313,7 @@ export async function goTest(testconfig: TestConfig): Promise { outputChannel.appendLine(['Running tool:', goRuntimePath, ...outArgs].join(' ')); outputChannel.appendLine(''); - + outputChannel.appendLine('isgop:' + testconfig.isGop); let testResult = false; try { testResult = await new Promise(async (resolve, reject) => { @@ -346,14 +346,14 @@ export async function goTest(testconfig: TestConfig): Promise { } }); - const modPath = getModPath(testconfig.dir) || testconfig.dir; + // When go+ test output error occurs, the path thrown by cl is relative to the project root directory + const errOutputDir = testconfig.isGop + ? (await getModFolderPath(vscode.Uri.file(testconfig.dir), true)) || testconfig.dir + : testconfig.dir; // go test emits build errors on stderr, which contain paths relative to the cwd 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 ? modPath : testconfig.dir) - ); + return outputChannel.appendLine(expandFilePathInOutput(line, errOutputDir)); }); errBuf.onDone((last) => last && outputChannel.appendLine(expandFilePathInOutput(last, testconfig.dir))); diff --git a/src/utils/pathUtils.ts b/src/utils/pathUtils.ts index 0f2854e425..34539f5fb1 100644 --- a/src/utils/pathUtils.ts +++ b/src/utils/pathUtils.ts @@ -276,23 +276,3 @@ 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; -} From 9e5289d7751bd3320740df5b80802901e33288b5 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 25 Mar 2024 16:58:30 +0800 Subject: [PATCH 5/7] update:support goptest output errors relative to package --- src/testUtils.ts | 22 ++++++++++++---------- src/utils/pathUtils.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/testUtils.ts b/src/testUtils.ts index 8d54deade5..93df7733ac 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -24,7 +24,8 @@ import { getEnvPath, expandFilePathInOutput, getCurrentGoRoot, - getCurrentGoWorkspaceFromGOPATH + getCurrentGoWorkspaceFromGOPATH, + expandFilePathInErrorOutput } from './utils/pathUtils'; import { killProcessTree } from './utils/processUtils'; import { GoExtensionContext } from './context'; @@ -313,7 +314,6 @@ export async function goTest(testconfig: TestConfig): Promise { outputChannel.appendLine(['Running tool:', goRuntimePath, ...outArgs].join(' ')); outputChannel.appendLine(''); - outputChannel.appendLine('isgop:' + testconfig.isGop); let testResult = false; try { testResult = await new Promise(async (resolve, reject) => { @@ -345,17 +345,19 @@ export async function goTest(testconfig: TestConfig): Promise { testResultLines.forEach((line) => outputChannel.appendLine(line)); } }); - - // When go+ test output error occurs, the path thrown by cl is relative to the project root directory - const errOutputDir = testconfig.isGop - ? (await getModFolderPath(vscode.Uri.file(testconfig.dir), true)) || testconfig.dir - : testconfig.dir; - + const modPath = (await getModFolderPath(vscode.Uri.file(testconfig.dir), true)) || testconfig.dir; // go test emits build errors on stderr, which contain paths relative to the cwd errBuf.onLine((line) => { - return outputChannel.appendLine(expandFilePathInOutput(line, errOutputDir)); + outputChannel.appendLine( + expandFilePathInErrorOutput(line, testconfig.dir, !!testconfig.isGop, modPath) + ); + }); + errBuf.onDone((last) => { + last && + outputChannel.appendLine( + expandFilePathInErrorOutput(last, testconfig.dir, !!testconfig.isGop, modPath) + ); }); - errBuf.onDone((last) => last && outputChannel.appendLine(expandFilePathInOutput(last, testconfig.dir))); tp.stdout.on('data', (chunk) => outBuf.append(chunk.toString())); tp.stderr.on('data', (chunk) => errBuf.append(chunk.toString())); diff --git a/src/utils/pathUtils.ts b/src/utils/pathUtils.ts index 34539f5fb1..b3ada6f08e 100644 --- a/src/utils/pathUtils.ts +++ b/src/utils/pathUtils.ts @@ -276,3 +276,39 @@ export function expandFilePathInOutput(output: string, cwd: string): string { } return lines.join('\n'); } + +/** + * This function accepts the error output of goptest or gotest and converts any relative file paths within it to absolute file paths. + * @param output + * @param cwd + * @param isGop + * @param modPath + * @returns + */ +export function expandFilePathInErrorOutput(output: string, cwd: string, isGop: boolean, modPath: string): string { + const lines = output.split('\n'); + for (let i = 0; i < lines.length; i++) { + const matches = lines[i].match(/\s*(\S+\.(go|gop)):(\d+):/); + if (matches && matches[1]) { + const file = matches[1]; + const fileExt = path.extname(file); + let absoluteFilePath = ''; + if (path.isAbsolute(file)) { + absoluteFilePath = file; + } else { + // When go+ test output error occurs, the path thrown by cl is relative to the project root directory + if (fileExt === '.go') { + let outputDir = cwd; + if (isGop && path.dirname(file) !== '.') { + // If it is the output of goptest and the file path is not relative to the current directory, + // update outputDir to modPath as the output directory is relative to the module path + outputDir = modPath; + } + absoluteFilePath = path.join(outputDir, file); + } else if (fileExt === '.gop') absoluteFilePath = path.join(modPath, file); + } + lines[i] = lines[i].replace(file, absoluteFilePath); + } + } + return lines.join('\n'); +} From e3da992f28226dbb7d0f714c98d30afc701e375b Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 25 Mar 2024 21:16:38 +0800 Subject: [PATCH 6/7] update:add .gox file error output --- src/utils/pathUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/pathUtils.ts b/src/utils/pathUtils.ts index b3ada6f08e..07575f4ce3 100644 --- a/src/utils/pathUtils.ts +++ b/src/utils/pathUtils.ts @@ -269,7 +269,7 @@ export function getToolFromToolPath(toolPath: string): string | undefined { 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|gop)):(\d+):/); + 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])); } @@ -288,7 +288,7 @@ export function expandFilePathInOutput(output: string, cwd: string): string { export function expandFilePathInErrorOutput(output: string, cwd: string, isGop: boolean, modPath: string): string { const lines = output.split('\n'); for (let i = 0; i < lines.length; i++) { - const matches = lines[i].match(/\s*(\S+\.(go|gop)):(\d+):/); + const matches = lines[i].match(/\s*(\S+\.(go|gop|gox)):(\d+):/); if (matches && matches[1]) { const file = matches[1]; const fileExt = path.extname(file); @@ -305,7 +305,7 @@ export function expandFilePathInErrorOutput(output: string, cwd: string, isGop: outputDir = modPath; } absoluteFilePath = path.join(outputDir, file); - } else if (fileExt === '.gop') absoluteFilePath = path.join(modPath, file); + } else if (['.gop', '.gox'].includes(fileExt)) absoluteFilePath = path.join(modPath, file); } lines[i] = lines[i].replace(file, absoluteFilePath); } From 869e4883a42953d15a4533f439a7349a60b2f3a5 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 16 Apr 2024 18:34:54 +0800 Subject: [PATCH 7/7] fix:prevents unnecessary mod path acquisition --- src/testUtils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/testUtils.ts b/src/testUtils.ts index 93df7733ac..8fb65edd3b 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -345,7 +345,10 @@ export async function goTest(testconfig: TestConfig): Promise { testResultLines.forEach((line) => outputChannel.appendLine(line)); } }); - const modPath = (await getModFolderPath(vscode.Uri.file(testconfig.dir), true)) || testconfig.dir; + const modPath = + testconfig.isMod && testconfig.isGop + ? (await getModFolderPath(vscode.Uri.file(testconfig.dir), true)) || testconfig.dir + : testconfig.dir; // go test emits build errors on stderr, which contain paths relative to the cwd errBuf.onLine((line) => { outputChannel.appendLine(