Skip to content

Commit

Permalink
update:correct absolute path output when goptest's cl error
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Mar 22, 2024
1 parent 541c183 commit 797f5bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -345,8 +346,15 @@ export async function goTest(testconfig: TestConfig): Promise<boolean> {
}
});

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()));
Expand Down
20 changes: 20 additions & 0 deletions src/utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 797f5bb

Please sign in to comment.