Skip to content

Fix format cwd fallback #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@
"@types/glob": "^8.1.0",
"@types/mocha": "~10.0.6",
"@types/node": "~22.10.0",
"@types/proxyquire": "^1.3.31",
"@types/vscode": "^1.75.0",
"@types/which": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^7.3.1",
Expand Down
7 changes: 6 additions & 1 deletion src/ctags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as vscode from 'vscode';
import {exec as execNonPromise} from 'child_process';
import * as util from 'util';
import * as path from 'path';
import { Logger } from './logger';
const exec = util.promisify(execNonPromise);

Expand Down Expand Up @@ -200,7 +201,7 @@
// pattern = parts[2];
type = parts[3];
// override "type" for parameters (See #102)
if (parts.length == 6 && parts[5] === 'parameter:') {

Check warning on line 204 in src/ctags.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (macos-latest)

Expected '===' and instead saw '=='

Check warning on line 204 in src/ctags.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (windows-latest)

Expected '===' and instead saw '=='

Check warning on line 204 in src/ctags.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (ubuntu-latest)

Expected '===' and instead saw '=='
type = 'parameter';
}
if (parts.length >= 5) {
Expand Down Expand Up @@ -366,7 +367,11 @@
}

// kick off async job for indexing for module.sv
let searchPattern = new vscode.RelativePattern(vscode.workspace.workspaceFolders[0], `**/${moduleToFind}.sv`);
const wsFolders = vscode.workspace.workspaceFolders;
const base = wsFolders && wsFolders.length > 0
? wsFolders[0]
: path.dirname(document.uri.fsPath);
let searchPattern = new vscode.RelativePattern(base, `**/${moduleToFind}.sv`);
let files = await vscode.workspace.findFiles(searchPattern);
if (files.length !== 0) {
let file = await vscode.workspace.openTextDocument(files[0]);
Expand Down
4 changes: 3 additions & 1 deletion src/linter/BaseLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export default abstract class BaseLinter {
if (path.isAbsolute(inputPath)) {
return inputPath;
}
return path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, inputPath);
const wsFolders = vscode.workspace.workspaceFolders;
const base = wsFolders && wsFolders.length > 0 ? wsFolders[0].uri.fsPath : '';
return path.join(base, inputPath);
}

