Skip to content

Commit cb7334c

Browse files
committed
fix formatting cwd fallback and add test
1 parent 1b1d9d0 commit cb7334c

File tree

9 files changed

+79
-7
lines changed

9 files changed

+79
-7
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@
607607
"@types/glob": "^8.1.0",
608608
"@types/mocha": "~10.0.6",
609609
"@types/node": "~22.10.0",
610+
"@types/proxyquire": "^1.3.31",
610611
"@types/vscode": "^1.75.0",
611612
"@types/which": "^3.0.3",
612613
"@typescript-eslint/eslint-plugin": "^7.3.1",

src/ctags.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as vscode from 'vscode';
33
import {exec as execNonPromise} from 'child_process';
44
import * as util from 'util';
5+
import * as path from 'path';
56
import { Logger } from './logger';
67
const exec = util.promisify(execNonPromise);
78

@@ -366,7 +367,11 @@ export class CtagsManager {
366367
}
367368

368369
// kick off async job for indexing for module.sv
369-
let searchPattern = new vscode.RelativePattern(vscode.workspace.workspaceFolders[0], `**/${moduleToFind}.sv`);
370+
const wsFolders = vscode.workspace.workspaceFolders;
371+
const base = wsFolders && wsFolders.length > 0
372+
? wsFolders[0]
373+
: path.dirname(document.uri.fsPath);
374+
let searchPattern = new vscode.RelativePattern(base, `**/${moduleToFind}.sv`);
370375
let files = await vscode.workspace.findFiles(searchPattern);
371376
if (files.length !== 0) {
372377
let file = await vscode.workspace.openTextDocument(files[0]);

src/linter/BaseLinter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export default abstract class BaseLinter {
1919
if (path.isAbsolute(inputPath)) {
2020
return inputPath;
2121
}
22-
return path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, inputPath);
22+
const wsFolders = vscode.workspace.workspaceFolders;
23+
const base = wsFolders && wsFolders.length > 0 ? wsFolders[0].uri.fsPath : '';
24+
return path.join(base, inputPath);
2325
}
2426

2527
public startLint(doc: vscode.TextDocument) {

src/linter/IcarusLinter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ export default class IcarusLinter extends BaseLinter {
7272
let command: string = binPath + ' ' + args.join(' ');
7373

7474
// TODO: We have to apply the the #419 fix?
75+
const wsFolders = vscode.workspace.workspaceFolders;
7576
let cwd: string =
76-
this.runAtFileLocation || vscode.workspace.workspaceFolders === undefined
77+
this.runAtFileLocation || !wsFolders || wsFolders.length === 0
7778
? path.dirname(doc.uri.fsPath)
78-
: vscode.workspace.workspaceFolders[0].uri.fsPath;
79+
: wsFolders[0].uri.fsPath;
7980

8081
this.logger.info('Execute');
8182
this.logger.info(' command: ', command);

src/linter/SlangLinter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ export default class SlangLinter extends BaseLinter {
7878
args.push(`"${docUri}"`);
7979
let command: string = binPath + ' ' + args.join(' ');
8080

81+
const wsFolders = vscode.workspace.workspaceFolders;
8182
let cwd: string = this.runAtFileLocation
8283
? isWindows
8384
? cwdWin
8485
: docFolder
85-
: vscode.workspace.workspaceFolders[0].uri.fsPath;
86+
: wsFolders && wsFolders.length > 0
87+
? wsFolders[0].uri.fsPath
88+
: path.dirname(doc.uri.fsPath);
8689

8790
this.logger.info('[slang] Execute');
8891
this.logger.info('[slang] command: ' + command);

src/linter/VerilatorLinter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ export default class VerilatorLinter extends BaseLinter {
7676
? this.convertToWslPath(path.dirname(doc.uri.fsPath))
7777
: path.dirname(doc.uri.fsPath).replace(/\\/g, '/')
7878
: path.dirname(doc.uri.fsPath);
79+
const wsFolders = vscode.workspace.workspaceFolders;
7980
let cwd: string = this.runAtFileLocation
8081
? isWindows
8182
? path.dirname(doc.uri.fsPath.replace(/\\/g, '/'))
8283
: docFolder
83-
: vscode.workspace.workspaceFolders[0].uri.fsPath;
84+
: wsFolders && wsFolders.length > 0
85+
? wsFolders[0].uri.fsPath
86+
: path.dirname(doc.uri.fsPath);
8487
let verilator: string = isWindows
8588
? this.useWSL
8689
? 'wsl verilator'

src/providers/FormatPrivider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ abstract class FileBasedFormattingEditProvider implements vscode.DocumentFormatt
6868
let binPath: string = this.config.get('path');
6969
this.logger.info('Executing command: ' + binPath + ' ' + args.join(' '));
7070
try {
71-
child_process.execFileSync(binPath, args, {cwd: vscode.workspace.workspaceFolders[0].uri.fsPath});
71+
const wsFolders = vscode.workspace.workspaceFolders;
72+
const cwd =
73+
wsFolders && wsFolders.length > 0
74+
? wsFolders[0].uri.fsPath
75+
: path.dirname(document.uri.fsPath);
76+
child_process.execFileSync(binPath, args, { cwd });
7277
let formattedText: string = tempFile.readFileSync({ encoding: 'utf-8' });
7378
let wholeFileRange: vscode.Range = new vscode.Range(
7479
document.positionAt(0),

src/test/suite/format.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as assert from 'assert';
2+
import * as fs from 'fs';
3+
import * as os from 'os';
4+
import * as path from 'path';
5+
import * as proxyquire from 'proxyquire';
6+
7+
suite('Formatting Provider', () => {
8+
test('does not crash without workspace', () => {
9+
const tmp = path.join(os.tmpdir(), 'fmt.v');
10+
fs.writeFileSync(tmp, 'module m; endmodule');
11+
12+
const vscodeStub = {
13+
workspace: {
14+
getConfiguration: () => ({ get: () => '/bin/true' }),
15+
workspaceFolders: undefined,
16+
},
17+
Range: class { constructor(public start: any, public end: any) {} },
18+
Position: class { constructor(public line: number, public char: number) {} },
19+
TextEdit: { replace: (_r: any, _t: any) => ({}) },
20+
};
21+
22+
const providerModule = proxyquire('../../providers/FormatPrivider', {
23+
vscode: vscodeStub,
24+
});
25+
26+
const Provider = providerModule.VerilogFormatProvider;
27+
const logger: any = {
28+
info: () => {},
29+
error: () => {},
30+
warn: () => {},
31+
debug: () => {},
32+
};
33+
logger.getChild = () => logger;
34+
const provider = new Provider(logger);
35+
const doc = {
36+
uri: { fsPath: tmp },
37+
getText: () => fs.readFileSync(tmp, 'utf8'),
38+
positionAt: (_o: number) => new vscodeStub.Position(0, 0),
39+
languageId: 'verilog',
40+
};
41+
42+
assert.doesNotThrow(() => provider.provideDocumentFormattingEdits(doc as any, {} as any, {} as any));
43+
});
44+
});

0 commit comments

Comments
 (0)