public startLint(doc: vscode.TextDocument) {
Expand Down
5 changes: 3 additions & 2 deletions src/linter/IcarusLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ export default class IcarusLinter extends BaseLinter {
let command: string = binPath + ' ' + args.join(' ');

// TODO: We have to apply the the #419 fix?
const wsFolders = vscode.workspace.workspaceFolders;
let cwd: string =
this.runAtFileLocation || vscode.workspace.workspaceFolders === undefined
this.runAtFileLocation || !wsFolders || wsFolders.length === 0
? path.dirname(doc.uri.fsPath)
: vscode.workspace.workspaceFolders[0].uri.fsPath;
: wsFolders[0].uri.fsPath;

this.logger.info('Execute');
this.logger.info(' command: ', command);
Expand Down
5 changes: 4 additions & 1 deletion src/linter/SlangLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ export default class SlangLinter extends BaseLinter {
args.push(`"${docUri}"`);
let command: string = binPath + ' ' + args.join(' ');

const wsFolders = vscode.workspace.workspaceFolders;
let cwd: string = this.runAtFileLocation
? isWindows
? cwdWin
: docFolder
: vscode.workspace.workspaceFolders[0].uri.fsPath;
: wsFolders && wsFolders.length > 0
? wsFolders[0].uri.fsPath
: path.dirname(doc.uri.fsPath);

this.logger.info('[slang] Execute');
this.logger.info('[slang] command: ' + command);
Expand Down
5 changes: 4 additions & 1 deletion src/linter/VerilatorLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ export default class VerilatorLinter extends BaseLinter {
? this.convertToWslPath(path.dirname(doc.uri.fsPath))
: path.dirname(doc.uri.fsPath).replace(/\\/g, '/')
: path.dirname(doc.uri.fsPath);
const wsFolders = vscode.workspace.workspaceFolders;
let cwd: string = this.runAtFileLocation
? isWindows
? path.dirname(doc.uri.fsPath.replace(/\\/g, '/'))
: docFolder
: vscode.workspace.workspaceFolders[0].uri.fsPath;
: wsFolders && wsFolders.length > 0
? wsFolders[0].uri.fsPath
: path.dirname(doc.uri.fsPath);
let verilator: string = isWindows
? this.useWSL
? 'wsl verilator'
Expand Down
7 changes: 6 additions & 1 deletion src/providers/FormatPrivider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
import * as vscode from 'vscode';
import * as child_process from 'child_process';

Check warning on line 3 in src/providers/FormatPrivider.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (macos-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase

Check warning on line 3 in src/providers/FormatPrivider.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (windows-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase

Check warning on line 3 in src/providers/FormatPrivider.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (ubuntu-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase
import * as fs from 'fs';
import * as os from 'os';
import * as crypto from 'crypto';
Expand All @@ -23,7 +23,7 @@
}

public readFileSync(
options: BufferEncoding | { encoding: BufferEncoding; flag?: string }

Check warning on line 26 in src/providers/FormatPrivider.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (macos-latest)

'BufferEncoding' is not defined

Check warning on line 26 in src/providers/FormatPrivider.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (windows-latest)

'BufferEncoding' is not defined

Check warning on line 26 in src/providers/FormatPrivider.ts

View workflow job for this annotation

GitHub Actions / Upload vsix package (ubuntu-latest)

'BufferEncoding' is not defined
): string {
return fs.readFileSync(this.path, options);
}
Expand Down Expand Up @@ -68,7 +68,12 @@
let binPath: string = this.config.get('path');
this.logger.info('Executing command: ' + binPath + ' ' + args.join(' '));
try {
child_process.execFileSync(binPath, args, {cwd: vscode.workspace.workspaceFolders[0].uri.fsPath});
const wsFolders = vscode.workspace.workspaceFolders;
const cwd =
wsFolders && wsFolders.length > 0
? wsFolders[0].uri.fsPath
: path.dirname(document.uri.fsPath);
child_process.execFileSync(binPath, args, { cwd });
let formattedText: string = tempFile.readFileSync({ encoding: 'utf-8' });
let wholeFileRange: vscode.Range = new vscode.Range(
document.positionAt(0),
Expand Down
44 changes: 44 additions & 0 deletions src/test/suite/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as assert from 'assert';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as proxyquire from 'proxyquire';

suite('Formatting Provider', () => {
test('does not crash without workspace', () => {
const tmp = path.join(os.tmpdir(), 'fmt.v');
fs.writeFileSync(tmp, 'module m; endmodule');

const vscodeStub = {
workspace: {
getConfiguration: () => ({ get: () => '/bin/true' }),
workspaceFolders: undefined,
},
Range: class { constructor(public start: any, public end: any) {} },
Position: class { constructor(public line: number, public char: number) {} },
TextEdit: { replace: (_r: any, _t: any) => ({}) },
};

const providerModule = proxyquire('../../providers/FormatPrivider', {
vscode: vscodeStub,
});

const Provider = providerModule.VerilogFormatProvider;
const logger: any = {
info: () => {},
error: () => {},
warn: () => {},
debug: () => {},
};
logger.getChild = () => logger;
const provider = new Provider(logger);
const doc = {
uri: { fsPath: tmp },
getText: () => fs.readFileSync(tmp, 'utf8'),
positionAt: (_o: number) => new vscodeStub.Position(0, 0),
languageId: 'verilog',
};

assert.doesNotThrow(() => provider.provideDocumentFormattingEdits(doc as any, {} as any, {} as any));
});
});
Loading